如何利用JDBC显示数据库详细信息?
本来打算在控制台就OK了!但是效果不好看!在jsp上面写了一个管理类!还没有完全完工的!就是显示数据库表 存储过程 还有基本信息而已...代码如下
//database manager class
class DBM{
private javax.servlet.jsp.JspWriter out;
private Connection con;
private Statement stmt;
private ResultSet rs;
public DBM(String driverName,String url,String userName,String passWord,javax.servlet.jsp.JspWriter out)throws Exception{
Class.forName(driverName);
this.out=out;
con=DriverManager.getConnection(url,userName,passWord);
}
public void lookInfo()throws Exception{
DatabaseMetaData dbmd=con.getMetaData();
String tableType=null;
out.print("<strong>DataBaseInfo</strong><table>");
out.print("<tr><td>DataBaseName:</td><td>"+dbmd.getDatabaseProductName()+"</td></tr>");
out.print("<tr><td>DataBaseVersion:</td><td>"+dbmd.getDatabaseProductVersion()+"</td></tr>");
out.print("<tr><td>the Numeric Function:</td><td>"+dbmd.getNumericFunctions()+"</td></tr>");
out.print("<tr><td>the String Function:</td><td>"+dbmd.getStringFunctions()+"</td></tr>");
out.print("<tr><td>the TimeDate Function:</td><td>"+dbmd.getTimeDateFunctions()+"</td></tr>");
out.print("<tr><td>the System Function:</td><td>"+dbmd.getSystemFunctions()+"</td></tr>");
out.print("</table>");
out.print("<strong>ProcedureInfo</strong><table>");
getProcedureDetail(dbmd.getProcedures(null,null,null));
//show all the tables
try{
rs=dbmd.getTables(null,null,null,null);
}catch(Exception proE){}
out.print("<strong>DataBase Tables Info</strong><br>");
while(rs.next()){
tableType=rs.getString(4);
out.print("<strong>TableName:</strong>"+rs.getString(3)+" <strong>Type:</strong>"+tableType+"<br>");
if(tableType.indexOf("VIEW")>=0||tableType.indexOf("TABLE")>=0){
try{
getTableDetail(dbmd.getColumns(null,null,rs.getString(3),null));
}catch(Exception columnE){}
}
}
this.closeAll();
}
//show the column information
private void getTableDetail(ResultSet tableRs)throws Exception{
out.print("<table border=1><tr><td>COLUMN_NAME</td><td>DATA_TYPE</td><td>TYPE_NAME</td><td>COLUMN_SIZE</td><td>IS_NULLABLE</td><td>CHAR_OCTET_LENGTH</td></tr>");
while(tableRs.next()){
out.print("<tr><td>"+tableRs.getString(4)+"</td><td>"+tableRs.getInt(5)+"</td><td>"+tableRs.getString(6)+"</td><td>"+tableRs.getInt(7)+"</td><td>"+tableRs.getString(18)+"</td><td>"+tableRs.getInt(16)+"</td></tr>");
}
out.print("</table>");
tableRs.close();
}
//show all the procedures
private void getProcedureDetail(ResultSet procRs)throws Exception{
out.print("<table border=1><tr><td>PROCEDURE_NAME</td><td>REMARKS</td><td>PROCEDURE_TYPE</td></tr>");
while(procRs.next()){
out.print("<tr><td>"+procRs.getString(3)+"</td><td>"+procRs.getString(7)+"</td><td>"+procRs.getShort(8)+"</td></tr>");
}
out.print("</table>");
procRs.close();
}
//close all the resource
private void closeAll()throws SQLException{
try{
if(rs!=null)rs.close();
}catch(Exception e){
}
try{
if(stmt!=null)stmt.close();
}catch(Exception e){
}
try{
if(con!=null)con.close();
}catch(Exception e){
}
}
}
------------
构造函数传入 驱动类 还有连接url 用户 密码 然后调用lookInfo 方法就OK了!
本文地址:http://www.45fan.com/bcdm/67885.html