在Java中从Access数据库检索数据时获取NullPointerException

时间:2015-12-12 08:15:36

标签: java database

我正在尝试用Java从Access数据库中检索已保存的数据,但是获得了nullpointerexception。我无法弄清楚问题所在。这是代码:

cnnct.connect();
try {
     cnnct.rs = cnnct.statement.executeQuery(getMR());
     Reception b = new Reception();
     System.out.println("adadas");
     while (cnnct.rs.next()) {
          String a = cnnct.rs.getString("MRNumber");
          String c = cnnct.rs.getString("PatientName");
          String d = cnnct.rs.getString("FatherName");
          String e = "a";//cnnct.rs.getString("Age");
          String f = cnnct.rs.getString("Address");
          String g = cnnct.rs.getString("Contact");
          String h = cnnct.rs.getString("CNIC");
          String i = cnnct.rs.getString("Consultant");
          String j = cnnct.rs.getString("Gender");
          System.out.println(b+ c);
          b.editRecord(mrNumberInput.getText(), c, d, e, f, g, h, i, j);
     }
}
catch (Exception e) {
     System.out.println(e);
}

我尝试将所有getString()方法直接写入b.editRecord()的参数列表,但问题仍然存在。而cnnct是连接到db的类的对象。它工作正常。 以下是getMR()函数的代码。但问题似乎是在while循环中。

public String getMR () {
    System.out.println("adads");
    String mr = mrNumberInput.getText();
    String sql = "select MRNumber, PatientName, FatherName, Age, Address, Contact, CNIC, Consultant, Gender from Patient where MRNumber = " + mr;
    return sql;
}

我写的一些打印语句只是为了测试代码是否正常工作,它们将被忽略。感谢

这是错误报告:

  

1   net.ucanaccess.jdbc.UcanaccessSQLException:UCAExc ::: 3.0.2用户缺少未找到的权限或对象:MS

     

2   UCAExc ::: 3.0.2用户缺少未找到的权限或对象:MS

     

3   UCAExc ::: 3.0.2用户缺少未找到的权限或对象:MS

     

4   java.sql.SQLSyntaxErrorException:user缺少未找到的权限或对象:MS

     

5   [net.ucanaccess.jdbc.UcanaccessStatement.executeQuery(UcanaccessStatement.java:211),sad.ReceptionView.okButtonMouseClicked(ReceptionView.java:111),sad.ReceptionView.access $ 000(ReceptionView.java:14),sad.ReceptionView $ 1。 mouseClicked(ReceptionView.java:68),java.awt.Component.processMouseEvent(Component.java:6528),javax.swing.JComponent.processMouseEvent(JComponent.java:3324),java.awt.Component.processEvent(Component.java) :6290),java.awt.Container.processEvent(Container.java:2234),java.awt.Component.dispatchEventImpl(Component.java:4881),java.awt.Container.dispatchEventImpl(Container.java:2292),java .awt.Component.dispatchEvent(Component.java:4703),java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898),java.awt.LightweightDispatcher.processMouseEvent(Container.java:4542),java.awt.LightweightDispatcher。 dispatchEvent(Container.java:4462),java.awt.Container.dispatchEventImpl(Container.java:2278),java.awt.Window.dispatchEventImpl(Window.java:2750),java.awt.Compon ent.dispatchEvent(Component.java:4703),java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758),java.awt.EventQueue.access $ 500(EventQueue.java:97 ),java.awt.EventQueue $ 3.run (EventQueue.java:709),java.awt.EventQueue $ 3.run(EventQueue.java:703),java.security.AccessController.doPrivileged(Native Method),java.security.ProtectionDomain $ 1.doIntersectionPrivilege(ProtectionDomain.java:75 ),java.security.ProtectionDomain $ 1.doIntersectionPrivilege(ProtectionDomain.java:86),java.awt.EventQueue $ 4.run(EventQueue.java:731),java.awt.EventQueue $ 4.run(EventQueue.java:729), java.security.AccessController.doPrivileged(Native Method),java.security.ProtectionDomain $ 1.doIntersectionPrivilege(ProtectionDomain.java:75),java.awt.EventQueue.dispatchEvent(EventQueue.java:728),java.awt.EventDispatchThread.pumpOneEventForFilters (EventDispatchThread.java:201),java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116),java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatch Thread.java:105),java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101),java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93),java.awt.EventDispatchThread.run(EventDispatchThread.java:82 )]

     

