存储过程是子例程,是SQL语句的一部分,存储在SQL目录中。所有可以访问关系数据库的应用程序(Java,Python,PHP等)都可以访问存储过程。
存储过程包含IN和OUT参数或两者。如果您使用SELECT语句,它们可能会返回结果集。存储过程可以返回多个结果集。
您可以使用CallableStatement调用现有的存储过程。Connection接口的prepareCall()方法接受字符串格式的过程调用,并返回可调用的语句对象。
CallableStatement cstmt = con.prepareCall("{call sampleProcedure()}");
使用executeQuery()方法执行上面创建的可调用语句,这将返回结果集对象。
//执行CallableStatement- ResultSet rs1 = cstmt.executeQuery();
如果此过程返回更多结果集对象,则使用cstmt.getMoreResults()方法移至下一个结果集。
然后,使用CallableStatement接口的getResultSet()方法检索下一个结果集。
ResultSet rs2 = cstmt.getResultSet();
假设我们在数据库中有一个名为cricketers_data的表,其内容如下:
+----------------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------------+--------------+------+-----+---------+-------+ | First_Name | varchar(255) | YES | | NULL | | | Last_Name | varchar(255) | YES | | NULL | | | Year_Of_Birth | date | YES | | NULL | | | Place_Of_Birth | varchar(255) | YES | | NULL | | | Country | varchar(255) | YES | | NULL | | +----------------+--------------+------+-----+---------+-------+
还有一个名为dispatch_data的表,其内容如下:
+------------------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +------------------+--------------+------+-----+---------+-------+ | Product_Name | varchar(255) | YES | | NULL | | | Name_Of_Customer | varchar(255) | YES | | NULL | | | Dispatch_Date | date | YES | | NULL | | | Location | varchar(255) | YES | | NULL | | +------------------+--------------+------+-----+---------+-------+
我们创建了一个名为sampleProcedure的过程,该过程检索这两个表的内容,如下所示:
mysql> DELIMITER // ; mysql> Create procedure sampleProcedure () BEGIN Select * from cricketers_data; Select * from dispatch_data; END// mysql> DELIMITER ;
以下JDBC示例建立与数据库的连接,调用名为sampleProcedure的过程,检索其返回的结果集,并打印内容。
import java.sql.CallableStatement; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; public class MultipleResultSetsStoredProcedure { public static void main(String args[]) throws SQLException { //注册驱动程序 DriverManager.registerDriver(new com.mysql.jdbc.Driver()); //获得连接 String mysqlUrl = "jdbc:mysql://localhost/mydatabase"; Connection con = DriverManager.getConnection(mysqlUrl, "root", "password"); System.out.println("Connection established......"); //准备一个CallableStatement来调用retrieveData过程 CallableStatement cstmt = con.prepareCall("{call sampleProcedure()}"); //执行CallableStatement- ResultSet rs1 = cstmt.executeQuery(); //显示结果 System.out.println("Contents of the first result-set"); while(rs1.next()) { System.out.print("First Name: "+rs1.getString("First_Name")+", "); System.out.print("Last Name: "+rs1.getString("Last_Name")+", "); System.out.print("Year of Birth: "+rs1.getDate("Year_Of_Birth")+", "); System.out.print("Place of Birth: "+rs1.getString("Place_Of_Birth")+", "); System.out.print("Country: "+rs1.getString("Country")); System.out.println(); } System.out.println(" "); cstmt.getMoreResults(); System.out.println("Contents of the second result-set"); ResultSet rs2 = cstmt.getResultSet(); while(rs2.next()) { System.out.print("Product Name: "+rs2.getString("Product_Name")+", "); System.out.print("Name of Customer: "+rs2.getString("Name_Of_Customer")+", "); System.out.print("Dispatch Date: "+rs2.getDate("Dispatch_Date")+", "); System.out.print("Location: "+rs2.getString("Location")); System.out.println(); } } }
输出结果
Connection established...... Contents of the first result-set First Name: Shikhar, Last Name: Dhawan, Year of Birth: 1981-12-05, Place of Birth: Delhi, Country: India First Name: Jonathan, Last Name: Trott, Year of Birth: 1981-04-22, Place of Birth: CapeTown, Country: SouthAfrica First Name: Lumara, Last Name: Sangakkara, Year of Birth: 1977-10-27, Place of Birth: Matale, Country: Srilanka First Name: Virat, Last Name: Kohli, Year of Birth: 1988-11-05, Place of Birth: Delhi, Country: India First Name: Rohit, Last Name: Sharma, Year of Birth: 1987-04-30, Place of Birth: Nagpur, Country: India Contents of the second result-set Product Name: KeyBoard, Name of Customer: Amith, Dispatch Date: 1981-12-05, Location: Hyderabad Product Name: Ear phones, Name of Customer: Sumith, Dispatch Date: 1981-04-22, Location: Vishakhapatnam Product Name: Mouse, Name of Customer: Sudha, Dispatch Date: 1988-11-05, Location: Vijayawada