Statement 和 PreparedStatement 的区别
当多次执行Statement的时候,PrepareedStatement对象会降低运行时间,加快了访问数据库的速度,
好处是不必重复SQL语句的句法,只需改变其中的变量值即可更改 PreparedStatement 语句
Java 操作sql server数据表:
在java中如何用java程序来控制对数据库表的创建、删除、备份和恢复工作
在java中使用Statement 和PreparedStatement语句来对数据库执行SQL语句
1、 执行静态的SQL语句,通常用Statement实力实现
2、 执行动态的SQL语句,通常用PreparedStatement语句实现
3、 执行数据库的存储过程通常通过CallableStatement实现
具体的实现方式,
定义变量:
Connection con = null;
Statement st = null;
PreparedStatement ps = null;
ResultSet rs = null;
第一种用Statement 执行SQL语句
Try{
con =DriverManager.getConnection(url,dbName,passWord);
st = con. createStatement();
st.excuteQuery(sql);//返回的是ResultSet对象//这个语句执行查询
st.excuteUpdate(sql);//返回sql语句执行了几次,此语句执行Insert update delete语句
}catch(Exception e){
e.printStackTrace();
}
//如果使用PreparedStatement时只讲上面的Statement改成PreparedStatement即可