6   net.ucanaccess.jdbc.UcanaccessSQLException:UCAExc ::: 3.0.2用户缺少未找到的权限或对象:MS       at net.ucanaccess.jdbc.UcanaccessStatement.executeQuery(UcanaccessStatement.java:211)       在sad.ReceptionView.okButtonMouseClicked(ReceptionView.java:111)       在sad.ReceptionView.access $ 000(ReceptionView.java:14)       在sad.ReceptionView $ 1.mouseClicked(ReceptionView.java:68)       at java.awt.Component.processMouseEvent(Component.java:6528)       在javax.swing.JComponent.processMouseEvent(JComponent.java:3324)       at java.awt.Component.processEvent(Component.java:6290)       at java.awt.Container.processEvent(Container.java:2234)       at java.awt.Component.dispatchEventImpl(Component.java:4881)       at java.awt.Container.dispatchEventImpl(Container.java:2292)       at java.awt.Component.dispatchEvent(Component.java:4703)       at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)       at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4542)       at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)       at java.awt.Container.dispatchEventImpl(Container.java:2278)       at java.awt.Window.dispatchEventImpl(Window.java:2750)       at java.awt.Component.dispatchEvent(Component.java:4703)       at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)       at java.awt.EventQueue.access $ 500(EventQueue.java:97)       at java.awt.EventQueue $ 3.run(EventQueue.java:709)       at java.awt.EventQueue $ 3.run(EventQueue.java:703)       at java.security.AccessController.doPrivileged(Native Method)       at java.security.ProtectionDomain $ 1.doIntersectionPrivilege(ProtectionDomain.java:75)       at java.security.ProtectionDomain $ 1.doIntersectionPrivilege(ProtectionDomain.java:86)       at java.awt.EventQueue $ 4.run(EventQueue.java:731)       at java.awt.EventQueue $ 4.run(EventQueue.java:729)       at java.security.AccessController.doPrivileged(Native Method)       at java.security.ProtectionDomain $ 1.doIntersectionPrivilege(ProtectionDomain.java:75)       at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)       at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)       at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)       at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)       at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)       at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)       at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)   引起:java.sql.SQLSyntaxErrorException:用户缺少未找到的权限或对象:MS       在org.hsqldb.jdbc.JDBCUtil.sqlException(未知来源)       在org.hsqldb.jdbc.JDBCUtil.sqlException(未知来源)       在org.hsqldb.jdbc.JDBCStatement.fetchResult(未知来源)       在org.hsqldb.jdbc.JDBCStatement.executeQuery(未知来源)       在net.ucanaccess.jdbc.UcanaccessStatement.executeQuery(UcanaccessStatement.java:208)       ......还有34个   引起:org.hsqldb.HsqlException:用户缺少未找到的权限或对象:MS       在org.hsqldb.error.Error.error(未知来源)       在org.hsqldb.error.Error.error(未知来源)       在org.hsqldb.ExpressionColumn.checkColumnsResolved(未知来源)       在org.hsqldb.QueryExpression.resolve(未知来源)       at org.hsqldb.ParserDQL.compileCursorSpecification(未知来源)       在org.hsqldb.ParserCommand.compilePart(未知来源)       在org.hsqldb.ParserCommand.compileStatements(未知来源)       at org.hsqldb.Session.executeDirectStatement(未知来源)       在org.hsqldb.Session.execute(未知来源)       ......还有37个

第#111行是cnnct.rs = cnnct.statement.executeQuery(getMR());

这是我的数据库数据: enter image description here

这是连接类:     公共类ConnectToDb {     连接con = null;     声明;     ResultSet rs;

public void connect () {
    try {
        String dir = System.getProperty("user.dir"), dbName = "SAD_Database.accdb";
        String db = "jdbc:ucanaccess://" + dir  + "/" + dbName;
        con = DriverManager.getConnection(db);
        statement = con.createStatement();
        //return con;
    }

    catch (Exception e) {
        new Error().textMessage.setText("Unable to connect to database.");
        new Error().setVisible(true);
        //System.out.println(e);
    }
} 

0 个答案:

没有答案