怎么做蛋糕店的网站,seo怎么发文章 seo发布工具,网页编辑用什么软件,建设网站需要懂什么获取数据库里的数据放入下拉框中#xff0c;使下拉框显示的内容是数据库里的内容
功能分析#xff1a;
设计并实现数据库插入相关数据在登陆页面点击注册按钮时跳到Servlet中在Servlet中连接数据库查询内容放入session中传给jsp页面在jsp页面接受session内容并使用for循环输…获取数据库里的数据放入下拉框中使下拉框显示的内容是数据库里的内容
功能分析
设计并实现数据库插入相关数据在登陆页面点击注册按钮时跳到Servlet中在Servlet中连接数据库查询内容放入session中传给jsp页面在jsp页面接受session内容并使用for循环输出
效果图演示
登陆页面 点击注册按钮之后下拉框的数据是从SQLServer数据库里获取的数据 看了上述演示有没有一点心动的感觉呢
下面跟随我一起来探究一下代码吧
数据库
我的数据库名为Select数据表名为people 数据库内容展示 在正式看代码之前还是要先看一下**目录结构**的
代码演示
index.jsp代码
% page languagejava importjava.util.* pageEncodingutf-8%
%
String path request.getContextPath();
String basePath request.getScheme()://request.getServerName():request.getServerPort()path/;
%!DOCTYPE HTML PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN
htmlheadbase href%basePath%titleMy JSP index.jsp starting page/titlemeta http-equivpragma contentno-cachemeta http-equivcache-control contentno-cachemeta http-equivexpires content0 meta http-equivkeywords contentkeyword1,keyword2,keyword3meta http-equivdescription contentThis is my page!--link relstylesheet typetext/css hrefstyles.css--/headbodyform action methodpost账号input typetext namenamebr密码input typepassword namepwdbrinput typesubmit value登录a hreftoRegister注册/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;databaseNameSelect;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;ListDepartment depts new ArrayListDepartment();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 importcom.zsh.bean.Department%
% page languagejava importjava.util.* pageEncodingutf-8%
%
String path request.getContextPath();
String basePath request.getScheme()://request.getServerName():request.getServerPort()path/;
%!DOCTYPE HTML PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN
htmlheadbase href%basePath%titleMy JSP register.jsp starting page/titlemeta http-equivpragma contentno-cachemeta http-equivcache-control contentno-cachemeta http-equivexpires content0 meta http-equivkeywords contentkeyword1,keyword2,keyword3meta http-equivdescription contentThis is my page!--link relstylesheet typetext/css hrefstyles.css--/headbodyform actionregister methodpost部门select namedep%ListDepartment depts (List)session.getAttribute(deptList);for(Department dept : depts){%option value%dept.getId() %%dept.getName() %/option%}%option/option/selectinput typesubmit value注册/form/body
/html
仔细观看了解上述代码之后快去实现它吧。
获取更多关注我呦