这篇文章主要介绍怎样通过JDBC Statement来向数据库表中插入记录。
通过调用s
tatement.execute()方法来执行sql语句,语法如下:
- stmt = conn.createStatement();
-
stmt.executeUpdate(sql);
代码如下:
- private static void insertRecord() throws SQLException{
-
Connection conn = null;
-
Statement stmt = null;
-
-
String sql = "INSERT INTO t_user(username,password) values"
-
+"('Dendi','password')";
-
-
try{
-
conn = getDBConnection();
-
stmt = conn.createStatement();
-
System.out.println(sql);
-
stmt.executeUpdate(sql);
-
System.out.println("A Record has been inserted!");
-
}catch(SQLException e){
-
System.out.println(e.getMessage());
-
}finally{
-
if(stmt!=null){
-
stmt.close();
-
}
-
if(conn!=null){
-
conn.close();
-
}
-
}
-
}
阅读(1154) | 评论(0) | 转发(0) |