0

java eclipse を使用して ms access データベースにクエリを実行すると、このエラーが発生します。

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

1 に答える 1

1

これが単なる学習演習である場合は、次のようなものを試してください

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

これが製品コード用である場合は、上記を使用しないでください。代わりに、「SQL インジェクション」攻撃について Web 検索を行い、「パラメータ化されたクエリ」について学び、それらを使用してください。

于 2013-04-12T10:19:59.120 に答える