SQL异常:java.sql.SQLException:参数索引超出范围(1>参数个数,为0)

时间:2017-04-18 15:55:51

标签: java sql jdbc

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());
}

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分: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, 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到JAVA连接区分大小写。