jsp连接Sql2008--查询操作

<%@ page language="java" contentType="text/html; charset=gb2312"
 pageEncoding="gb2312" import="java.sql.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>添加数据</title>
</head>
<body>
 <center>
  <table border=1 cellspacing="1">
   <tr>
    <th>id</th>
    <th>name</th>
    <th>age</th>
    <th>address</th>
    <th>city</th>
   </tr>
   <%
    String url = "jdbc:sqlserver://localhost:1433; DatabaseName=JavaJdbc";
    String user = "sa";
    String password = "110";
    Connection conn = null;
    PreparedStatement pstmt = null;
    String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
    try {
      Class.forName(driverName).newInstance();
     conn = DriverManager.getConnection(url, user, password);
    } catch (ClassNotFoundException e) {
     out.println(e);
    } catch (SQLException e) {
     out.println("连接SQLserver数据库失败!!!!!!");
    }
    
    
    
    
    Statement stmt = conn.createStatement();
    ResultSet rs = stmt.executeQuery("select * from employee");
    while (rs != null && rs.next()) {
     out.print("<tr><td>" + rs.getInt("id") + "</td>");
     out.print("<td>" + rs.getString("name") + "</td>");
     out.print("<td>" + rs.getInt("age") + "</td>");
     out.print("<td>" + rs.getString("address") + "</td>");
     out.print("<td>" + rs.getString("city") + "</td></tr>");
    }

   %>
  </table>
 </center>
</body>
</html>

posted @ 2012-11-13 09:02  W&L  阅读(198)  评论(0编辑  收藏  举报