2013-04-12 47 views
0

我在使用java eclipse查詢ms訪問數據庫時發生此錯誤。什麼是從ms訪問jdbcodbc查詢數據庫的正確語法

java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Undefined function txtIcNo.getText' in expression. 
    at sun.jdbc.odbc.JdbcOdbc.createSQLException(Unknown Source) 
    at sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source) 
    at sun.jdbc.odbc.JdbcOdbc.SQLExecDirect(Unknown Source) 
at sun.jdbc.odbc.JdbcOdbcStatement.execute(Unknown Source) 
    at sun.jdbc.odbc.JdbcOdbcStatement.executeQuery(Unknown Source) 
    at GetDataFromDB.retrieveDB(GetDataFromDB.java:103) 
    at GetDataFromDB.actionPerformed(GetDataFromDB.java:79) 
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) 
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) 
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) 
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source) 
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source) 
    at java.awt.Component.processMouseEvent(Unknown Source) 
    at javax.swing.JComponent.processMouseEvent(Unknown Source) 
    at java.awt.Component.processEvent(Unknown Source) 
    at java.awt.Container.processEvent(Unknown Source) 
    at java.awt.Component.dispatchEventImpl(Unknown Source) 
    at java.awt.Container.dispatchEventImpl(Unknown Source) 
    at java.awt.Component.dispatchEvent(Unknown Source) 
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) 
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) 
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) 
    at java.awt.Container.dispatchEventImpl(Unknown Source) 
    at java.awt.Window.dispatchEventImpl(Unknown Source) 
    at java.awt.Component.dispatchEvent(Unknown Source) 
    at java.awt.EventQueue.dispatchEvent(Unknown Source) 
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
    at java.awt.EventDispatchThread.run(Unknown Source)" 

我的代碼如下

public void retrieveDB() throws SQLException,ClassNotFoundException{ 
    //load JDBC Driver 
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 
    Connection con = DriverManager.getConnection("jdbc:odbc:PatientDB"); 
    String query = "SELECT * FROM PatientDB WHERE IC_No = txtIcNo.getText()"; 
    Statement stmt = con.createStatement(); 
    ResultSet rs = stmt.executeQuery(query); 
    con.commit(); 
    while(rs.next()){ 
    if("txtIcNo.getText()"!=null){ 

    txtIcNo.setText(rs.getObject(1).toString());  
    txtName.setText(rs.getObject(2).toString()); 
    } 
    else{ 
    System.out.println("Bye bye"); 
    } 
    } 
    con.close(); 
    } 
+0

PatientDB是一張表嗎?此外,不應該將txtIcNo.getText()附加到您的字符串?如果不是,對象txtIcNo在哪裏?讓我知道如果我把你的問題弄錯了。 – Sid

回答

1

如果這不只是一個學習鍛鍊嘗試像

String query = "SELECT * FROM PatientDB WHERE IC_No = '" + txtIcNo.getText() + "'"; 

如果這是生產代碼然後做使用以上。相反,對「SQL注入」攻擊進行一些網絡搜索,然後瞭解「參數化查詢」並使用它們。

相關問題