线程池连接,向数据库添加数据

Stement与PreparedStatement:
1:数据添加成功返回false

		Connection connection = dataSource.getConnection();
		Stement statement = connection.createStatement();
		 String sql ="INSERT INTO sys_notice VALUES(); ";//数据库执行语句
		 //这样的字符串拼接不安全,不推荐
        boolean b = statement.execute(sql);
        System.out.println(b);//b = false但是插入数据是成功的
        //释放资源
        statement.close();
        connection.close();

Stement:
true if the first result is a Result Set object; false if it is an update count or there are no results
如果返回的是一个结果是结果集对象,则为true;如果是更新计数或没有结果,则为false

2:预编译,安全

		Connection connection = dataSource.getConnection();
        String sql="insert into sys_db(title,content values (?,?)";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1,"通知");
        preparedStatement.setString(2,"内容");
		preparedStatement.execute();
		
        preparedStatement.close();//释放资源
        connection.close();//将连接返回池中

PreparedStatement:
他的方法针对处理从预编译中受益的参数化SQL语句进行了优化。如果驱动程序支持预编译,那么方法prepareStatement会将语句发送到数据库进行预编译。某些驱动程序可能不支持预编译。在这种情况下,在执行PreparedStatement对象之前,语句可能不会发送到数据库。这对用户没有直接影响;但是,它确实会影响哪些方法抛出某些SQLException对象。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值