`
dahuilang
  • 浏览: 10664 次
  • 性别: Icon_minigender_1
  • 来自: 烟台
社区版块
存档分类
最新评论

数据库 oracle

阅读更多
/**
02
首先在数据库中创建存储过程(如:pl_pro),代码如下:
03
create or replace procedure pl_emp(pl_name varchar2,salary number) is
04
begin
05
  --根据用户名去修改职工工资
06
  update emp set sal=salary where ename=pl_name;
07
end pl_pro;
08
/
09
*/
10
package testOraclePLSQL;
11

12
import java.io.File;
13
import java.io.FileInputStream;
14
import java.io.IOException;
15
import java.sql.CallableStatement;
16
import java.sql.Connection;
17
import java.sql.DriverManager;
18
import java.sql.SQLException;
19
import java.util.Properties;
20

21
/**
22
* 利用JDBC调用Oracle存储过程
23
*
24
* @author Nilux
25
*
26
*/
27
public class GetPLSQL {
28

29
    // test
30
    public static void main(String[] args) {
31
        getConn();
32
        execPLSQL();
33
    }
34

35
    static Connection conn;
36

37
    // 获得连接
38
    public static void getConn() {
39
        FileInputStream fis = null;
40
        // 读取Properties的配置
41
        try {
42
            fis = new FileInputStream(new File(
43
                    "src/testOraclePLSQL/db.properties"));
44
            Properties properties = new Properties();
45
            properties.load(fis);
46
            String url = properties.getProperty("db.url");
47
            String user = properties.getProperty("db.user");
48
            String password = properties.getProperty("db.password");
49
            // 加载Oracle驱动
50
            Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
51
            // 得到数据库连接
52
            conn = DriverManager.getConnection(url, user, password);
53

54
        } catch (Exception e) {
55
            e.printStackTrace();
56
        } finally {
57
            try {
58
                fis.close();
59
            } catch (IOException e) {
60
                e.printStackTrace();
61
            }
62
        }
63

64
    }
65

66
    // ................................//
67
    public static void execPLSQL() {
68
        CallableStatement cs = null;
69
        try {
70
            String sql = "{call pl_emp(?,?)}";
71
            // 创建CallableStatement
72
            cs = conn.prepareCall(sql);
73
            // 给?赋相应的值
74
            cs.setString(1, "SMITH");
75
            cs.setInt(2, 1800);
76
            // 执行
77
            cs.execute();
78
        } catch (Exception e) {
79
            e.printStackTrace();
80
        } finally {
81
            if (conn != null) {
82
                try {
83
                    // 关闭连接
84
                    cs.close();
85
                    conn.close();
86
                } catch (SQLException e1) {
87
                    e1.printStackTrace();
88
                }
89
            }
90
        }
91
    }
92
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics