首页 > 其他 > 详细

黑马day10 使用PrepareStatement增加&删除&更改

时间:2015-06-30 10:39:30      阅读:219      评论:0      收藏:0      [点我收藏+]

一个简单的小测试案例:

package cn.itheima.jdbc;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import org.junit.Test;

import cn.itheima.utils.JDBCUtils;

public class JDBCDemo6 {
	Connection con = null;
	PreparedStatement ps = null;
	ResultSet rs = null;

	@Test
	public void update() {
		try {
			con=JDBCUtils.getConnection();
			ps=con.prepareStatement("update user set name='程崇树' where name=?");
			ps.setString(1, "李卫康");
			ps.executeUpdate();
			
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException();
		} finally {
			JDBCUtils.closeResource(rs, ps, con);
		}
	}

	@Test
	public void delete() {
		try {
			con=JDBCUtils.getConnection();
			ps=con.prepareStatement("delete from user where name=?");
			ps.setString(1, "程崇树");
			ps.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException();
		} finally {
			JDBCUtils.closeResource(rs, ps, con);
		}
	}

	@Test
	public void add() {
		try {
			con=JDBCUtils.getConnection();
			ps=con.prepareStatement("insert into user values(2,?,?,?)");
			ps.setString(1, "李卫康");
			ps.setByte(2, (byte)1);
			ps.setDate(3, new Date(1992, 3, 4));
			ps.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException();
		} finally {
			JDBCUtils.closeResource(rs, ps, con);
		}
}
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

黑马day10 使用PrepareStatement增加&删除&更改

原文:http://blog.csdn.net/u014010769/article/details/46687497

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!