列计数与第1行的值计数不匹配JAVA mysql [英] Column count doesn't match value count at row 1 JAVA mysql

查看:269
本文介绍了列计数与第1行的值计数不匹配JAVA mysql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到错误java.sql.SQLException:

I have encountered an error, 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更改为所有字符串,但是错误仍然出现.

It's super frustrating, because I have been making amendments to the code like changing in the arg to all string, but the error still appears.

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_general_ci否无
时间文本utf8_general_ci否无
需求文本utf8_general_ci否无
供应文字utf8_general_ci

Database: Field Type Collation Attributes Null Default Extra Action id int(11) No None
Date text utf8_general_ci No None
Time text utf8_general_ci No None
Demand text utf8_general_ci No None
SUPPLY text utf8_general_ci

推荐答案

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

Well, I suspect this is the problem:

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

您还没有指定这些参数中的每一个都应指向哪一列-我怀疑您没有6列.即使您在表中有6列,最好还是在SQL中明确声明要为每个参数使用哪一列.

You haven't specified which column each of those parameters is meant to refer to - and I suspect you've not got 6 columns. Even if you do have 6 columns in the table, it would be a good idea to explicitly state in the SQL which column you mean to use for each parameter.

这篇关于列计数与第1行的值计数不匹配JAVA mysql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