黑马day10 使用PrepareStatement增加&删除&更改
2015-06-29 21:41
281 查看
一个简单的小测试案例:
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); } } }
相关文章推荐
- create mystic by Django
- [leetcode] Spiral Matrix II
- 最全最强解析:支付宝钱包系统架构内部剖析(架构图)
- 113.根据要求合并两个整数
- iOS开发入门实例
- Java基础总结之反射
- 日志分析系统所用知识
- 二叉树题目集合
- c语言结构体指针初始化
- vi编辑器的常用命令
- AJAX异步检查,检查用户名是否存在
- 互联网行业的奇葩问答,瞬间秒碎你的节操!
- [leetcode][math] Basic Calculator II
- Android AsyncTaskLoader需要注意的问题
- nc默认枚举,获取前台传过来的数据块
- 谈谈对AOP的认识三——优化AOP,进一步解耦
- Xamarin.Android开发实践(一)
- (译)Asynchronous programming and Threading in C# (.NET 4.5)
- [LeetCode] Add Two Numbers
- 数组赋值于双指针