天天看点

JSP+Servlet+JavaBean+JDBC

tables.jsp

  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

  2. <%@ page import="java.sql.DriverManager" %>

  3. <%@ page import="java.sql.PreparedStatement" %>

  4. <%@ page import="java.sql.ResultSet" %>

  5. <%@ page import="java.sql.SQLException" %>

  6. <%@ page import="java.sql.Connection" %>

  7. <%@ page import="java.sql.Statement" %>

  8. <%@ page import="model.User" %>

  9. <%@ page import="javax.servlet.http.HttpServletRequest" %>

  10. <!DOCTYPE html>

  11. <html lang="en">

  12. <%@ include file="menu.jsp" %>

  13. <body>

  14. <div id="content">

  15. <div id="content-header">

  16. <h1>Tables</h1>

  17. <div class="btn-group">

  18. <a class="btn btn-large tip-bottom" title="Manage Files"><i class="icon-file"></i></a>

  19. <a class="btn btn-large tip-bottom" title="Manage Users"><i class="icon-user"></i></a>

  20. <a class="btn btn-large tip-bottom" title="Manage Comments"><i class="icon-comment"></i><span class="label label-important">5</span></a>

  21. <a class="btn btn-large tip-bottom" title="Manage Orders"><i class="icon-shopping-cart"></i></a>

  22. </div>

  23. </div>

  24. <div id="breadcrumb">

  25. <a href="#" title="Go to Home" class="tip-bottom"><i class="icon-home"></i> Home</a>

  26. <a href="#" class="current">Tables</a>

  27. </div>

  28. <div class="container-fluid">

  29. <div class="row-fluid">

  30. <div class="span12">

  31. <div class="widget-box">

  32. <div class="widget-title">

  33. <h5>Dynamic table</h5>

  34. </div>

  35. <div class="widget-content nopadding">

  36. <table class="table table-bordered data-table">

  37. <thead>

  38. <tr>

  39. <th>ID</th>

  40. <th>USER</th>

  41. <th>NAME</th>

  42. <th>TEL</th>

  43. </tr>

  44. </thead>

  45. <tbody>

  46. <%

  47. List<User> list = new ArrayList<User>();

  48. list = (List<User>)request.getAttribute("list");

  49. for (User user : list){

  50. %>

  51. <tr>

  52. <td><%=user.getId() %></td>

  53. <td><%=user.getAccount() %></td>

  54. <td><%=user.getUsername() %></td>

  55. <td><%=user.getTel() %></td>

  56. </tr>

  57. <%} %>

  58. </tbody>

  59. </table>

  60. </div>

  61. </div>

  62. </div>

  63. </div>

  64. <div class="row-fluid">

  65. <div id="footer" class="span12">

  66. 2012 &copy; UniAdmin.</div>

  67. </div>

  68. </div>

  69. </div>

  70. <script src="js/jquery.min.js"></script>

  71. <script src="js/jquery.ui.custom.js"></script>

  72. <script src="js/bootstrap.min.js"></script>

  73. <script src="js/jquery.uniform.js"></script>

  74. <script src="js/select2.min.js"></script>

  75. <script src="js/jquery.dataTables.min.js"></script>

  76. <script src="js/unicorn.js"></script>

  77. <script src="js/unicorn.tables.js"></script>

  78. </body>

  79. </html>

web.xml

  1. <?xml version="1.0" encoding="UTF-8"?>

  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">

  3. <display-name>web_project</display-name>

  4. <welcome-file-list>

  5. <welcome-file>welcome.jsp</welcome-file>

  6. </welcome-file-list>

  7. <servlet>

  8. <servlet-name>FindServlet</servlet-name>

  9. <servlet-class>servlet.FindServlet</servlet-class>

  10. </servlet>

  11. <servlet-mapping>

  12. <servlet-name>FindServlet</servlet-name>

  13. <url-pattern>/FindServlet</url-pattern>

  14. </servlet-mapping>

  15. </web-app>

