码迷,mamicode.com
首页 > 其他好文 > 详细

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

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

标签:jdbc

一个简单的小测试案例:

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增加&删除&更改

标签:jdbc

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!