通过java将数据插入H2数据库

时间:2017-05-12 00:02:48

标签: java database h2

数据不会按原样插入数据库。我相信我已经按照文档中的说明做了适当的连接。有人可以帮我改进我的代码吗?

    import java.sql.*;

    public class DB_Connection {

    private final String jdbcdriver = "org.h2.Driver";
    private final String db_url = "jdbc:h2:~/RetailerDB";
    private Connection conn = null;
    private Statement stmt = null;

    private Connection Connect() throws SQLException {
        try {
            Class.forName(jdbcdriver);
            conn = DriverManager.getConnection(db_url,"","");
            return conn;
        } catch (ClassNotFoundException ex) {
            return null;
        }
    }

    /**
     *
     * @param emp_id
     * @param username
     * @param first_name
     * @param last_name
     * @param password
     * @param position
     */
    public void Register(String emp_id, String username, String first_name, String last_name,String password, String position){
        try {
            conn = Connect();
            String sql = "INSERT into EMPLOYEES values(?,?,?,?,?,?,);";
            stmt = conn.createStatement();
            stmt.executeUpdate(sql);
            conn.commit();
            conn.close();
        } catch (SQLException ex) {
        }
    }
}

0 个答案:

没有答案