FindSerlvet.java

  1. package servlet;

  2. import java.io.IOException;

  3. import java.sql.Connection;

  4. import java.sql.DriverManager;

  5. import java.sql.PreparedStatement;

  6. import java.sql.ResultSet;

  7. import java.sql.SQLException;

  8. import java.util.ArrayList;

  9. import java.util.List;

  10. import javax.servlet.ServletException;

  11. import javax.servlet.annotation.WebServlet;

  12. import javax.servlet.http.HttpServlet;

  13. import javax.servlet.http.HttpServletRequest;

  14. import javax.servlet.http.HttpServletResponse;

  15. import model.User;

  16. public class FindServlet extends HttpServlet {

  17. private static final long serialVersionUID = 1L;

  18. public FindServlet() {

  19. super();

  20. }

  21. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

  22. Connection conn = null;

  23. PreparedStatement ps = null;

  24. ResultSet resultSet = null;

  25. String sql;

  26. // MySQL的JDBC URL编写方式:jdbc:mysql://主机名称:连接端口/数据库的名称?参数=值

  27. // 避免中文乱码要指定useUnicode和characterEncoding

  28. // 执行数据库操作之前要在数据库管理系统上创建一个数据库,名字自己定,

  29. // 下面语句之前就要先创建javademo数据库

  30. String url = "jdbc:mysql://localhost:3306/s61?"

  31. + "user=root&password=&useUnicode=true&characterEncoding=UTF8";

  32. try {

  33. // 之所以要使用下面这条语句,是因为要使用MySQL的驱动,所以我们要把它驱动起来,

  34. // 可以通过Class.forName把它加载进去,也可以通过初始化来驱动起来,下面三种形式都可以

  35. Class.forName("com.mysql.jdbc.Driver");// 动态加载mysql驱动

  36. System.out.println("成功加载MySQL驱动程序");

  37. // 一个Connection代表一个数据库连接

  38. conn = DriverManager.getConnection(url);

  39. // Statement里面带有很多方法,比如executeUpdate可以实现插入,更新和删除等

  40. sql = "select a.F01 as id,a.F02 as user,b.F02 as name,a.F04 as tel from s61.t6110 a,s61.t6141 b where a.F01=b.F01 ";

  41. ps = conn.prepareStatement(sql);

  42. resultSet = ps.executeQuery();

  43. Integer id = null;

  44. String account = null;

  45. String name = null;

  46. String tel = null;

  47. List<User> list = new ArrayList<User>();

  48. while (resultSet.next()){

  49. User myuser = new User();

  50. id = resultSet.getInt(1);

  51. myuser.setId(id);

  52. account = resultSet.getString(2);

  53. myuser.setAccount(account);

  54. name = resultSet.getString(3);

  55. myuser.setUsername(name);

  56. tel = resultSet.getString(4);

  57. myuser.setTel(tel);

  58. list.add(myuser);

  59. }

  60. request.setAttribute("list", list);

  61. } catch (SQLException e) {

  62. System.out.println("MySQL操作错误");

  63. e.printStackTrace();

  64. } catch (Exception e) {

  65. e.printStackTrace();

  66. } finally {

  67. try {

  68. resultSet.close();

  69. ps.close();

  70. conn.close();

  71. } catch (SQLException e) {

  72. e.printStackTrace();

  73. }

  74. }

  75. request.getRequestDispatcher("tables.jsp")

  76. .forward(request, response);

  77. }

  78. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

  79. doGet(request, response);

  80. }

  81. }

User.java

  1. package model;

  2. public class User {

  3. private Integer id;

  4. private String account;

  5. private String username;

  6. private String tel;

  7. public void setId(Integer id){

  8. this.id = id;

  9. }

  10. public Integer getId(){

  11. return id;

  12. }

  13. public void setAccount(String account){

  14. this.account = account;

  15. }

  16. public String getAccount(){

  17. return account;

  18. }

  19. public void setUsername(String username){

  20. this.username = username;

  21. }

  22. public String getUsername(){

  23. return username;

  24. }

  25. public void setTel(String tel){

  26. this.tel = tel;

  27. }

  28. public String getTel(){

  29. return tel;

  30. }

  31. }

来自为知笔记(Wiz)

转载于:https://www.cnblogs.com/tangbinghaochi/p/6293150.html