1. 在項(xiàng)目src目錄下創(chuàng)建jdbc.properties文件
內(nèi)容:
dbName=JMJL
className=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@168.9.202.49:1521:test
user=TEST
password=TEST
2. 寫一個(gè)jdbc處理通用的簡(jiǎn)單處理工具類JdbcByPropertiesUtil
參照《JDBC獲取連接、關(guān)閉連接的簡(jiǎn)單工具類2》,
地址:http://blog.csdn.net/hu_shengyang/article/details/7861187
3. 下面就是一段獲取數(shù)據(jù)庫(kù)元數(shù)據(jù)的示例代碼:
- package com.adam.test.entity;
-
- import java.sql.Connection;
- import java.sql.DatabaseMetaData;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import java.util.Properties;
-
- import com.adam.dev.utils.JdbcByPropertiesUtil;
-
- public class TestDatabaseMetaData {
- private JdbcByPropertiesUtil jbpu = JdbcByPropertiesUtil.getInstance();
-
- public JdbcByPropertiesUtil getJbpu() {
- return jbpu;
- }
-
- public void setJbpu(JdbcByPropertiesUtil jbpu){
- this.jbpu = jbpu;
- }
-
- public Properties getProperties(){
- Properties pros = JdbcByPropertiesUtil.readPropertiesFile();
- return pros;
- }
-
- /**
- * 讀取配置文件jdbc.properties中的數(shù)據(jù)庫(kù)名稱
- * @return
- * @throws Exception
- */
- public String getDataSourceName()throws Exception{
- Properties pros = this.getProperties();
- String dbName = pros.get("dbName").toString();
- return dbName;
- }
-
- /**
- * 獲取數(shù)據(jù)庫(kù)中的表名稱與視圖名稱
- * @return
- */
- public List getTablesAndViews()throws Exception{
- Connection conn = jbpu.getConnection();
- ResultSet rs = null;
- List list = new ArrayList();
- try {
- Properties pros = this.getProperties();
- String schema = pros.get("user").toString();
- DatabaseMetaData metaData = conn.getMetaData();
- rs = metaData.getTables(null, schema, null, new String[]{"TABLE","VIEW"});
- while(rs.next()){
- String tableName = rs.getString("TABLE_NAME");
- list.add(tableName);
- }
- } catch (SQLException e) {
- e.printStackTrace();
- } finally{
- jbpu.close(rs, null, conn);
- }
- return list;
- }
-
- /**
- * 利用表名和數(shù)據(jù)庫(kù)用戶名查詢出該表對(duì)應(yīng)的字段類型
- * @param tableName 表名
- * @return
- * @throws Exception
- */
- public String generateBean()throws Exception{
- Connection conn = jbpu.getConnection();
- ResultSet rs = null;
- String strJavaBean = "";
- String tableName = this.getDataSourceName();
- try{
- Properties pros = this.getProperties();
- String schema = pros.get("user").toString();
- DatabaseMetaData metaData = conn.getMetaData();
- rs = metaData.getColumns(null, schema, tableName, null);
- Map map = new HashMap();
- while(rs.next()){
- String columnName = rs.getString("COLUMN_NAME");//列名
- String dataType = rs.getString("DATA_TYPE");//字段數(shù)據(jù)類型(對(duì)應(yīng)java.sql.Types中的常量)
- String typeName = rs.getString("TYPE_NAME");//字段類型名稱(例如:VACHAR2)
- map.put(columnName, dataType+":"+typeName);
- }
-
- // 其它生成javaBean的相關(guān)操作
-
- }catch(Exception e){
- e.printStackTrace();
- }finally{
- jbpu.close(rs, null, conn);
- }
- return strJavaBean;
- }
-
- }
這只是一些簡(jiǎn)單常用的jdbc元數(shù)據(jù)的獲取方式,如果需要更加深入請(qǐng)參考java api。
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)
點(diǎn)擊舉報(bào)。