列计数不为';第1行JAVA mysql的值计数不匹配

列计数不为';第1行JAVA mysql的值计数不匹配,java,mysql,Java,Mysql,我遇到了一个错误,java.sql.SQLException: Column count doesn't match value count at row 1 at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3609) at com.mysql.jdbc.MysqlIO.checkErrorPa

我遇到了一个错误,java.sql.SQLException:

    Column count doesn't match value count at row 1
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3609)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3541)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2002)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2163)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2624)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2127)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2427)
这非常令人沮丧,因为我一直在修改代码,比如将arg改为all字符串,但仍然出现错误

public void readDataBase(int val, String d1, String d2, String d3, String d4 ) throws     Exception {
try {
// This will load the MySQL driver, each DB has its own driver
Class.forName("com.mysql.jdbc.Driver");
// Setup the connection with the DB
connect = DriverManager
.getConnection("jdbc:mysql://localhost/MAXdb?"+ "user=root&password=");

// Statements allow to issue SQL queries to the database
statement = connect.createStatement();
// Result set get the result of the SQL query       
   resultSet = statement
.executeQuery("select * from MAXdb.emcsg");
writeResultSet(resultSet);

// PreparedStatements can use variables and are more efficient
preparedStatement = connect
.prepareStatement("insert into  MAXdb.emcsg values (default,?, ?, ? , ?, ?)");
// Parameters start with 1

preparedStatement.setInt(1, val);
    preparedStatement.setString(2, d1);
preparedStatement.setString(3, d2);
preparedStatement.setString(4, d3);
preparedStatement.setString(5, d4);


preparedStatement.executeUpdate();

preparedStatement = connect
.prepareStatement("SELECT  id, Date, Time, Demand, SUPPLY from MAXdb.emcsg");
resultSet = preparedStatement.executeQuery();
writeResultSet(resultSet);



    } catch (Exception e) {
throw e;
} finally {
close();
}

}
我的第二节课:

public void Csvreader() throws IOException {
try {
// TODO code application logic here

CSVReader reader = new CSVReader(new FileReader("D:/TEST.csv"));

String  nextLine[];
int i = 1;
Mysql sen = new Mysql();
while ((nextLine = reader.readNext()) != null) {
try {
sen.readDataBase( i, nextLine[0], nextLine[1], nextLine[2], nextLine[3] );
i = i+1;
} catch (Exception ex) {
Logger.getLogger(Opencsv.class.getName()).log(Level.SEVERE, null, ex);
}
}


} catch (FileNotFoundException ex) {
Logger.getLogger(Opencsv.class.getName()).log(Level.SEVERE, null, ex);
}

}
数据库: 字段类型排序规则属性Null默认额外操作 id int(11)无无
日期文本utf8\u常规\u ci编号无
时间文本utf8\u常规\u ci编号无
需求文本utf8\u常规\u ci编号无

提供文本utf8\u general\u ci

好吧,我怀疑这就是问题所在:

insert into  MAXdb.emcsg values (default,?, ?, ? , ?, ?)

您还没有指定这些参数中的每一个都要引用哪一列,我怀疑您还没有得到6列。即使表中有6列,最好在SQL中明确说明每个参数要使用哪一列。

下面的语句:

insert into  MAXdb.emcsg values (default,?, ?, ? , ?, ?)

导致错误。检查emcsg是否有6列。

数据库表中是否确实有6列


总是用谷歌搜索错误消息或错误代码。通常,返回的第一个链接会清楚地解释问题并提供解决方案。

在我看来,您的数据库结构存在问题。我认为您没有假设的所有字段。

您的方法签名

public void readDataBase(int val, String d1, String d2, String d3, String d4 )
有5个变量,但方法调用有6个:

preparedStatement = connect
.prepareStatement("insert into  MAXdb.emcsg values (default,?, ?, ? , ?, ?)");

你试过改变这个吗

preparedStatement = connect
.prepareStatement("insert into  MAXdb.emcsg values (default,?, ?, ? , ?, ?)");
对此

preparedStatement = connect
.prepareStatement("insert into  MAXdb.emcsg values (?, ?, ? , ?, ?)");
那可能对你有用


问候

我刚刚检查过,MySQL中只有5个字段。谢谢您的帮助!我很感激!我刚检查过,MySQL中只有5个字段。谢谢你的帮助!我发现了我的错误!这没关系,该方法可以硬编码一些值,或者从一个或多个其他参数计算它们。也许我不清楚:不管sig方法有多少个参数,重要的是INSERT语句的值数与表中的列数相同。@PaulMedcraft:我注意到我的错,我想投票给其他人,但似乎点击的位置不对。对不起,我的错。我一点击就意识到了我的错误。事实上,我的坏朋友,根本没有被否决!谢谢我刚才发现了我的错误!谢谢我刚才设法解决了,但我很感激@黄:不客气。很高兴你解决了。当做