<%@ page contentType="text/html; charset=gb2312" %>
<%@ page info="database handler"%> 
<%@ page import="java.io.*"%> 
<%@ page import="java.util.*"%> 
<%@ page import="java.sql.*"%> 
<%@ page import="javax.servlet.*"%> 
<%@ page import="javax.servlet.http.*"%> 
<!--导入结束--> 
<html>
<head>
<title>数据库连接实验</title>
</head>
<body>
<% 
    String sDBDriver = "sun.jdbc.odbc.JdbcOdbcDriver";  
    String sConnStr = "jdbc:odbc:try";//“try”的意思就是与刚才设置的数据源名对应; 
    Connection Con = null;
    Statement Stmt = null;
try 
{ 
     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();    /*创建JDBC声明*/ 
        String query="CREATE TABLE GUEST" +           /*在数据库中创建新的表*/
                     "(NUM VARCHAR(32),"+ 
                     "USERNAME TEXT,"+ 
                     "CONTENT TEXT,"+ 
                     "PASSWORD INTEGER,"+ 
                     "TOTAL INTEGER,"+
                     "REALNAME TEXT)"; 
        Stmt.executeUpdate(query);         /*更新数据库*/
} 
catch (Exception e) {} 
out.println("恭喜，表连接创建成功！"); 
%> 
<%
ResultSet RS= Stmt.executeQuery("SELECT * FROM EMPLOYEE WHERE eage<25");     /*连接到数据库中的表EMPLOYEE上*/
String tt;                                                  /**号表示选择表中所有的列*/
String link;                                                /*若选择其中的某几列可以将*号换成列标号*/  
String makeStr;                                             /*WHERE语句表示条件选择*/
out.print("<table border=2 style=color:blue >");
while (RS.next()) {
  out.print("<tr><td>");
       tt = RS.getString("ename");              /*查询表中NUM字段的内容，字段内容可能使数组，默认列输出*/
       out.print(tt);
   out.print("</td><td>");
   tt = RS.getString("esex");
   out.print(tt);
   out.print("</td><td>");
   tt = RS.getString("eage");
   out.print(tt);
   out.print("</td><td>");
   tt=RS.getString("egrade");
   out.print(tt);
   out.print("</td><td>");
 } 
out.print("</table>");
RS.close();
out.println("数据查询成功！");
%>
<%                                                                        /*在数据库中添加数据*/
        String sql="INSERT INTO EMPLOYEE VALUES ('5','aaa','m',54,'hehe')"; 
        Stmt.executeUpdate(sql);
        out.println("添加数据成功!");
%>
</body>
</html>
