10月22日学习日志

今天学习了Java对数据库操作的增加数据方法。

代码如:

public static int insert(Student student) {
   Connection conn = getConn();
   int i = 0;
   String sql = "insert into students (Name,Sex,Age) values(?,?,?)";
   PreparedStatement pstmt;
   try {
       pstmt = (PreparedStatement) conn.prepareStatement(sql);
       pstmt.setString(1, student.getName());
       pstmt.setString(2, student.getSex());
       pstmt.setString(3, student.getAge());
       i = pstmt.executeUpdate();
       pstmt.close();
       conn.close();
   } catch (SQLException e) {
       e.printStackTrace();
   }
   return i;
}

 

posted @ 2020-10-22 14:50  张笑天  阅读(43)  评论(0编辑  收藏  举报