大家好,又见面了,我是你们的朋友全栈君。
Statement 和 PreparedStatement之间的关系和区别. 关系:PreparedStatement继承自Statement,都是接口 区别:PreparedStatement可以使用占位符,是预编译的,批处理比Statement效率高 详解: 1、PreparedStatement:表示预编译的 SQL 语句的对象。 接口:public interface PreparedStatement extends Statement之间的继承关系 SQL 语句被预编译并存储在 PreparedStatement 对象中。然后可以使用此对象多次高效地执行该语句。 注:用于设置 IN 参数值的设置方法(setShort、setString 等等)必须指定与输入参数的已定义 SQL 类型兼容的类型。例如,如果 IN 参数具有 SQL 类型 INTEGER,那么应该使用 setInt 方法,问号的位置也是应该注意的,因为第一个问好的位置为1,第二个问号的位置为2.以此类推。 如果需要任意参数类型转换,使用 setObject 方法时应该将目标 SQL 类型作为其参数。 在以下设置参数的示例中,con 表示一个活动连接: PreparedStatement pstmt = con.prepareStatement(“UPDATE EMPLOYEES SALARY = ? WHERE ID = ?”); pstmt.setBigDecimal(1, 1533.00) pstmt.setInt(2, 1102) pstmt.execute()//注意提交时这里不能再有sql语句,不同于Statment
演示代码:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
public class PreparedStatementTest {
public static void main(String[] args) {
test_autoCommit();
}
public static void test_autoCommit()
{
String driver="oracle.jdbc.driver.OracleDriver";
String url="jdbc:oracle:thin:@127.0.0.1:1521:orcl";
String user="admin";
String password="admin";
Connection conn=null;
PreparedStatement ps=null;
try {
//1、注册驱动
Class.forName(driver);
//2、获取连接
conn= DriverManager.getConnection(url, user, password);
//System.out.println(conn);
//3、创建prepareStatement对象
String sql="insert into lover values(?,?,?)";
ps=conn.prepareStatement(sql);
//4、执行sql语句
ps.setInt(1,21);//代表设置给第一个?号位置的值为Int类型的21
ps.setString(2,"suwu150");//代表设置给第二个?号位置的值为String类型的suwu150
java.util.Date utilDate=new java.util.Date();//进行类型转换,由util类型的date转化为sql类型的
ps.setDate(3, new java.sql.Date(utilDate.getTime()));
//ps.execute();//执行
System.out.println(ps.execute());//执行表输出返回的结果,结果为false,因为没有返回的结果集
//5、处理结果集
} catch (Exception e) {
e.printStackTrace();
}
finally{
//6、关闭资源
try {
if(ps!=null)ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
if(conn!=null)conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
插入之后的结果
2、Statement:用于执行静态 SQL 语句并返回它所生成结果的对象。 接口:public interface Statement extends Wrapper 在默认情况下,同一时间每个 Statement 对象只能打开一个 ResultSet 对象。因此,如果读取一个 ResultSet 对象与另一个交叉,则这两个对象必须是由不同的 Statement 对象生成的。如果存在某个语句的打开的当前 ResultSet 对象,则 Statement 接口中的所有执行方法都会隐式关闭它。 如以下操作:创建statement对象 Statement stat=conn.createStatement(); String sql=”insert into lover values(6,’suxingxing’,to_date(’21-9-2016′,’dd-mm-yyyy’))”; stat.execute(sql);//这里提交时应该有sql语句,不同于PreparedStatment 来看一下实际使用吧:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class StatementTest {
public static void main(String[] args) {
test_autoCommit();
}
public static void test_autoCommit()
{
String driver="oracle.jdbc.driver.OracleDriver";
String url="jdbc:oracle:thin:@127.0.0.1:1521:orcl";
String user="admin";
String password="admin";
Connection conn=null;
Statement stat=null;
try {
//1、注册驱动
Class.forName(driver);
//2、获取连接
conn= DriverManager.getConnection(url, user, password);
conn.setAutoCommit(false);
//System.out.println(conn);
//3、创建statement对象
stat=conn.createStatement();
//4、执行sql语句
String sql="insert into lover values(22,'suxingxing',to_date('21-9-2016','dd-mm-yyyy'))"; //注意格式
// stat.execute(sql);
System.out.println(stat.execute(sql)); //返回值为false,因为同样没有ResultSet返回集
conn.commit();
//5、处理结果集
} catch (Exception e) {
e.printStackTrace();
try {
conn.rollback();
} catch (SQLException e1) {
e1.printStackTrace();
}
}
finally{
//6、关闭资源
try {
if(stat!=null)stat.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
if(conn!=null)conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
插入之后的结果:
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/130449.html原文链接:https://javaforall.cn
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有