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;
===================================================================================================
- JDBC副本
- JDBC副本
- 副本系统
- 线程副本
- 副本集
- 副本dp
- 克隆-浅表副本与深层副本
- C#中浅表副本与深表副本
- 教学计划副本解决方案介绍
- 保存指针?还是副本?
- 副本推荐等级
- 用脚本实现副本
- 克隆对象副本
- Cassandra数据副本策略
- mongodb 副本集+分片
- mongodb 系统 副本集
- mongoDB 副本集配置
- 增加控制文件副本
- js xml操作类.txt
- 自已写的滑动门
- 郁闷啊
- 斐波那契数列实现的两种实现(表查询、向量)
- 最大公约数---欧几里得实现
- JDBC副本
- Servelt
- java连数据库
- DBManager
- method属性
- 最小公倍数--算法
- C语言编程的的四大绝招
- 素数判断算法-----应用
- jdk1.5新特性