日期:2014-05-16  浏览次数:20501 次

Java应用之得到Oracle,Mysql数据库表信息

???? 最近在使用公司内部框架生成代码的时候,在思考框架是怎么判断表是否存在,怎样得到表的信息,利用中午的时间找了点资料,现在可以实现判断Oracle,Mysql表是否存在,表内是否存在某个字段,得到表的字段信息,如字段名称,字段类型,长度,以及注释,首先带上我参考的信息:

???

http://stackoverflow.com/questions/205736/get-list-of-all-tables-in-oracle
http://dev.mysql.com/doc/refman/5.0/en/columns-table.html
http://stackoverflow.com/questions/9016578/how-to-get-primary-key-column-in-oracle

??? 数据库及版本:Oracle 10g,Mysql 5.6.14。

??? 废话不多说,上代码:

??? 首先是公共方法:

???

public Connection getMysqlConnection() {
		String driver = "com.mysql.jdbc.Driver";
		String url = "jdbc:mysql://localhost:3306/test";// 要操作的数据库名称
		String username = "root";// 数据库用户名
		String password = "123";// 密码
		return getConnection(driver, url, username, password);
	}

	public Connection getOracleConnection() {
		String driver = "oracle.jdbc.driver.OracleDriver";
		String url = "jdbc:oracle:thin:@localhost:1521:xe";// 要操作的数据库名称
		String username = "tmd";// 数据库用户名
		String password = "tmd";// 密码
		return getConnection(driver, url, username, password);
	}

	public Connection getConnection(String driver, String url, String userName,
			String passwd) {
		Connection conn = null;
		try {
			Class.forName(driver);
			conn = DriverManager.getConnection(url, userName, passwd);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return conn;
	}

	public void closeConnection(ResultSet rs, Statement statement,
			Connection conn) {
		try {
			if (rs != null) {
				rs.close();
			}
			if (statement != null) {
				statement.close();
			}
			if (conn != null) {
				conn.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

??? 得到Mysql指定数据库所有表名:

???

//得到Mysql指定数据库所有表名
	public void getMysqlDataBaseTables(String database) throws Exception {
		Connection conn = getMysqlConnection();
		ResultSet rs = null;
		Statement statement = conn.createStatement();
		String sql = "select table_name from information_schema.tables where table_schema='"
				+ database + "'";
		rs = statement.executeQuery(sql);
		System.out.println("-------------DB[" + database
				+ "] All Tables---------------");
		while (rs.next()) {
			System.out.println(rs.getString("table_name"));
		}
		closeConnection(rs, statement, conn);
	}

??? Oracle当前用户下所有表名:

???

public void getOracleDataBaseTables() throws Exception {
		Connection conn = getOracleConnection();
		ResultSet rs = null;
		Statement statement = conn.createStatement();
		String sql = "select table_name from user_tables";
		rs = statement.executeQuery(sql);
		while (rs.next()) {
			System.out.println(rs.getString("table_name"));
		}
		closeConnection(rs, statement, conn);
	}

?? 检测Mysql表是否存在:

??

//检查Mysql表是否存在
	public boolean checkMysqlTableExist(String db, String tableName)
			throws Exception {
		Connection conn = getMysqlConnection();
		DatabaseMetaData meta = conn.getMetaData();
		boolean isExist = false;
		// types -要包括的表类型组成的列表,可设为null,表示所有的
		// ResultSet rs = meta.getTables(db, null, tableName,new String[] {
		// "TABLE" });
		ResultSet rs = meta.getTables(db, null, tableName, null);
		System.out
				.println("TABLE_CAT \t TABLE_SCHEM \t TABLE_NAME \t TABLE_TYPE");
		while (rs.next()) {
			System.out.println(rs.getString("TABLE_CAT") + "\t"
					+ rs.getString("TABLE_SCHEM") + "\t"
					+ rs.getString("TABLE_NAME") + "\t"
					+ rs.getString("TABLE_TYPE"));
			isExist = true;
		}
		closeConnection(rs, null, conn);
		return isExist;
	}

?? 检查Oracle表是否存在

???

//检查Oracle表是否存在
	//经我测试只能检查当前连接用户下的是