简体   繁体   中英

Getting SQL exception while entering data into H2 database through java

public class RegisterDao {

private static final String DB_DRIVER = "org.h2.Driver";
private static final String DB_CONNECTION = "jdbc:h2:~/test";
private static final String DB_USER = "admin";
private static final String DB_PASSWORD = "admin";

Connection dbConnection = null;
PreparedStatement pstmt = null;
public void forRegister(String name,int age,String address,String password,String email,String uuid){
    try {
        Class.forName(DB_DRIVER);
        System.out.println("Connecting to a selected database...");
    } catch (ClassNotFoundException e) {
        System.out.println(e.getMessage());
    }
    try {
        dbConnection = DriverManager.getConnection(DB_CONNECTION, DB_USER,
                DB_PASSWORD);
    System.out.println("Connected database successfully...");
    //Statement stat = dbConnection.createStatement();
    //ResultSet rs;
    if (dbConnection != null){
        System.out.println("starting entering data");
        pstmt=dbConnection.prepareStatement("insert into registrationtable (?,?,?,?,?,?)");

        pstmt.setString(1, name);
        pstmt.setInt(2, age);
        pstmt.setString(3, address);
        pstmt.setString(4, password);
        pstmt.setString(5, email);
        pstmt.setString(6, uuid);

        pstmt.executeUpdate();
        System.out.println("details are added");
    }
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }  finally{
        //finally block used to close resources
        try{
           if(dbConnection!=null)
               dbConnection.close();
        }catch(SQLException se){
           se.printStackTrace();
        }//end finally try
     }//end try

}

Exception that I am getting : org.h2.jdbc.JdbcSQLException: Syntax error in SQL statement "INSERT INTO REGISTRATIONTABLE (?[*],?,?,?,?,?) "; expected "identifier"; SQL statement: insert into registrationtable (?,?,?,?,?,?) [42001-193]

Please help.Thanks in advance.

Try this sql:

 insert into registrationtable (name, age, address, password, email, uuid) values (?, ?, ?, ?, ?, ?)"

where name, age, address, password, email, uuid are the table field names, so probably you will have to rename them to fit your table schema.

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