Java 使用 Jconnect
再会!
为了访问 mysql 服务器,我使用了 JConnect,我的代码如下:
public AddBooks() {
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/catalog";
conn = DriverManager.getConnection(url,"root","upittc");
stmt = conn.prepareStatement("INSERT INTO books VALUES(?,?,?,?,?,?,?,?,?,?,)");
} catch (Exception exc) {
JOptionPane.showMessageDialog(null, exc.getMessage());
}
initComponents();
}
为了将数据放入数据库,我使用了以下代码:
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
try {
stmt.setString(1, jTextField0.getText());
stmt.setString(2, jTextField1.getText());
stmt.setString(3, jTextField2.getText());
stmt.setString(4, jTextField3.getText());
stmt.setString(5, jTextField4.getText());
stmt.setString(6, Jan2.getSelectedItem().toString());
stmt.setString(7, Jan3.getSelectedItem().toString());
stmt.setString(8, jTextField5.getText());
stmt.setString(9, jTextField6.getText());
stmt.setString(10, jTextField8.getText());
stmt.executeUpdate();
JOptionPane.showMessageDialog(null, "Save Successful!");
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, ex);
}
}
But There's an error on row 1. COLUMN COUNT DOESN'T MATCH THE VALUE AT ROW 是什么意思? 请指教。谢谢。
Good day!
In order to access the mysql server, I used the JConnect and my code is as follows:
public AddBooks() {
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/catalog";
conn = DriverManager.getConnection(url,"root","upittc");
stmt = conn.prepareStatement("INSERT INTO books VALUES(?,?,?,?,?,?,?,?,?,?,)");
} catch (Exception exc) {
JOptionPane.showMessageDialog(null, exc.getMessage());
}
initComponents();
}
To put the data in the database, I used the following code:
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
try {
stmt.setString(1, jTextField0.getText());
stmt.setString(2, jTextField1.getText());
stmt.setString(3, jTextField2.getText());
stmt.setString(4, jTextField3.getText());
stmt.setString(5, jTextField4.getText());
stmt.setString(6, Jan2.getSelectedItem().toString());
stmt.setString(7, Jan3.getSelectedItem().toString());
stmt.setString(8, jTextField5.getText());
stmt.setString(9, jTextField6.getText());
stmt.setString(10, jTextField8.getText());
stmt.executeUpdate();
JOptionPane.showMessageDialog(null, "Save Successful!");
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, ex);
}
}
But there's an error on row 1. COLUMN COUNT DOESN'T MATCH THE VALUE AT ROW What does it mean?
Please advise. Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果该字段是自动递增的,则不应为其分配任何内容,将其从 SQL 准备好的语句字符串中删除,然后设置其他所有内容,自动递增将自行完成工作。
删除第一列 to have:
并将值替换为问号,然后按照稍后的操作进行设置。
注意:递减的索引(问号值的数量)。
希望有帮助!
If the field is auto-increment, you should not assign anything to it, remove it from your SQL prepared statement string and just set everything else, the auto increment will do the job by itself.
get rid of first column to have:
and replace values with question marks and set them as you do later on.
NOTICE: decremented indices (the number of the question mark value).
Hope that helps!