日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

获取数据库内容放入下拉框中

發布時間:2025/3/12 数据库 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 获取数据库内容放入下拉框中 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

獲取數據庫里的數據放入下拉框中,使下拉框顯示的內容是數據庫里的內容

功能分析:

  • 設計并實現數據庫
  • 插入相關數據
  • 在登陸頁面點擊注冊按鈕時跳到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>

    仔細觀看了解上述代碼之后快去實現它吧。

    獲取更多關注我呦!!!

    總結

    以上是生活随笔為你收集整理的获取数据库内容放入下拉框中的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。