標籤:
1. 添加資料
在jsp頁面中添加資料,和在serv中添加資料相似。獲得頁面中提交的資料以後,把資料儲存到資料庫表中,JSP的代碼如下:
add.jsp
<%@ page language="java" import="java.sql.*" pageEncoding="gb2312"%><%request.setCharacterEncoding("gb2312");String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <base href="<%=basePath%>"> <title>My JSP ‘add.jsp‘ starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <form action="" method="post"> <h1><label>請輸入部門資訊:</label></h1> <label>部門號:</label><br/> <input type="text" name="id"/><br/> <label>部門名:</label><br/> <input type="text" name="d_name"/><br/> <label>部門人數:</label><br/> <input type="text" name="empnumber"/><br/> <label>地址:</label><br/> <input type="text" name="address"/><br/><br/> <input type="submit" value="提交"/> </form> </body></html><% Connection conn = null; PreparedStatement ps = null; try{ Class.forName("com.mysql.jdbc.Driver"); System.out.println("建立資料庫驅動成功!"); conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bank","root","1234"); System.out.println("資料庫連接成功!"); String sql = "insert into dept(id,d_name,address,empnumber) values(?,?,?,?)"; ps = conn.prepareStatement(sql); String id = request.getParameter("id"); String d_name = request.getParameter("d_name"); String address = request.getParameter("address"); int empnumber = Integer.parseInt(request.getParameter("empnumber")); ps.setString(1,id); ps.setString(2,d_name); ps.setString(3,address); ps.setInt(4,empnumber); int result = ps.executeUpdate(); if(result == 1) out.print("插入資料成功!"); else out.print("插入資料失敗,請重新插入!"); }catch(Exception e){ out.println("無法串連資料庫,請檢查資料庫連接是否正確!"); }%>
2. 顯示全部資料
在頁面中顯示全部資料,也就是要把資料庫中的全部資料查詢出來,要實現這個功能,需要用到實體類,即資料庫和實體物件的映射類。實體類代碼如下:
DeptVo.java
package com.cn.vo;public class DeptVo { private String id; private String address; private int empnumber; private String d_name; private int d_id; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public int getEmpnumber() { return empnumber; } public void setEmpnumber(int empnumber) { this.empnumber = empnumber; } public String getD_name() { return d_name; } public void setD_name(String dName) { d_name = dName; } public int getD_id() { return d_id; } public void setD_id(int dId) { d_id = dId; }}
編寫好實體類型後,就可以在頁面中調用該類,在JSP頁面中編寫JDBC,串連資料庫和查詢資料,再用JSTL標籤庫中的c標籤遍曆輸出資料,使用EL運算式取值。JSP中的代碼如下:
showAll.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %><%@ page import="com.cn.vo.*" %><%@ page import="java.sql.*" %><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <base href="<%=basePath%>"> <title>My JSP ‘showAll.jsp‘ starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <% List<DeptVo> list = new ArrayList<DeptVo>(); try{ Class.forName("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/bank","root","1234"); Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery("SELECT * FROM dept"); while(rs.next()){ DeptVo deptVo = new DeptVo(); deptVo.setId(rs.getString("id")); deptVo.setAddress(rs.getString("address")); deptVo.setD_id(rs.getInt("d_id")); deptVo.setD_name(rs.getString("d_name")); deptVo.setEmpnumber(rs.getInt("empnumber")); list.add(deptVo); } request.setAttribute("list",list); //把list集合放入request對象中 }catch(Exception e){ e.printStackTrace(); } %> <body> <table border="1" align="center" width="70%"> <tr> <td>部門編號</td> <td>部門地址</td> <td>部門人數</td> <td>部門名稱</td> <td>部門id</td> </tr> <c:forEach items="${list}" var="list"> <tr> <td>${list.id }</td> <td>${list.address }</td> <td>${list.empnumber }</td> <td>${list.d_name }</td> <td>${list.d_id }</td> </tr> </c:forEach> </table> </body></html>
3. 顯示單條資料資訊
顯示單條資訊就是根據資料的唯一標示符來查詢出單條資料的詳細資料。在dept表中,主鍵d_id的值是自動成長的,不會有重複,可以根據d_id查詢出單條資料資訊。首先要在頁面中輸入要查詢的d_id值,然後根據d_id來查詢資料的詳細資料。輸入查詢條件的JSP檔案代碼如下:
item.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <base href="<%=basePath%>"> <title>My JSP ‘item.jsp‘ starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <form action="ShowById.jsp" method="post"> <label>請輸入部門id:</label><br/><br/> <input type="text" name="d_id"/><br/><br/> <input type="submit" value="尋找"/> </form> </body></html>
顯示在同一頁面中的ShowById.jsp檔案的代碼如下:
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%><%@ page import="com.cn.vo.*" %><%@ page import="java.sql.*" %><%@page import="javax.servlet.jsp.tagext.TryCatchFinally"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <base href="<%=basePath%>"> <title>My JSP ‘ShowById.jsp‘ starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <% int d_id = Integer.parseInt(request.getParameter("d_id")); Connection con = null; PreparedStatement pstmt = null; ResultSet rs = null; try{ Class.forName("com.mysql.jdbc.Driver"); System.out.println("建立驅動成功!"); con = DriverManager.getConnection("jdbc:mysql://localhost:3306/bank","root","1234"); System.out.println("資料庫連接成功!"); String sql = "select * from dept where d_id =?"; pstmt = con.prepareStatement(sql); pstmt.setInt(1,d_id); rs = pstmt.executeQuery(); while(rs.next()){ DeptVo deptVo = new DeptVo(); deptVo.setId(rs.getString("id")); deptVo.setAddress(rs.getString("address")); deptVo.setD_id(rs.getInt("d_id")); deptVo.setD_name(rs.getString("d_name")); deptVo.setEmpnumber(rs.getInt("empnumber")); request.setAttribute("deptVo",deptVo); System.out.println(deptVo.getD_id()); } }catch(Exception e){ e.printStackTrace(); } %> <body> <jsp:include flush="true" page="item.jsp"></jsp:include> <hr/> <h2>d_id值為<%=d_id%>的資料詳細資料</h2> <table border="1" align="center" width="70%"> <tr> <td>部門編號</td> <td>部門地址</td> <td>部門人數</td> <td>部門名稱</td> <td>部門id</td> </tr> <tr> <td>${deptVo.id }</td> <td>${deptVo.address }</td> <td>${deptVo.empnumber }</td> <td>${deptVo.d_name }</td> <td>${deptVo.d_id }</td> </tr> </table> </body></html>
JDBC結合JSP使用(1)