Issue
Error As follows:
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).
code
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");
}
}
Solution
You don't have any parameters in your preparedStatement, yet you try to fill in a parameter:
pstmt.setDouble(1,moneyrent);
Your prepare statement should be:
SELECT * FROM unit WHERE Cost >= ? And Occupied = 1
Answered By - AlexM
Answer Checked By - Willingham (JavaFixing Volunteer)