天天看點

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