<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*,java.io.*"%>
<%!
String typeToString(int i)
{
  String ret="";
  switch(i)
  {
  	//根据变量i的不同获得不同的类型
case(1):ret="CHAR";break;
  	case(2):ret="NUMERIC";break;
  	case(3):ret="DECIMAL";break;
  	case(4):ret="INTEGER";break;
  	case(5):ret="SMALLINT";break;
  	case(6):ret="FLOAT";break;
  	case(8):ret="DOUBLE";break;
  	case(12):ret="VARCHAR";break;
  	case(91):ret="DATE";break;
  	default:ret="other";
  }
  return ret;
  }
 %>
 <%!
   //定义函数getCol，得到表列信息
   String getCol(ResultSet rst,int type,int colNum)throws Exception
   {
      String ret="";
      switch(type)
      {
         case(1):ret=rst.getString(colNum);break;
         case(4):ret=String.valueOf(rst.getInt(colNum));break;
         case(5):ret=String.valueOf(rst.getInt(colNum));break;
         case(6):ret=String.valueOf(rst.getFloat(colNum));break;
         case(8):ret=String.valueOf(rst.getDouble(colNum));break;
         case(12):ret=rst.getString(colNum);break;
         default:ret="not know";
      }
      return trans(ret);
 }
 %>
 <%! String trans(String chi)
{
      String result = null;
      byte temp [];
      try
      {
          temp=chi.getBytes("iso-8859-1");
          result = new String(temp);
      }
      catch(UnsupportedEncodingException e)
      {
          System.out.println (e.toString());
      }
	    return result;
}
%>
 <html>
<body>
<center>
<html>
<body>
数据库信息
<form action="dbinfo.jsp" method=get>
<table><tr><td>数据库的名字：<input type=text name=database></td>
<td>表的名字：<input type=text name=table></td></tr>
<tr> <td  colspan=2><input type=submit value=提交></td></tr>
</table>
</form>
<%
String database=(String)request.getParameter("database");
String table=(String)request.getParameter("table");
%>
<table border=1>
<%
   try
   {
   
    String sDBDriver = "sun.jdbc.odbc.JdbcOdbcDriver";  
    String sConnStr = "jdbc:odbc:try";//“try”的意思就是与刚才设置的数据源名对应; 
    Connection Con = null;
    Statement Stmt = null;
     try          /*加载驱动程序*/
    {
    Class.forName(sDBDriver); 
     }
    catch(java.lang.ClassNotFoundException e)
    { 
   out.print("Connect to Database error: " + e.getMessage());
     }
   try          /*建立连接*/
    {
   Con = DriverManager.getConnection(sConnStr,"sa",""); 
     }
   catch(SQLException e)
   {
   out.print(e.toString());
   }
   Stmt = Con.createStatement();     
    ResultSet rst=Stmt.executeQuery("use "+database+";select * from  "+table);
    ResultSetMetaData meta=rst.getMetaData()  ;
    int col=meta.getColumnCount();
    out.println("表"+table+"共有："+col+"个字段，这些字段是：<br>");
    out.println("<tr>");
    for(int i=1;i<col+1;i++)
    {
       out.println("<td>字段名：");
       out.println(meta.getColumnName(i));
       out.println("类型：&nbsp;");
       out.println(typeToString(meta.getColumnType(i)));
       out.println("</td>");
    }
       out.println("</tr>");
   
    while(rst.next())
    {
      out.println("<tr>");
      for(int i=1;i<col+1;i++)
      {
        out.println("<td>"+getCol(rst,meta.getColumnType(i),i)+"</td>");
      }
      out.println("</tr>");
     }
    //关闭连接、释放资源
    rst.close();
    Stmt.close();
    Con.close();
    }
    catch(Exception e)
    {
    e.printStackTrace();
    }    
    %>  
    </table>  
<center>
</body>
</html>
