元数据案例(表和数据库的各种信息)
package wuerer; import java.security.interfaces.RSAKey; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.Statement; import com.mysql.jdbc.DatabaseMetaData; public class YuanShu { public static void main(String[] args) throws Exception { Class.forName("com.mysql.jdbc.Driver"); Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/boooks","root","tiger");
//获得数据库的各种连接信息 // java.sql.DatabaseMetaData dmd= conn.getMetaData(); // System.out.println(dmd.getDriverName()); // System.out.println(dmd.getDatabaseProductName()); // System.out.println(dmd.getDatabaseProductVersion()); // System.out.println(dmd.getURL()); // System.out.println(dmd.getUserName());
//获得表的信息 Statement st=conn.createStatement(); ResultSet rs=st.executeQuery("select * from book"); ResultSetMetaData rmd=rs.getMetaData(); System.out.println("列数:"+rmd.getColumnCount()); int size=rmd.getColumnCount(); for(int i=1;i<=size;i++){ System.out.println(rmd.getColumnName(i)+","+rmd.getColumnTypeName(i)+","); } while(rs.next()){ for(int i=1;i<=size;i++){ System.out.print(rs.getObject(i)+"\t"); } System.out.println(); } conn.close(); } }