Java SQL problems

208 views Asked by At

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

There are 2 answers

0
Aditya On

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.

2
Keerthivasan On

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