Java代码
1. package com.database.manager;
2.
3. import java.sql.Connection;
4. import java.sql.DatabaseMetaData;
5. import java.sql.DriverManager;
6. import java.sql.ResultSet;
7. import java.
sql.SQLException;
8.
9. /**
10. * @author daoger
11. *
12. * 2009-9-24
13. */
14.public class DatabaseMetaDateApplication
15.{
16. private DatabaseMetaData dbMetaData = null;
17.
18. private Connection con = null;
19.
20. private void getDatabaseMetaData()
21. {
22. try
23. {
24. if (dbMetaData == null)
25. {
26. Class.forName("oracle.jdbc.driver.OracleDriver");
27. String url = "jdbc:oracle:thin:@192.168.0.2:1521:×××";
28. String user = "×××";
29. String password = "×××";
30. con = DriverManager.getConnection(url, user, pass
word);
31. dbMetaData = con.getMetaData();
32. }
33. } catch (ClassNotFoundException e)
34. {
35. // TODO: handle ClassNotFoundException
36. e.printStackTrace();
37. } catch (SQLException e)
38. {
39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. ;"); 67.
// TODO: handle SQLException e.printStackTrace(); } } public void colseCon() { try { if (con != null) { con.close(); } } catch (SQLException e) { // TODO: handle SQLException e.printStackTrace(); } } /**
* 获得数据库的一些相关信息
*/
public void getDataBaseInformations()
{
try
{
System.out.println("URL:" + dbMetaData.getURL() + "
System.out.println("UserName:" + dbMetaData.getUser Name() + ";");
68. System.out.println("isReadOnly:" + dbMetaData.isRea dOnly() + ";");
69. System.out.println("DatabaseProductName:" + dbMetaD ata.getDatabaseProductName() + ";");
70. System.out.println("DatabaseProductVersion:" + dbMe taData.getDatabaseProductVersion() + ";");
71. System.out.println("DriverName:" + dbMetaData.getDr iverName() + ";");
72. System.out.println("DriverVersion:" + dbMetaData.ge tDriverVersion());
73. } catch (SQLException e)
74. {
75. // TODO: handle SQLException
76. 77. 78. 79.
80. 81. 82. 83. 84. 85. 86. 87.
e.printStackTrace(); } }
/** * 获得该用户下面的所有表 */ public void getAllTableList(String schemaName) { try { // table type. Typical types are "TABLE", "VIEW", " SYSTEM 88. // TABLE", "GLOBAL TEMPORARY", "LOCAL TEMPORARY", " ALIAS", 89. // "SYNONYM". 90. String[] types = 91. { "TABLE" }; 92. ResultSet rs = dbMetaData.getTables(null, schemaNam e, "%", types); 93. while (rs.next()) 94. { 95. String tableName = rs.getString("TABLE_NAME"); 96. ", "SYSTEM 97. ", "ALIAS", 98. 99. 100. 101. // "SYNONYM". String tableType = rs.getString("TABLE_TYPE"); // explanatory comment on the table String remarks = rs.getString("REMARKS"); // TABLE", "GLOBAL TEMPORARY", "LOCAL TEMPORARY // table type. Typical types are "TABLE", "VIEW
102. System.out.println(tableName + "-" + table Type + "-" + remarks);
103. }
104. } catch (SQLException e)
105. {
106. // TODO: handle SQLException
107. e.printStackTrace();
108. }
109. }
110.
111. 112. 113. 114. 115. 116. 117. 118. 119. 120. maName, 121. 122. 123. ");
124. "VIEW", 125. ORARY", 126. 127. ");
128. 129.
/** * 获得该用户下面的所有视图 */ public void getAllViewList(String schemaName) { try { String[] types = { "VIEW" }; ResultSet rs = dbMetaData.getTables(null, sche "%", types); while (rs.next()) { String viewName = rs.getString("TABLE_NAME // table type. Typical types are "TABLE", "SYSTEM // TABLE", "GLOBAL TEMPORARY", "L