获取数据库内容放入下拉框中
生活随笔
收集整理的這篇文章主要介紹了
获取数据库内容放入下拉框中
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
獲取數(shù)據(jù)庫里的數(shù)據(jù)放入下拉框中,使下拉框顯示的內(nèi)容是數(shù)據(jù)庫里的內(nèi)容
功能分析:
效果圖演示
登陸頁面
點擊注冊按鈕之后(下拉框的數(shù)據(jù)是從SQLServer數(shù)據(jù)庫里獲取的數(shù)據(jù))
看了上述演示有沒有一點心動的感覺呢???
下面跟隨我一起來探究一下代碼吧
數(shù)據(jù)庫
(我的數(shù)據(jù)庫名為Select,數(shù)據(jù)表名為people)
數(shù)據(jù)庫內(nèi)容展示
在正式看代碼之前還是要先看一下**目錄結(jié)構(gòu)**的
代碼演示
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>仔細觀看了解上述代碼之后快去實現(xiàn)它吧。
獲取更多關(guān)注我呦!!!
總結(jié)
以上是生活随笔為你收集整理的获取数据库内容放入下拉框中的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Vue 金额计算
- 下一篇: mysql并发 node_nodejs写