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对象。