import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
public class GettingCloumnsNames {
public static void main(String[] args) throws Exception{
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/bookshop";
String user = "root";
String password = "";
Class.forName(driver).newInstance();
Connection con = DriverManager.getConnection(url,user,password);
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("select * from book");
ResultSetMetaData rsmd = rs.getMetaData();
int col = rsmd.getColumnCount();
System.out.println("列数为:" + col);
for(int i = 1; i <= col; i ++){
String col_name = rsmd.getColumnName(i);
System.out.println(col_name);
}
con.close();
}
}
猎取数据库表的列名20.5.5.Getting Column Names from a database table in Java
最新推荐文章于 2024-11-06 11:17:40 发布