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

来源:互联网 发布:美国软件清理手机 编辑:程序博客网 时间:2024/06/16 21:05

一个简单的小测试案例:

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;@Testpublic 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);}}@Testpublic 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);}}@Testpublic 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);}}}


0 0
原创粉丝点击