通过java

时间:2017-03-11 07:08:24

标签: java jdbc h2

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

}

我得到的例外情况: org.h2.jdbc.JdbcSQLException:SQL语句中的语法错误" INSERT INTO REGISTRATIONTABLE(?[*],?,?,?,?,?)&#34 ;;预期"标识符&#34 ;; SQL语句:插入到注册表中(?,?,?,?,?,?)[42001-193]

请帮忙。谢谢。

1 个答案:

答案 0 :(得分:2)

试试这个sql:

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

其中name,age,address,password,email,uuid是表字段名称,因此您可能需要重命名它们以适合您的表模式。