通過連線檢視資料庫相關資訊

不設限發表於2011-12-11
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;


public class TestMetaData {
public static void main(String[] args) throws Exception {
Connection conn=null;
String url="jdbc:mysql://localhost/dandan?user=root&password=dada&characterEncoding=utf-8";
Class.forName("org.gjt.mm.mysql.Driver");
conn=DriverManager.getConnection(url);
ResultSet rs=null;
DatabaseMetaData dmd=conn.getMetaData();
if(dmd==null) {
System.out.println("No meta is available");
} else {
System.out.println("資料庫名稱: "+dmd.getDatabaseProductName());
System.out.println("資料庫版本 : "+dmd.getDatabaseProductVersion());
System.out.println("資料庫的驅動程式: "+dmd.getDriverName());
System.out.println("驅動程式版本號: "+dmd.getDriverVersion());
System.out.println("併發訪問的使用者個數: "+dmd.getMaxConnections());
System.out.println("資料型別列表: ");
System.out.println ("結果集型別及支援情況(true-支援,false-不支援)");
System.out.println ("TYPE_FORWARD_ONLY: " + 
dmd.supportsResultSetType(ResultSet.TYPE_FORWARD_ONLY)); 
System.out.println ("TYPE_SCROLL_INSENSITIVE: " + 
dmd.supportsResultSetType(ResultSet.TYPE_SCROLL_INSENSITIVE)); 
System.out.println ("TYPE_SCROLL_SENSITIVE: " + 
dmd.supportsResultSetType(ResultSet.TYPE_SCROLL_SENSITIVE)); 
System.out.println ("CONCUR_READ_ONLY: " + 
dmd.supportsResultSetType(ResultSet.CONCUR_READ_ONLY)); 
System.out.println ("CONCUR_UPDATABLE: " + 
dmd.supportsResultSetType(ResultSet.CONCUR_UPDATABLE)); 
rs=dmd.getTypeInfo();
while(rs.next()) {
System.out.println("\t"+rs.getString(1));
}
rs.close();
}
}
}

相關文章