1

i am getting error as following code in java file name javaDAO try to call a method from jbutton. in my javaDAO file 3 method is define 1.extablishConnection 2.retriveName 3.CloseConnection at time of getting userName and password from .access file it gives following errors

Dec 8, 2013 11:31:59 AM vuAssignment.JFrame jButton1ActionPerformed
SEVERE: null
java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver]COUNT field incorrect
at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6957)
at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7114)
at sun.jdbc.odbc.JdbcOdbc.SQLExecute(JdbcOdbc.java:3149)
    at sun.jdbc.odbc.JdbcOdbcPreparedStatement.execute
                                            (JdbcOdbcPreparedStatement.java:216)
    at sun.jdbc.odbc.JdbcOdbcPreparedStatement.executeQuery
                                             (JdbcOdbcPreparedStatement.java:91)
at vuAssignment.javaDAO.retrieveName(javaDAO.java:33)
at vuAssignment.JFrame.jButton1ActionPerformed(JFrame.java:139)

Code in javaDAO file is which is call by main method in JFrame.java in netbeans IDE

  //method to establish connection
 private void establishConnection() throws ClassNotFoundException,SQLException{
 String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
 String url = "jdbc:odbc:ProductDb";
 Class.forName(driver);
 con = DriverManager.getConnection(url);
  }
   // method to call query
 public void retrieveName(String pName,String pWord) throws 
 ClassNotFoundException,SQLException{
 String user1= "";
 String pass1 = "";
 String sql  = "SELECT * FROM admin where userName= ? && password= ?";
 PreparedStatement stmt = con.prepareStatement(sql);
 ResultSet res = stmt.executeQuery();
        while (res.next()) {
            user1 = res.getString("userName");
            pass1 = res.getString("password");
        }
        if (pName.equals(user1) && pWord.equals(pass1)) 
        JOptionPane.showMessageDialog(null,"correct");
        else
         JOptionPane.showMessageDialog(null,"incorrect");

2 Answers 2

3

I believe you are confused in using PreparedStatement and ResultSet. SQL query parameters are not set before executing the query. You need to set userName and password like

String sql  = "SELECT * FROM admin where userName= ? && password= ?";
 PreparedStatement stmt = con.prepareStatement(sql);
//you need the set the parameters
 stmt.setString(1,userName);
 stmt.setString(2,password);
 //the result set will contain the column values of your query            
 ResultSet res = stmt.executeQuery();
        while (res.next()) {
            //to get the value of the first col
            user1 = res.getString(column1_name);
            //to get the value of the second col
            pass1 = res.getString(column2_name);
        }

Hope this helps, Please read the documentation of PreparedStatement and ResultSet

Sign up to request clarification or add additional context in comments.

2 Comments

Two other problems that I can see: (1) The query should use AND instead of && in the WHERE clause. (2) PASSWORD is a reserved word in Access SQL and should be enclosed in square brackets: [password].
Yes, I couldn't find the && problem. nice catch! I couldn't know the second one is a problem, I learnt it today. Thanks for the information
0

Check out with your code. Here is something missing.

stmt.setString(1,userName);
 stmt.setString(2,password);

Put at right place, after the con.prepareStatement.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.