错误如下:
Error - This signifies the try catch statement with sql
Oct 29, 2017 5:19:43 PM tenantcheck.Check jButton1MouseClicked
SEVERE: null
java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0).
码
private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {
Connection con = null;
Statement statement = null;
try{
con = DriverManager.getConnection(CONN_STRING,USERNAME,PASSWORD);
if (con == null){
System.out.println("Connected to database");
}
String Cred;
try {
statement = con.createStatement();
} catch (SQLException ex) {
Logger.getLogger(Check.class.getName()).log(Level.SEVERE, null, ex);
System.out.println("Didnt Werk");
}
String name = JOptionPane.showInputDialog("Please enter Tenant Name");
int dialogButton = JOptionPane.YES_NO_OPTION;
int dialogResult = JOptionPane.showConfirmDialog (null, "Was credit check authority approved?","Important",dialogButton);
if(dialogResult == JOptionPane.YES_OPTION){
Cred = "Yes";
// Saving code here
}
else {
Cred = "No";
}
double income = Double.parseDouble(JOptionPane.showInputDialog("Please enter tenant income"));
double BalSet = Double.parseDouble(JOptionPane.showInputDialog("Please Enter tenants balanced settled"));
double moneyrent = 0.7 * income;
if ( Cred == "No"||BalSet < 0|| income < 5000 ){
JOptionPane.showMessageDialog(null,"The user doesnt qualify for any of the properties");
}
else {
try {
String insertQuery = "SELECT * FROM unit WHERE "+ moneyrent +" => Cost And Occupied = 1;";
PreparedStatement pstmt = con.prepareStatement(insertQuery);
pstmt.setDouble(1,moneyrent);
statement.execute(insertQuery);
txtUnit.setText( insertQuery);
//statement.execute(insertQuery);
} catch (SQLException ex) {
Logger.getLogger(Check.class.getName()).log(Level.SEVERE, null, ex);
System.out.println("Error");
}
}
}catch(Exception e) {
System.out.println("Not Connected");
}
}
&#13;
答案 0 :(得分:1)
您的preparedStatement中没有任何参数,但您尝试填写参数:
pstmt.setDouble(1,moneyrent);
您的准备声明应为:
SELECT * FROM unit WHERE Cost >= ? And Occupied = 1