在数据库中出现问题并在java

时间:2016-01-12 16:38:30

标签: java ms-access jdbc jdbc-odbc

我无法检查ms访问数据库中的用户名

java.sql.SQLException: No data found

这里是数据库连接

    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();
        }
    }
}

这是有错误的代码

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);

        }      
}

这是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)

实际上我只检查了连接的数据库,但没有找到数据

1 个答案:

答案 0 :(得分:0)

在进入while(db.rs.next())循环后,您立即致电db.rs.getString("username"),然后在循环中稍后再次致电db.rs.getString("username")。这是第二次调用抛出“No data found”异常。

在许多情况下,JDBC只允许我们为ResultSet中的给定(当前)行每列调用get...个方法一次。您应该将值保存到String

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

然后使用String变量进行(多次)比较。