本来打算在控制台就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了!
新闻热点
疑难解答