获取数据库里的数据放入下拉框中,使下拉框显示的内容是数据库里的内容
功能分析:
- 设计并实现数据库
- 插入相关数据
- 在登陆页面点击注册按钮时跳到Servlet中
- 在Servlet中连接数据库
- 查询内容放入session中传给jsp页面
- 在jsp页面接受session内容并使用for循环输出
效果图演示
登陆页面
点击注册按钮之后(下拉框的数据是从SQLServer数据库里获取的数据)
看了上述演示有没有一点心动的感觉呢???
下面跟随我一起来探究一下代码吧
数据库
(我的数据库名为Select,数据表名为people)
数据库内容展示
在正式看代码之前还是要先看一下**目录结构**的
代码演示
index.jsp代码
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
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 'index.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">账号:<input type="text" name="name"><br>密码:<input type="password" name="pwd"><br><input type="submit" value="登录"><a href="toRegister">注册</a></form></body>
</html>
util包里的DBUtil.java代码
package com.zsh.util;import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;public class DBUtil {public static Connection getConn(){String url = "jdbc:sqlserver://localhost:1433;databaseName=Select";String user = "sa";String pwd = "1";Connection conn = null;try {Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");conn = DriverManager.getConnection(url, user, pwd);} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}return conn;}public static void closeConn(Connection conn, PreparedStatement ps, ResultSet rs){if(conn!=null){try {conn.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if(ps!=null){try {ps.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if(rs!=null){try {rs.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}
bean包里的Department.java代码
package com.zsh.bean;public class Department {private int id;private String name;private String mark;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getMark() {return mark;}public void setMark(String mark) {this.mark = mark;}public Department() {super();// TODO Auto-generated constructor stub}public Department(int id, String name, String mark) {super();this.id = id;this.name = name;this.mark = mark;}}
servlet包里的ToRegisterServlet.Java代码
package com.zsh.servlet;import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;import com.zsh.bean.Department;
import com.zsh.util.DBUtil;public class ToRegisterServlet extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doPost(request, response);}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {request.setCharacterEncoding("utf-8");response.setContentType("text/html");Connection conn = DBUtil.getConn();PreparedStatement ps = null;ResultSet rs = null;String sql = "select * from people";List<Department> depts = new ArrayList<Department>();try {ps = conn.prepareStatement(sql);rs = ps.executeQuery();while(rs.next()){Department dept = new Department();dept.setId(rs.getInt(1));dept.setName(rs.getString(2));depts.add(dept);}} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{DBUtil.closeConn(conn, ps, rs);}HttpSession session = request.getSession();session.setAttribute("deptList", depts);response.sendRedirect("register.jsp");}}
register.jsp代码
<%@page import="com.zsh.bean.Department"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
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 'register.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="register" method="post">部门:<select name="dep"><%List<Department> depts = (List)session.getAttribute("deptList");for(Department dept : depts){%><option value="<%=dept.getId() %>"><%=dept.getName() %></option><%}%><option></option></select><input type="submit" value="注册"></form></body>
</html>
仔细观看了解上述代码之后快去实现它吧。
获取更多关注我呦!!!