[java]代码库
/**
首先在数据库中创建存储过程(如:pl_pro),代码如下:
create or replace procedure pl_emp(pl_name varchar2,salary number) is
begin
--根据用户名去修改职工工资
update emp set sal=salary where ename=pl_name;
end pl_pro;
/
*/
package testOraclePLSQL;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
/**
* 利用JDBC调用Oracle存储过程
*
* @author Nilux
*
*/
public class GetPLSQL {
// test
public static void main(String[] args) {
getConn();
execPLSQL();
}
static Connection conn;
// 获得连接
public static void getConn() {
FileInputStream fis = null;
// 读取Properties的配置
try {
fis = new FileInputStream(new File(
"src/testOraclePLSQL/db.properties"));
Properties properties = new Properties();
properties.load(fis);
String url = properties.getProperty("db.url");
String user = properties.getProperty("db.user");
String password = properties.getProperty("db.password");
// 加载Oracle驱动
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
// 得到数据库连接
conn = DriverManager.getConnection(url, user, password);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
// ................................//
public static void execPLSQL() {
CallableStatement cs = null;
try {
String sql = "{call pl_emp(?,?)}";
// 创建CallableStatement
cs = conn.prepareCall(sql);
// 给?赋相应的值
cs.setString(1, "SMITH");
cs.setInt(2, 1800);
// 执行
cs.execute();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (conn != null) {
try {
// 关闭连接
cs.close();
conn.close();
} catch (SQLException e1) {
e1.printStackTrace();
}
}
}
}
}
//源代码片段来自云代码http://yuncode.net