molesterman69 molesterman69 - 1 month ago 7
Java Question

My netbeans thinks that the value which entered into the textfield is a column name and not the value which i need it to delete

private void deleteActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:

try
{
// String sql = "Delete from tender.bidder where b_id =" + bidtxt.getText();
PreparedStatement pstmt = con.prepareStatement("delete from tender.bidder where bidder.b_id ="
+ bidtxt.getText());
int i = pstmt.executeUpdate();
System.out.print(i+"record deleted");
con.close();
bidtxt.setText("");
bnametxt.setText("");
officetxt.setText("");
streettxt.setText("");
citytxt.setText("");
statetxt.setText("");
contacttxt.setText("");
passwordtxt.setText("");
emailtxt.setText("");
tidtxt.setText("");
con.close();
}
catch(SQLException e)
{
e.printStackTrace();
}
selectionall();

}


The error : java.sql.SQLSyntaxErrorException: Column 'B001' is either not in any table in the FROM list or appears within a join specification and is outside the scope of the join specification or appears in a HAVING clause and is not in the GROUP BY list. If this is a CREATE or ALTER TABLE statement then 'B001' is not a column in the target table. THANK You in advance.

Answer

You are not using the PreparedStatement properly. You would use a ? where your values are supposed to be. However, you are also missing the quotes you need around the string in your statement which is causing your syntax error.

// Proper PreparedStatement example.
PreparedStatement pstmt = con.prepareStatement("DELETE FROM tender.bidder WHERE bidder.b_id = ?");
pstmt.setString(1, bidtxt.getText());
int i = pstmt.executeUpdate();

// String values need '' quotes.
Statement s = con.createStatement();
s.executeUpdate("DELETE FROM tender.bidder WHERE bidder.b_id = '"+bidtxt.getText()+"'");