jdbc调用存储过程



jdbc调用存储过程

import java.sql.*;

import java.sql.CallableStatement;

/**
*
* 简单的jdbc调用存储过程 只有输入参数 返回单个结果集
*
*/
public class GeTest1 {
public static void main(String[] args) {

Connection connection = null;
//用于执行 SQL 存储过程的接口
CallableStatement statement = null;
ResultSet resultSet = null;

try {
Class.forName(“com.mysql.jdbc.Driver”);
String url = “jdbc:mysql://localhost:3306/test”;

String user = “root”;

String password = “123″;


connection = DriverManager.getConnection(url, user, password);

String sql = “call proc_selectEmployee(?)”;

//调用存储过程
statement = connection.prepareCall(sql);

statement.setString(1, “SZ65380″);

resultSet = statement.executeQuery();

if (resultSet.next()) {
System.out.println(resultSet.getString(“address”));
}

} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (resultSet != null) {
resultSet.close();
}
if (statement != null) {
statement.close();
}
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}