JDBC副本

来源:互联网 发布:国民党吴敦义 知乎 编辑:程序博客网 时间:2024/04/20 10:26

===========================================添加的方法====================================================
    

//  数据库连接
  Connection conn=null;
//  sql语句的转载器
  PreparedStatement pstmt=null;
  String sql="insert into student"+"(id,name,age,sex,createTime)"+"values(?,?,?,?,?)";
  
  conn=DBManager.getConnection();
  try {
   pstmt=conn.prepareStatement(sql);
//   设计数据库的提交方式为手动
   conn.setAutoCommit(false);
//   第一个参数代表了石第几个问号
   pstmt.setString(1, stu.getId());
   pstmt.setString(2, stu.getName());
   pstmt.setInt(3, stu.getAge());
   pstmt.setString(4, stu.getSex());
   pstmt.setTimestamp(5, new java.sql.Timestamp(stu.getCreateTime().getTime()));
   pstmt.execute();     //开始像数据库中插入数据
   conn.commit();
  } catch (SQLException e) {
   e.printStackTrace();
  }
  finally{
   try {
    pstmt.close();
    conn.close();
   } catch (SQLException e) {
    e.printStackTrace();
   }
  }
  
 

==========================================删除的方法=========================================================


    

//  数据库连接
  Connection conn=null;
//  sql语句的转载器
  PreparedStatement pstmt=null;
  String sql="delete from student where id=?";
  
  conn=DBManager.getConnection();
  try {
   pstmt=conn.prepareStatement(sql);
//   设计数据库的提交方式为手动
   conn.setAutoCommit(false);
   pstmt.setString(1, id);
   pstmt.execute();     //开始像数据库中插入数据
   conn.commit();
  } catch (SQLException e) {
   e.printStackTrace();
  }
  finally{
   try {
    pstmt.close();
    conn.close();
   } catch (SQLException e) {
    e.printStackTrace();
   }
  }
  
 

=============================================修改的方法======================================================
 
    
 
//  数据库连接
  Connection conn=null;
//  sql语句的转载器
  PreparedStatement pstmt=null;
  String sql="update student set name=?,age=?,sex=? where id=?";
  
  conn=DBManager.getConnection();
  try {
   pstmt=conn.prepareStatement(sql);
//   设计数据库的提交方式为手动
   conn.setAutoCommit(false);
//   第一个参数代表了石第几个问号
   pstmt.setString(4, stu.getId());
   pstmt.setString(1, stu.getName());
   pstmt.setInt(2, stu.getAge());
   pstmt.setString(3, stu.getSex());
   pstmt.execute();     //开始像数据库中插入数据
   conn.commit();
  } catch (SQLException e) {
   e.printStackTrace();
  }
  finally{
   try {
    pstmt.close();
    conn.close();
   } catch (SQLException e) {
    e.printStackTrace();
   }
  }
  
 
 

=============================================查询一条记录的方法======================================================
 
   

  Student stu=null;
//  结果集合(结果集)
  ResultSet rs=null;
//  数据库连接
  Connection conn=null;
//  sql语句的转载器
  PreparedStatement pstmt=null;
  String sql="select * from student where id=?";
  
  conn=DBManager.getConnection();
  try {
   pstmt=conn.prepareStatement(sql);
//   第一个参数代表了石第几个问号
   pstmt.setString(1, id);
   System.out.println(pstmt);
   rs=pstmt.executeQuery();     //开始像数据库中插入数据
   while (rs.next()) {
       stu=new Student();
       stu.setName(rs.getString("name"));
       stu.setAge(rs.getInt("age"));
       stu.setSex(rs.getString("sex"));
       stu.setCreateTime(rs.getDate("createTime"));
   }
//    事物的提交
//   conn.commit();
  } catch (SQLException e) {
   e.printStackTrace();
  }
  finally{
   try {
    rs.close();
    pstmt.close();
    conn.close();
   } catch (SQLException e) {
    e.printStackTrace();
   }
  }
  return stu;
  
 

==============================================查询多条记录的方法===================================================== 
 
   

  //学生信息的集合
  ArrayList<Student> listStudent =new ArrayList<Student>();
  Student stu=null;
//  结果集合(结果集)
  ResultSet rs=null;
//  数据库连接
  Connection conn=null;
//  sql语句的转载器
  PreparedStatement pstmt=null;
  String sql="select * from student where sex=? ";
  
  conn=DBManager.getConnection();
  try {
   pstmt=conn.prepareStatement(sql);
//   第一个参数代表了石第几个问号
   pstmt.setString(1, sex);
   
   rs=pstmt.executeQuery();     //开始像数据库中插入数据
   while (rs.next()) {
       stu=new Student();
       stu.setName(rs.getString("name"));
       stu.setAge(rs.getInt("age"));
       stu.setSex(rs.getString("sex"));
       stu.setCreateTime(rs.getDate("createTime"));
   
   listStudent.add(stu);
   }
//    事物的提交
//   conn.commit();
  } catch (SQLException e) {
   e.printStackTrace();
  }
  finally{
   try {
    rs.close();
    pstmt.close();
    conn.close();
   } catch (SQLException e) {
    e.printStackTrace();
   }
  }
  return listStudent;
  
 
 

==============================================查询多条记录的方法2=====================================================

   

  //学生信息的集合
  ArrayList<Student> listStudent =new ArrayList<Student>();
  Student stu=null;
//  结果集合(结果集)
  ResultSet rs=null;
//  数据库连接
  Connection conn=null;
//  sql语句的转载器
  PreparedStatement pstmt=null;
  String sql="select * from student  "+whereString;
  
  conn=DBManager.getConnection();
  try {
   pstmt=conn.prepareStatement(sql);
//   
   
   rs=pstmt.executeQuery();     //开始像数据库中插入数据
   while (rs.next()) {
       stu=new Student();
       stu.setName(rs.getString("name"));
       stu.setAge(rs.getInt("age"));
       stu.setSex(rs.getString("sex"));
       stu.setCreateTime(rs.getDate("createTime"));
   
   listStudent.add(stu);
   }
//    事物的提交
//   conn.commit();
  } catch (SQLException e) {
   e.printStackTrace();
  }
  finally{
   try {
    rs.close();
    pstmt.close();
    conn.close();
   } catch (SQLException e) {
    e.printStackTrace();
   }
  }
  return listStudent;
  
 
 

===================================================================================================