JDBC学习(二)

JDBC学习(二)

在项目中我们一般不使用Statement来发送SQL语句到数据库,而是使用PreparedStatement预编译的 SQL 语句的对象。

 

接口 PreparedStatement

 

public interface PreparedStatementextends Statement表示预编译的 SQL 语句的对象。

SQL 语句被预编译并存储在 PreparedStatement 对象中。然后可以使用此对象多次高效地执行该语句。

注:用于设置 IN 参数值的设置方法(setShortsetString 等等)必须指定与输入参数的已定义 SQL 类型兼容的类型。例如,如果 IN 参数具有 SQL 类型 INTEGER,那么应该使用 setInt 方法。

如果需要任意参数类型转换,使用 setObject 方法时应该将目标 SQL 类型作为其参数。

在以下设置参数的示例中,con 表示一个活动连接:

   PreparedStatement pstmt = con.prepareStatement("UPDATE EMPLOYEES

                                    SET SALARY = ? WHERE ID = ?");

   pstmt.setBigDecimal(1, 153833.00)

   pstmt.setInt(2, 110592)

 

//使用PreparedStatement预处理SQL语句添加数据

public class Demo1 {

private static DbUtil dbUtil = new DbUtil(); //封装好的数据库链接类

 

//添加数据

private static int addBook(Book book)throws Exception{

Connection con = dbUtil.getConnection();  //获取链接对象

String sql ="insert into t_book values(null,?,?,?,?)"; //SQL预编译语句

PreparedStatement pre = con.prepareStatement(sql); //获取预编译SQL语句对象

pre.setString(1, book.getBookName()); //写入字符串对象

pre.setFloat(2, book.getPrice()); //写入Float对象

pre.setString(3, book.getAuthor()); //写入字符串对象

pre.setInt(4, book.getBookTyple()); //写入int对象

int result = pre.executeUpdate(); //判断是否提交SQL语句并更新成功,成功返回1

dbUtil.colse(pre, con); //清除链接,关闭接口

return result;

}

public static void main(String[] args)throws Exception{

Book book = new Book("第一行代码", 59.99f, "郭森", 3);

int result = addBook(book);

if(result==1){

System.out.println("添加成功!");

}else{

System.out.println("添加失败!");

}

}

}

 

//使用PreparedStatement预处理SQL语句更新数据

public class Demo2 {

private static DbUtil dbUtil = new DbUtil();

//更新数据

private static int updateBook(Book book)throws Exception{

Connection con = dbUtil.getConnection();

String sql ="update t_book set bookName=?,price=?,author=?,bookTypeId=? where id=?";

PreparedStatement pre = con.prepareStatement(sql);

pre.setString(1, book.getBookName());

pre.setFloat(2, book.getPrice());

pre.setString(3, book.getAuthor());

pre.setInt(4, book.getBookTyple());

pre.setInt(5, book.getId());

int result = pre.executeUpdate();

dbUtil.colse(pre, con);

return result;

}

public static void main(String[] args)throws Exception{

Book book = new Book(4,"Java编程思想", 69.99f, "埃克尔", 4);

int result = updateBook(book);

if(result==1){

System.out.println("更新成功!");

}else{

System.out.println("更新失败!");

}

}

}

 

//使用PreparedStatement预处理SQL语句删除数据

public class Demo3 {

private static DbUtil dbUtil = new DbUtil();

//删除数据

private static int deleteBook(int id)throws Exception{

Connection con = dbUtil.getConnection();

String sql ="delete from t_book where id= ?";

PreparedStatement pre = con.prepareStatement(sql);

pre.setInt(1, id);

int result = pre.executeUpdate();

dbUtil.colse(pre, con);

return result;

}

public static void main(String[] args)throws Exception{

int result = deleteBook(1);

if(result==1){

System.out.println("删除成功!");

}else{

System.out.println("删除失败!");

}

}

}

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值