Java 使用 Jconnect

发布于 2024-10-19 03:22:35 字数 1582 浏览 1 评论 0原文

再会!

为了访问 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情定在深秋 2024-10-26 03:22:35

如果该字段是自动递增的,则不应为其分配任何内容,将其从 SQL 准备好的语句字符串中删除,然后设置其他所有内容,自动递增将自行完成工作。

PreparedStatement stmt = connect.prepareStatement("INSERT INTO table_name (column1, column2, column3,...) VALUES (value1, value2, value3,...) ");

删除第一列 to have:

PreparedStatement stmt = connect.prepareStatement("INSERT INTO table_name (column2, column3,...) VALUES ( value2, value3,...) ");

并将值替换为问号,然后按照稍后的操作进行设置。

          stmt.setString(1, jTextField1.getText());
          stmt.setString(2, jTextField2.getText());
          stmt.setString(3, jTextField3.getText());
          stmt.setString(4, jTextField4.getText());
          stmt.setString(5, Jan2.getSelectedItem().toString());
          stmt.setString(6, Jan3.getSelectedItem().toString());
          stmt.setString(7, jTextField5.getText());
          stmt.setString(8, jTextField6.getText());
          stmt.setString(9, jTextField8.getText());
          stmt.executeUpdate();

注意:递减的索引(问号值的数量)。

希望有帮助!

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.

PreparedStatement stmt = connect.prepareStatement("INSERT INTO table_name (column1, column2, column3,...) VALUES (value1, value2, value3,...) ");

get rid of first column to have:

PreparedStatement stmt = connect.prepareStatement("INSERT INTO table_name (column2, column3,...) VALUES ( value2, value3,...) ");

and replace values with question marks and set them as you do later on.

          stmt.setString(1, jTextField1.getText());
          stmt.setString(2, jTextField2.getText());
          stmt.setString(3, jTextField3.getText());
          stmt.setString(4, jTextField4.getText());
          stmt.setString(5, Jan2.getSelectedItem().toString());
          stmt.setString(6, Jan3.getSelectedItem().toString());
          stmt.setString(7, jTextField5.getText());
          stmt.setString(8, jTextField6.getText());
          stmt.setString(9, jTextField8.getText());
          stmt.executeUpdate();

NOTICE: decremented indices (the number of the question mark value).

Hope that helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文