문제

I'm querying an MS Access database and getting the following error:

java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Data type mismatch in criteria expression.

The relevant code:

private SimpleStringProperty getProduct(String bc) {
    Connection conn;
    String dbDriver;
    Statement st;
    ResultSet rs;
    SimpleStringProperty prod = new SimpleStringProperty();
    System.out.println("BC sent to db is: " + bc.toString());

    try {
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        dbDriver = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};"
                + "DBQ=Inventario.mdb;";
        conn = DriverManager.getConnection(dbDriver, "", "");
        st = conn.createStatement();
        String getProduct = "SELECT * FROM Articulos "
                + "WHERE BarCode = " + bc; // .toString();

Where bc is a parameter obtained from a TextField box as a string, passed to this method as a string and the BarCode field in MS Access is of type Text.

As an aside, I am obviously working with a bar code, which is a number, but some of my bar codes have leading 0s, which is why I have it as text. Is there a way to work with bar codes of varying lengths and varying amounts of significant leading zeros as anything other than text/string? As you can see in my code, I even tried explicitly sending bc.toString() from JavaFX to MS Access, but I keep getting the data type mismatch error.

Final note: at various points I have placed System.out.println() to see what data is being passed, and at all points it appears correct. I have even taken out the criteria clause and just printed my result set, and all the proper data is coming back from Access.

도움이 되었습니까?

해결책

I suppose that your querystate ment is wrong... You could try to sourround your bc with '

String getProduct = "SELECT * FROM Articulos "
            + "WHERE BarCode = '" + bc+"'";

Try this tutorial http://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html

If you have excessive use of Databases i also would recommend you JPA.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top