JDBC调用存储过程基本流程(SXT)
Demo
- import java.sql.*;
- public class TestProcedure
- {
- /**
- * @JDBC调用存储过程基本流程, procedure p 是4个参数的。第3和第4个为输出参数
- */
- public static void main(String[] args)
- {
- // TODO Auto-generated method stub
- Connection conn = null;
- CallableStatement cstmt = null;
- try
- {
- Class.forName("com.mysql.jdbc.Driver");
- conn = DriverManager.getConnection(
- "jdbc:mysql://localhost:3306/college", "root", "123456");
- cstmt = conn.prepareCall("{call p (?,?,?,?)}");
- cstmt.registerOutParameter(3, Types.INTEGER);
- cstmt.registerOutParameter(4, Types.INTEGER);
- cstmt.setInt(1, 20);
- cstmt.setInt(2, 30);
- cstmt.setInt(4, 40);
- cstmt.execute();
- System.out.println(cstmt.getInt(3));
- System.out.println(cstmt.getInt(4));
- } catch (ClassNotFoundException e)
- {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (SQLException e)
- {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } finally
- {
- try
- {
- if (cstmt != null)
- {
- cstmt.close();
- cstmt = null;
- }
- if(conn != null)
- {
- conn.close();
- conn = null;
- }
- } catch (SQLException e)
- {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- }
本文介绍了一个使用JDBC调用存储过程的具体示例,演示了如何连接数据库、准备并执行带有输入及输出参数的存储过程。该示例适用于初学者了解JDBC与存储过程的基本交互方式。
3418

被折叠的 条评论
为什么被折叠?



