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;
}
原文地址:https://www.cnblogs.com/20193925zxt/p/14159133.html