DatabaseMetaData类的应用获得表、列、主键相关信息

package Core;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class jdbc {

/**
* @param args
*/
private final String oracleDriverName = "oracle.jdbc.driver.OracleDriver";
private final String oracleUrlToConnect = "jdbc:oracle:thin:@192.168.0.111:1521:orcl";
private Connection OracleConnection = null;
public void conn() {
try {
Class.forName(oracleDriverName);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {

OracleConnection = DriverManager.getConnection(oracleUrlToConnect,
"apl", "apl");
//列注释和表注释必须在驱动时打开。下边这句话是必要的
((oracle.jdbc.driver.OracleConnection) OracleConnection)
.setRemarksReporting(true);
if (OracleConnection != null) {
System.out.println("Connection");
}
DatabaseMetaData db = OracleConnection.getMetaData();
// 表信息
String[] t = { "TABLE", "VIEW" };
ResultSet tableRs = db.getTables(null, null, "IV_TM_IV_GRP", t);
while (tableRs.next()) {
for (int i = 1; i < 6; i++) {
System.out.print(tableRs.getString(i) + " ");
}
System.out.println("+++++++++++++++++++++++++++++++++");
}
// 列信息
ResultSet rs = db.getColumns(null, null, "IV_TM_IV_GRP", null);
while (rs.next()) {
for (int i = 1; i < 19; i++) {
System.out.print(rs.getString(i) + " ");
}
System.out.println("+++++++++++++++++++++++++++++++++");

}
//主键信息
ResultSet pkRs = db.getPrimaryKeys(null, null, "IV_TM_IV_GRP");
while (pkRs.next()) {
for (int i = 1; i < 6; i++) {
System.out.print(pkRs.getString(i) + " ");
}
System.out.println("+++++++++++++++++++++++++++++++++");

}

} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

public static void main(String[] args) {
// TODO Auto-generated method stub
jdbc j = new jdbc();
j.conn();
}

}

你可能感兴趣的:(Oracle)