Search code examples
javams-accessjdbcjdbc-odbc

trouble in the database and checking the database in java


I am having trouble in checking the username in the ms access database

java.sql.SQLException: No data found

here is the database connect

    public class dbAccess 
{
    Connection conn;
    ResultSet rs;
    Statement s;
    PreparedStatement ps;
    public void doConnect()
    {
        try
        {
            String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
            Class.forName(driver);
            String login = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=E:\\File Kuliah\\Semester 7\\AOOP\\JavaApplication1\\build\\classes\\Database1.accdb";
            conn = DriverManager.getConnection(login);
            System.out.println("Connected");
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
    }
}

and here is the code that has the error

private void registerButtonActionPerformed(java.awt.event.ActionEvent evt) {                                               
    try
        {   
                db.ps =  db.conn.prepareStatement("SELECT * FROM DatabaseAOOP");
                db.rs = db.ps.executeQuery();
                System.out.print("1");
                while(db.rs.next())
                {
                    if(userRegis.getText().equals(db.rs.getString("username"))) 
                    {
                        System.out.print("3");
                        JOptionPane.showMessageDialog(this, "Username is already exists !");
                        userRegis.setText("");
                        passRegis.setText("");
                        //db.rs=null;
                        //db.ps=null;
                        break;

                    }
                    else if(userRegis.getText().isEmpty())
                    {
                        JOptionPane.showMessageDialog(this, "Please input the name");
                    }
                    else if(!userRegis.getText().equals(db.rs.getString("username")))
                    {
                            JOptionPane.showMessageDialog(this, "Searching ...");
                    }
                    else
                    {
                        System.out.print("2");
                        db.ps =  db.conn.prepareStatement("INSERT INTO DatabaseAOOP(username,password) VALUES(?,?)");
                        db.s = db.conn.createStatement();
                        db.ps.setString(1, userRegis.getText());
                        db.ps.setString(2, passRegis.getText());
                        db.ps.executeUpdate();
                        JOptionPane.showMessageDialog(this, "Thank you for registering !");
                        userRegis.setText("");
                        passRegis.setText("");
                        break;
                    }
                }
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
            System.out.println(ex);

        }      
}

here are the printstacktrace

at sun.jdbc.odbc.JdbcOdbc.SQLGetDataString(JdbcOdbc.java:3914)
at sun.jdbc.odbc.JdbcOdbcResultSet.getDataString(JdbcOdbcResultSet.java:5697)
at sun.jdbc.odbc.JdbcOdbcResultSet.getString(JdbcOdbcResultSet.java:353)
at sun.jdbc.odbc.JdbcOdbcResultSet.getString(JdbcOdbcResultSet.java:410)
at Frame.registerButtonActionPerformed(Frame.java:277)
at Frame.access$600(Frame.java:4)
at Frame$7.actionPerformed(Frame.java:135)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6516)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3320)
at java.awt.Component.processEvent(Component.java:6281)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4872)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4698)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4698)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:708)
at java.awt.EventQueue$4.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

actually i just checked the database connected, but there is no data found


Solution

  • Immediately after entering the while(db.rs.next()) loop you call db.rs.getString("username"), then later in the loop you call db.rs.getString("username") again. It is the second invocation that throws the "No data found" exception.

    In many cases JDBC will only let us call get... methods once per column for a given (current) row in the ResultSet. You should save the value to a String

    String theUserName = db.rs.getString("username");
    

    and then use the String variable for your (multiple) comparisons.