[英]java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0). why such exception is occurring and how to resolve it
[英]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到JAVA的連接區分大小寫。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.