核心代码
dbmeta.jsp
<body>
<%
String JDriver="com.microsoft.sqlserver.jdbc.SQLServerDriver";
String connectDB="jdbc:sqlserver://127.0.0.1:1433;DatabaseName=ShopSystem";
try{
Class.forName(JDriver);
}catch(ClassNotFoundException e){
System.out.println("加载数据库失败");
System.exit(0);
}
try{
String user="sa";
String password="xuelong";
Connection con=DriverManager.getConnection(connectDB, user, password);
System.out.println("数据库连接成功");
//获取数据库原始信息
DatabaseMetaData dmd=con.getMetaData();
out.println("<center><h2>ShopSystem 数据库信息</h2></center>");
out.println("连接到URL"+dmd.getURL()+"<br>");
out.println("驱动程序名:"+dmd.getDriverName()+"<br>");
out.println("驱动程序版本:"+dmd.getDriverVersion()+"<br>");
out.println("最大连接数:"+dmd.getMaxConnections()+"<br>");
out.println("数据库名:"+dmd.getDatabaseProductName()+dmd.getDatabaseProductVersion()+"<br>");
out.println("数据库是否支持外部连接");
if(dmd.supportsOuterJoins())
out.println("是");
else
out.println("否");
//创建Statement和Resultset对象,将结果集的原始信息存放在rsmd中
Statement stmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
ResultSet rs=stmt.executeQuery("select * from admin");
ResultSetMetaData rsmd =rs.getMetaData();
//使用表格,显示表的基本信息
String strClmname="<tr><td>列名</td>";
String strClmtype="<tr><td>类型</td>";
String strClmsize="<tr><td>大小</td>";
String strNull="<tr><td>是否为空</td>";
String strIncrease="<tr><td>是否自动增长</td>";
for(int i=1;i<=rsmd.getColumnCount();i++){
strClmname +="<td>"+rsmd.getColumnName(i)+"</td>";
strClmtype +="<td>"+rsmd.getColumnTypeName(i)+"</td>";
strClmsize +="<td>"+rsmd.getColumnDisplaySize(i)+"</td>";
strNull +="<td>"+rsmd.isNullable(i)+"</td>";
strIncrease +="<td>"+rsmd.isAutoIncrement(i)+"</td>";
}
strClmname+="</tr>";
strClmtype+="</tr>";
strClmsize+="</tr>";
strNull+="</tr>";
strIncrease+="</tr>";
out.println("<center><h2>admin 表信息</h2></center>");
out.println("<table width=100% border=1>");
out.println(strClmname);
out.println(strClmtype);
out.println(strClmsize);
out.println(strNull);
out.println(strIncrease);
out.println("</table>");
stmt.close();
if(con!=null)
con.close();
}catch(SQLException e){
out.println(e.getMessage());
}
%>
</body>