java.sql.SQLException:列计数与第 1 行的值计数不匹配

java.sql.SQLException:列计数与第 1 行的值计数不匹配

问题描述:

我的表的结构:

id int AUTO_INCREMENT PRIMARY KEY
title text
url text
age int

这是我尝试将数据保存到此表中的方法:

Here's how I am trying to save data into this table:

PreparedStatement ps=con.prepareStatement("insert into table(title, url, age) values ('"+title+","+url+","+age+"')");
System.out.println("Connected database successfully..");
ps.executeUpdate(); 

但是当我运行应用程序时,我得到

But when I run the app, I get

java.sql.SQLException: 列计数与第 1 行的值计数不匹配

java.sql.SQLException: Column count doesn't match value count at row 1

我猜问题可能出在id列,如何解决?

I guess the problem might be in the id column, how to solve it?

实际上,您遇到了不同的问题(您只传递一个值")-

Actually, you have a different problem (you're only passing one "value") -

PreparedStatement ps=con.prepareStatement("insert into table(title, url, age) "
    + "values (?,?,?)");
ps.setString(1, title);
ps.setString(2, url);
ps.setInt(3, age); // <-- at a guess!

您的原始查询将所有三个值都放在一个字符串中 '"+title+","+url+","+age+"'.

You original query put all three values in one string '"+title+","+url+","+age+"'.