Reputation: 57
I am currently working on an SQL project, and I am making a GUI on Netbeans. I have found a code that someone used, and I wish to alter it around. However, I am currently getting an error that I don't know how to fix it. I have contacted the person who created this code, but I didn't get a response. The error I get after running my code is:
com.microsoft.sqlserver.jbdc.SQLServerException: The method executeQuery() cannot take arguments on a PreparedStatement or CallableStatement
Here is the portion of the code I copied
private void signinActionPerformed(java.awt.event.ActionEvent evt) {
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url = "jdbc:sqlserver://SQL2.cis245.mc3.edu:1433;\" + \"databaseName=zz_CIS245_16;user=tpatel;password=tpatel";
Connection con = DriverManager.getConnection(url);
System.out.println("connection created");
String sql="select * from test where Username=? and Passeword=?";
PreparedStatement pst = con.prepareStatement(sql);
pst.setString(1, username.getText());
pst.setString(2, password.getText());
ResultSet rs=pst.executeQuery(sql);
if(rs.next())
{
JOptionPane.showMessageDialog(null, "password and username matched");
//System.out.println("Address : "+rs.getString(2));
}
else {
JOptionPane.showMessageDialog(null,"password or username not corrected");
username.setText("");
password.setText("");
}
con.close();
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null, e);
}
}
Upvotes: 0
Views: 52
Reputation: 33
You have already passed the query to the PreparedStatement variable here:
PreparedStatement pst = con.prepareStatement(sql);
Just remove the argument from exectuteQuery() method
ResultSet rs=pst.executeQuery();
Upvotes: 1