个性化阅读
专注于IT技术分析

Java PreparedStatement接口

PreparedStatement接口是Statement的子接口。用于执行参数化查询。

让我们看一下参数化查询的示例:

String sql="insert into emp values(?, ?, ?)";

如你所见, 我们正在传递参数(?)作为值。它的值将通过调用PreparedStatement的setter方法来设置。

为什么要使用PreparedStatement?

提高性能:如果使用PreparedStatement接口, 则应用程序的性能将更快, 因为查询仅被编译一次。


如何获取PreparedStatement的实例?

Connection接口的prepareStatement()方法用于返回PreparedStatement的对象。句法:

public PreparedStatement prepareStatement(String query)throws SQLException{}

PreparedStatement接口的方法

下面给出了PreparedStatement接口的重要方法:

方法 描述
public void setInt(int paramIndex, int value) 将整数值设置为给定的参数索引。
public void setString(int paramIndex, String value) 将String值设置为给定的参数索引。
public void setFloat(int paramIndex, float value) 将float值设置为给定的参数索引。
public void setDouble(int paramIndex, double value) 将double值设置为给定的参数索引。
public int executeUpdate() 执行查询。它用于创建, 删除, 插入, 更新, 删除等。
public ResultSet executeQuery() 执行选择查询。它返回一个ResultSet实例。

插入记录的PreparedStatement接口的示例

首先创建表, 如下所示:

create table emp(id number(10), name varchar2(50));

现在, 通过以下代码在该表中插入记录:

import java.sql.*;
class InsertPrepared{
public static void main(String args[]){
try{
Class.forName("oracle.jdbc.driver.OracleDriver");

Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "system", "oracle");

PreparedStatement stmt=con.prepareStatement("insert into Emp values(?, ?)");
stmt.setInt(1, 101);//1 specifies the first parameter in the query
stmt.setString(2, "Ratan");

int i=stmt.executeUpdate();
System.out.println(i+" records inserted");

con.close();

}catch(Exception e){ System.out.println(e);}

}
}

更新记录的PreparedStatement接口示例

PreparedStatement stmt=con.prepareStatement("update emp set name=? where id=?");
stmt.setString(1, "Sonoo");//1 specifies the first parameter in the query i.e. name
stmt.setInt(2, 101);

int i=stmt.executeUpdate();
System.out.println(i+" records updated");

删除记录的PreparedStatement接口示例

PreparedStatement stmt=con.prepareStatement("delete from emp where id=?");
stmt.setInt(1, 101);

int i=stmt.executeUpdate();
System.out.println(i+" records deleted");

检索表记录的PreparedStatement接口示例

PreparedStatement stmt=con.prepareStatement("select * from emp");
ResultSet rs=stmt.executeQuery();
while(rs.next()){
System.out.println(rs.getInt(1)+" "+rs.getString(2));
}

在用户按下n之前插入记录的PreparedStatement示例

import java.sql.*;
import java.io.*;
class RS{
public static void main(String args[])throws Exception{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "system", "oracle");

PreparedStatement ps=con.prepareStatement("insert into emp130 values(?, ?, ?)");

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

do{
System.out.println("enter id:");
int id=Integer.parseInt(br.readLine());
System.out.println("enter name:");
String name=br.readLine();
System.out.println("enter salary:");
float salary=Float.parseFloat(br.readLine());

ps.setInt(1, id);
ps.setString(2, name);
ps.setFloat(3, salary);
int i=ps.executeUpdate();
System.out.println(i+" records affected");

System.out.println("Do you want to continue: y/n");
String s=br.readLine();
if(s.startsWith("n")){
break;
}
}while(true);

con.close();
}}
赞(0)
未经允许不得转载:srcmini » Java PreparedStatement接口

评论 抢沙发

评论前必须登录!