gpt4 book ai didi

jdbc - JDBC中如何查看表结构?

转载 作者:行者123 更新时间:2023-12-05 09:23:46 42 4
gpt4 key购买 nike

我是 JDBC 的新手,我有疑问,如何使用 jdbc 程序查看表结构.. 请提供代码,我试过但它显示错误我的代码是

import java.sql.*;

public class ram {

static final String JDBC_DRIVER = "oracle.jdbc.driver.OracleDriver";
static final String DB_URL = "jdbc:oracle:thin:@192.168.1.12:1521:aftdb";


static final String USER = "system";
static final String PASS = "manager";

public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try{

Class.forName("oracle.jdbc.driver.OracleDriver");


System.out.println("Connecting to database...");
conn = DriverManager.getConnection(DB_URL,USER,PASS);


System.out.println("Creating statement...");
stmt = conn.createStatement();
String sql;
sql = "desc emp";
ResultSet rs = stmt.executeQuery(sql);

while(rs.next()){

String emp= rs.getString("emp");


System.out.println("ID: " + emp);
}

rs.close();
stmt.close();
conn.close();
}catch(SQLException se){

se.printStackTrace();
}catch(Exception e){

e.printStackTrace();
}finally{

try{
if(stmt!=null)
stmt.close();
}catch(SQLException se2){
}// nothing we can do
try{
if(conn!=null)
conn.close();
}catch(SQLException se){
se.printStackTrace();
}
}

}
}

但它显示错误。请帮助任何人如何在 jdbc 程序中获取表结构,建议我

最佳答案

Documentation for ResultSetMetadata

使用结果集元数据

改变你的sql查询

sql = "desc emp";

作为

sql = "select * from emp";

例如来自文档

 ResultSet rs = stmt.executeQuery("select * from emp");
ResultSetMetaData rsmd = rs.getMetaData();
System.out.println("No. of columns : " + rsmd.getColumnCount());
System.out.println("Column name of 1st column : " + rsmd.getColumnName(1));
System.out.println("Column type of 1st column : " + rsmd.getColumnTypeName(1));

关于jdbc - JDBC中如何查看表结构?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19222577/

42 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com