Step By Step(Java JDBC篇) (八)

2014-11-24 03:19:38 · 作者: · 浏览: 10
rs.next()) {

26 String tableName = rs.getString(3);

27 System.out.println("Tablename = " + tableName);

28 String tableCatalog = rs.getString(1);

29 System.out.println("TableCatalog = " + tableCatalog);

30 String tableSchema = rs.getString(2);

31 System.out.println("TableSchema = " + tableSchema);

32 }

33 } catch (Exception e) {

34 e.printStackTrace();

35 } finally {

36 try {

37 rs.close();

38 conn.close();

39 } catch (SQLException e) {

40 e.printStackTrace();

41 }

42 }

43 }

44 }

45 //获取当前db下所有schema,及其包含的数据库对象的metadata。

46 public class MyTest {

47 private static Connection getConnection() throws SQLException,

48 ClassNotFoundException {

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

50 Properties conProps = new Properties();

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

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

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

54 return DriverManager.getConnection(

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

56 }

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

58 Connection conn = null;

59 try {

60 conn = getConnection();

61 DatabaseMetaData dmd = conn.getMetaData();

62 //1. 获取当前数据库的所有schema信息

63 ResultSet rs1 = dmd.getSchemas();

64 while (rs1.next()) {

65 String ss = rs1.getString(1);

66 //2. 根据得到schema名字作为过滤条件,来获取其包含的objects的metadata

67 ResultSet rs2 = dmd.getTables(null, ss, "%", null);

68 while (rs2.next()) {

69 System.out.println(rs2.getString(3) + " " + rs2.getString(4));

70 }

71 rs2.close();

72 }

73 rs1.close();

74 } catch (Exception e) {

75 e.printStackTrace();

76 } finally {

77 try {

78 conn.close();

79 } catch (SQLException e) {

80 e.printStackTrace();

81 }

82 }

83 }

84 }

8. 批量数据插入。

当有大量数据需要被同时插入的时候,如果使用批量插入的方式可以大大提高插入的效率。其中效率提高主要源于一次性的批量插入减少了大量的网络IO,与此同时,数据库服务器在一次收到多条记录插入时,可以执行更好的优化处理。特别对于实时性比较高,数据量也很大的系统,使用批量插入确实可以给系统带来明显的改善。

1 public class MyTest {

2 private static Connection getConnection() throws SQLException,

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.g