0

I'm trying to write some data to an MS-Access database but I keep getting "unknown source" error. Stack trace below:

java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Syntax error in INSERT INTO statement.
    at sun.jdbc.odbc.JdbcOdbc.createSQLException(Unknown Source)
    at sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source)
    at sun.jdbc.odbc.JdbcOdbc.SQLExecute(Unknown Source)
    at sun.jdbc.odbc.JdbcOdbcPreparedStatement.execute(Unknown Source)
    at sun.jdbc.odbc.JdbcOdbcPreparedStatement.executeUpdate(Unknown Source)
    at SampleDB.main(SampleDB.java:15)

Code sample below:

import java.sql.*;

public class SampleDB {
    public static void main (String[]args) {
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 
        Connection con = DriverManager.getConnection("jdbc:odbc:sample_db"); 
            PreparedStatement ps = con.prepareStatement("INSERT INTO Data_Sample (FirstName,Surname,Address) (?,?,?)");
            ps.setString(1,"John");
            ps.setString(2,"Smith");
        ps.setString(3,"New York");

            ps.executeUpdate();
            con.close();    
            System.out.println("inserted");
        } catch(Exception e) {
            e.printStackTrace();
        }

    }
}

Would appreciate any help in debugging the above.

2 Answers 2

3

your insert query is wrong

INSERT INTO Data_Sample (FirstName,Surname,Address) (?,?,?)

it should be

INSERT INTO Data_Sample(FirstName,Surname,Address) values(?,?,?)
Sign up to request clarification or add additional context in comments.

1 Comment

Just realised that now. Guess I'm just tired and overlooking these little things.
0

The issue is the format of the SQL statement is malformed.

should it be like

 PreparedStatement ps = con.prepareStatement("INSERT INTO Data_Sample (FirstName,Surname,Address) VALUES (?,?,?)");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.