下例将以sqlserver2000为例子,说明一下怎么得到列名:
String connStr="jdbc:microsoft:sqlserver://hello:1433;DatabaseName=pubs";
String connDriver="com.microsoft.jdbc.sqlserver.SQLServerDriver";
String lsql="select * from authors";
try {
Class.forName(connDriver);
Connection con = DriverManager.getConnection(connStr, "sa", "iloveyou");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery(lsql);
----------------------------------------------------------
虚线中间代码将打顺序找印出来表authors的字段名
ResultSetMetaData md=rs.getMetaData();
for (int i=1;i<=md.getColumnCount();i++){
System.out.println(md.getColumnName(i));
}
-----------------------------------------------------------
}
catch (SQLException ex) {
ex.printStackTrace();
}
catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
}
通过jdbc取表结构
最新推荐文章于 2023-02-13 13:23:06 发布