用java实现对数据库的基本操作

public class PreperadStatementUnpadateTest1 {
	//向customers表中添加一条记录
	@Test
	public void testInsert(){
		//1、获取连接
		Connection conn=null;
		PreparedStatement ps=null;
		try {
			conn = jdbcConnection.connection();
			ps = null;
			//2、创建失sql语句
			String sql="insert into customers(name,email,birth)values(?,?,?)";//?是占位符
			ps = conn.prepareStatement(sql);
			//3、填充占位符
			ps.setString(1, "nihao");
			ps.setString(2, "1234@qq.com");
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
			java.util.Date date=sdf.parse("1000-01-23");
			ps.setDate(3, new Date(date.getTime()));
			//4、执行操作
			ps.execute();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			//6、关闭资源
			jdbcConnection.CloseResource(conn, ps);
		}
	}
	@Test
	//修改customers表中一条记录
	public void testUpdata(){
		//1、获取连接
		Connection conn=null;
		PreparedStatement ps=null;
		try {
			conn = jdbcConnection.connection();
			//2、创建sql语句
			String sql="update customers set name=? where id=? ";
			ps = conn.prepareStatement(sql);
			//3、填充占位符
			ps.setString(1, "大雄");
			ps.setString(2, "35");
			//4、执行操作
			ps.execute();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			//6、关闭资源
			jdbcConnection.CloseResource(conn, ps);
		}
	}
	//查找customers表中一条记录
	@Test
	public void testSeek(){
		//1、连接数据库
		Connection conn=null;
		PreparedStatement ps=null;
		ResultSet resultset=null;
		try {
			conn = jdbcConnection.connection();
			//2、创建sql语句
			String sql="select *from customers where id=? and name=?";
			ps = conn.prepareStatement(sql);
			//3、填充占位符
			ps.setObject(1, 35);
			ps.setObject(2, "daxiong");
			//4、执行
			resultset=ps.executeQuery();
			//5、输出查询结果
			if(resultset.next()){
				int id=resultset.getInt(1);
				String name=resultset.getString(2);
				String email=resultset.getString(3);
				String birth=resultset.getString(4);
				System.out.println("id=" + id + ", name=" + name + ", email=" + email + ", birth=" + birth);
			}else{
				System.out.print("记录不存在!");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			//6、关闭资源
			jdbcConnection.CloseResource(conn, ps,resultset);
		}
	}
	@Test
	//删除customers表中一条记录
	public void testdelete(){
		//1、获取连接
		Connection conn=null;
		PreparedStatement ps=null;
		try {
			conn = jdbcConnection.connection();
			//2、创建sql语句,实例化PreparedStatement对象
			String sql="delete from customers where id=? ";
			ps = conn.prepareStatement(sql);
			//3、填充占位符
			ps.setInt(1, 35);
			//4、执行
			ps.execute();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			//5、关闭资源
			jdbcConnection.CloseResource(conn, ps);
		}
	}
}

对于数据库的连接关闭,封装在jdbcConnection类中,查看封装数据库的连接关闭操作_qq_46053741的博客-CSDN博客

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值