永发信息网

请教:JDBC如何得到数据库列的类型

答案:1  悬赏:0  手机版
解决时间 2021-03-30 23:56
  • 提问者网友:我们很暧昧
  • 2021-03-30 06:05
请教:JDBC如何得到数据库列的类型
最佳答案
  • 五星知识达人网友:琴狂剑也妄
  • 2021-03-30 07:16
经过access测试,ok

String url = "jdbc:odbc:test";//最后一个为数据库名
Statement sm = null;
String command = null;
ResultSet rs = null;
String tableName = null;
String cName = null;
String result = null;
String []pram = new String[1];
List tables = new ArrayList();
pram[0] = "table";
BufferedReader input = new BufferedReader(new InputStreamReader(
System.in));
try {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); // 加载驱动
} catch (ClassNotFoundException e) {
System.out.println("Can not load Jdbc-Odbc Bridge Driver");
System.err.print("ClassNotFoundException:");
System.err.println(e.getMessage());
}
Connection con = DriverManager.getConnection(url, "admin", "admin"); // 连接到数据库
DatabaseMetaData dm = con.getMetaData();
rs = dm.getTables(null, null, null, pram);//查找所有的表
while(rs.next()){
tables.add(rs.getString(3));
}
rs.close();
for(String tb : tables){
rs = dm.getColumns(null, null, tb, null);//查找当前表的字段
System.out.println("\nthe table is: " + tb);
ResultSetMetaData rsmd = rs.getMetaData();
int len, type;
len = rsmd.getColumnCount();
// System.out.println(len);
for(int i = 1; i <= len; i ++){
type = rsmd.getColumnType(i);
//这里是获取了一个字段类型的int型,需要转化成string的话要做一个swtich,就不转了,你自己看java.sql.Types这个类去
System.out.print("\t" + rsmd.getColumnName(i) + ": " + rsmd.getColumnType(i));
}
}
rs.close();
con.close();
// }
} catch (SQLException ex) {
System.out.println("SQLException:");
while (ex != null) {
System.out.println("Message:" + ex.getMessage());
ex = ex.getNextException();
}
} catch (Exception e) {
System.out.println("IOException");
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