简体   繁体   中英

what is the correct syntax to query database from ms access jdbcodbc in java

I have this error when querying the ms access database using java eclipse.

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

My code is as below

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

If this is just a learning exercise then try something like

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

If this is for production code then do not use the above. Instead, do some Web searches on "SQL injection" attacks, then learn about "Parameterized queries" and use those.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM