简体   繁体   中英

SQL Exception: java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0)

try {  
    Class.forName("com.mysql.jdbc.Driver");
    String connectionUrl = "jdbc:mysql://Localhost/basic_credit? autoReconnect=true&useSSL=false" ;

    Connection con = DriverManager.getConnection(connectionUrl,"root","superchan009");
    String sql="INSERT INTO new_table(date, time, customer_name, address, contact#1, contact#2, item_name, final_price, downpayment, remaining_balance, length_ofinstallment, payment_permonth, first_due, last_due)VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
    PreparedStatement ps=con.prepareStatement(sql);

    ps.setString(1,jLabel16.getText());
    ps.setString(2,jLabel17.getText());
    ps.setString(3,tf1.getText());
    ps.setString(4,tf2.getText());
    ps.setString(5,tf3.getText());
    ps.setString(6,tf4.getText());
    ps.setString(7,tf6.getText());
    ps.setString(8,tf7.getText());
    ps.setString(9,tf8.getText());
    ps.setString(10,tf9.getText());
    ps.setString(11,tf10.getText());
    ps.setString(12,tf11.getText());
    ps.setString(13,tf12.getText());
    ps.setString(14,tf13.getText());

    ps.executeUpdate();
    JOptionPane.showMessageDialog(null,"DATA SAVED! THANK YOU!");

} catch (SQLException e) {
    System.out.println("SQL Exception: "+ e.toString());
} catch (ClassNotFoundException cE) {
    System.out.println("Class Not Found Exception: "+ cE.toString());
}

您在列名中使用#正在创建问题...您应该从列名中删除#标记,并将其更正为数据库。

try {  
    Class.forName("com.mysql.jdbc.Driver");
    String connectionUrl = "jdbc:mysql://Localhost/basic_credit? autoReconnect=true&useSSL=false" ;

    Connection con = DriverManager.getConnection(connectionUrl,"root","superchan009");
    String sql="INSERT INTO new_table(date, time, client_name, address, contact1, contact2, item_name, final_price, downpayment, remaining_balance, length_ofinstallment, payment_permonth, first_due, last_due) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
    PreparedStatement ps=con.prepareStatement(sql);

    ps.setString(1,jLabel16.getText());
    ps.setString(2,jLabel17.getText());
    ps.setString(3,tf1.getText());
    ps.setString(4,tf2.getText());
    ps.setString(5,tf3.getText());
    ps.setString(6,tf4.getText());
    ps.setString(7,tf6.getText());
    ps.setString(8,tf7.getText());
    ps.setString(9,tf8.getText());
    ps.setString(10,tf9.getText());
    ps.setString(11,tf10.getText());
    ps.setString(12,tf11.getText());
    ps.setString(13,tf12.getText());
    ps.setString(14,tf13.getText());

    ps.executeUpdate();
    JOptionPane.showMessageDialog(null,"DATA SAVED! THANK YOU!");

} catch (SQLException e) {
    System.out.println("SQL Exception: "+ e.toString());
} catch (ClassNotFoundException cE) {
    System.out.println("Class Not Found Exception: "+ cE.toString());
}

SQL to JAVA connection is case sensitive if you're going to use special char "#" for column names.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM