package cn.gzsxt.test;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
public class TestJDBCgetTable {
public static void main(String[] args) {
try {
// 1.加载数据库驱动类
Class.forName("oracle.jdbc.driver.OracleDriver");
// 2.获取数据库连接对象
String url = "jdbc:oracle:thin:@127.0.0.1:1521:orcl";
String user = "SCOTT";
String password = "tiger";
Connection conn = DriverManager.getConnection(url, user, password);
// 3.获取数据库的元信息
DatabaseMetaData metaData = conn.getMetaData();
// 查看数据库中有哪些表
// ResultSet该对象类似于迭代器
ResultSet rs = metaData.getTables(conn.getCatalog(), "SCOTT", null, new String[] { "TABLE", "VIEW" });
while (rs.next()) {
System.out.println(rs.getString(2) + "-->" + rs.getString(3));
}
// 查看数据库的emp表有哪些列
ResultSet rs2 = metaData.getColumns(conn.getCatalog(), "SCOTT", "EMP", null);
while(rs2.next()) {
System.out.println(rs2.getString(4)+"-->"+rs2.getString(6)+"-->"+rs2.getInt(7));
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
}