Step By Step(Java JDBC篇) (十一)

2014-11-24 03:19:38 · 作者: · 浏览: 8
ion,

3 ClassNotFoundException {

4 Class.forName("oracle.jdbc.driver.OracleDriver");

5 Properties conProps = new Properties();

6 conProps.put("user", "sys");

7 conProps.put("password", "helloworld");

8 conProps.put("internal_logon", "sysdba");

9 return DriverManager.getConnection(

10 "jdbc:oracle:thin:@//192.168.1.101:1526/OraHome", conProps);

11 }

12 public static void main(String[] args) throws ClassNotFoundException {

13 Connection conn = null;

14 ResultSet rs = null;

15 try {

16 conn = getConnection();

17 DatabaseMetaData meta = conn.getMetaData();

18 //存储过程使用以下函数获取。

19 rs = meta.getProcedures(null, null, "%");

20 while (rs.next()) {

21 String spName = rs.getString("PROCEDURE_NAME");

22 int spType = rs.getInt("PROCEDURE_TYPE");

23 System.out.println("Stored Procedure Name: " + spName);

24 if (spType == DatabaseMetaData.procedureReturnsResult) {

25 System.out.println("procedure Returns Result");

26 } else if (spType == DatabaseMetaData.procedureNoResult) {

27 System.out.println("procedure No Result");

28 } else {

29 System.out.println("procedure Result unknown");

30 }

31 }

32 } catch (Exception e) {

33 e.printStackTrace();

34 } finally {

35 try {

36 rs.close();

37 conn.close();

38 } catch (SQLException e) {

39 e.printStackTrace();

40 }

41 }

42 }

43 }

11. 插入和查询BLOB类型的二进制数据。

1 public class MyTest {

2 private static Connection getConnection() throws ClassNotFoundException,

3 SQLException {

4 Class.forName("oracle.jdbc.driver.OracleDriver");

5 Properties conProps = new Properties();

6 conProps.put("user", "sys");

7 conProps.put("password", "helloworld");

8 conProps.put("internal_logon", "sysdba");

9 return DriverManager.getConnection(

10 "jdbc:oracle:thin:@//192.168.1.101:1526/OraHome", conProps);

11 }

12

13 public static void main(String[] args) {

14 Connection conn = null;

15 try {

16 conn = getConnection();

17 conn.setAutoCommit(false);

18 // 创建带有blob字段的表

19 String strSQL = "CREATE TABLE Test(name VARCHAR2(20),content BLOB)";

20 Statement stmt = conn.createStatement();

21 stmt.execute(strSQL);

22 //1. 插入一个空的BLOB www.2cto.com

23 oracle.sql.BLOB blob = null;

24 String insertSQL = "insert into Test(name,content) values( ,empty_blob())";

25 PreparedStatement pstmt = conn.