这次测试我们尝试的编辑了河北省重大技术需求证据系统,程序需求用户注册,用户登陆,系统主机面,需求征集,浏览需求,需求审核,修改密码等功能,并且需要进行数据库的连接,这些内容大部分在上一学期就有了些许的掌握,但在课堂测试的过程中,我还是只完成了部分功能,我只满足了用户注册,用户登陆,需求征集,修改密码这几个功能,并与数据库正常连接,图形化界面也并没有完成,下面我将展示我的代码。
//Dao.java package Dao; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import Entity.User; import Entity.jishu; import DB.DB; public class Dao { public String denglu(String username) { String sql = "select * from yonghu where "; if (username != "") { sql += "name like ‘%" + username + "%‘"; } Connection conn = DB.getConn(); Statement state = null; ResultSet rs = null; String password2 = null; try { state = conn.createStatement(); rs = state.executeQuery(sql); while (rs.next()) { password2 = rs.getString("password"); } } catch (SQLException e) { e.printStackTrace(); } finally { DB.close(rs, state, conn); } return password2; } public boolean zhuce(User user) { String sql = "insert into yonghu(name, password,tel,danwei) values(‘" + user.getName() + "‘,‘" + user.getPassword()+ "‘,‘" + user.getTel()+ "‘,‘" + user.getDanwei() + "‘)"; // 鍒涘缓鏁版嵁搴撻摼鎺? Connection conn = DB.getConn(); Statement state = null; boolean f = false; int a = 0; try { state = conn.createStatement(); a = state.executeUpdate(sql); } catch (Exception e) { e.printStackTrace(); } finally { // 鍏抽棴z 杩炴帴 DB.close(state, conn); } if (a > 0) { f = true; } return f; } public boolean zhengji(jishu js) { String sql = "insert into jishu(name, info,val,moshi,touzi) values(‘" + js.getName() + "‘,‘" + js.getInfo()+ "‘,‘" + js.getValues()+ "‘,‘" + js.getMoshi()+ "‘,‘" + js.getTouzi() + "‘)"; // 鍒涘缓鏁版嵁搴撻摼鎺? Connection conn = DB.getConn(); Statement state = null; boolean f = false; int a = 0; try { state = conn.createStatement(); a = state.executeUpdate(sql); } catch (Exception e) { e.printStackTrace(); } finally { // 鍏抽棴z 杩炴帴 DB.close(state, conn); } if (a > 0) { f = true; } return f; } }
//DB.java package DB; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class DB { public static String db_url = "jdbc:mysql://localhost:3306/course"; public static String db_user = "root"; public static String db_pass = "z376371066."; public static Connection getConn () { Connection conn = null; try { Class.forName("com.mysql.jdbc.Driver");//鍔犺浇椹卞姩 conn = DriverManager.getConnection(db_url, db_user, db_pass); } catch (Exception e) { e.printStackTrace(); } return conn; } /** * 鍏抽棴杩炴帴 * @param state * @param conn */ public static void close (Statement state, Connection conn) { if (state != null) { try { state.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } public static void close (ResultSet rs, Statement state, Connection conn) { if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if (state != null) { try { state.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } public static void main(String[] args) throws SQLException { Connection conn = getConn(); PreparedStatement pstmt = null; ResultSet rs = null; String sql ="select * from USER"; pstmt = conn.prepareStatement(sql); rs = pstmt.executeQuery(); if(rs.next()){ System.out.println("绌?"); }else{ System.out.println("涓嶇┖"); } } }
//jishu.java package Entity; public class jishu { private String name; private String info; private String values; private String moshi; private String touzi; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getInfo() { return info; } public void setInfo(String info) { this.info = info; } public String getValues() { return values; } public void setValues(String values) { this.values = values; } public String getMoshi() { return moshi; } public void setMoshi(String moshi) { this.moshi = moshi; } public String getTouzi() { return touzi; } public void setTouzi(String touzi) { this.touzi = touzi; } public jishu(String name,String info,String values,String moshi,String touzi) { this.name = name; this.info = info; this.values = values; this.moshi = moshi; this.touzi = touzi; } }
//User.java package Entity; public class User { private String name; private String password; private String tel; private String danwei; public String getTel() { return tel; } public void setTel(String tel) { this.tel = tel; } public String getDanwei() { return danwei; } public void setDanwei(String danwei) { this.danwei = danwei; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public User(String name,String password) { this.name = name; this.password = password; } public User(String name,String password,String tel,String danwei) { this.name = name; this.password = password; this.tel = tel; this.danwei = danwei; } }
//Servlt.java package Servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import Dao.Dao; import Entity.User; import Entity.jishu; /** * Servlet implementation class Servlet */ @WebServlet("/Servlet") public class Servlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public Servlet() { super(); // TODO Auto-generated constructor stub } Dao dao = new Dao(); protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("utf-8"); String method = req.getParameter("method"); if ("denglu".equals(method)) { denglu(req, resp); } else if ("zhuce".equals(method)) { zhuce(req, resp); } else if ("zhengji".equals(method)) { zhengji(req, resp); } } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ private void denglu(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { // TODO Auto-generated method stub req.setCharacterEncoding("utf-8"); String username = req.getParameter("username"); String password = req.getParameter("password"); String rpassword = dao.denglu(username); if(password.equals(rpassword)) { req.setAttribute("message", "鐧婚檰鎴愬姛锛?"); req.getRequestDispatcher("index.jsp").forward(req,resp); } else { req.setAttribute("message", "璐﹀彿涓嶅瓨鍦ㄦ垨瀵嗙爜閿欒锛?"); req.getRequestDispatcher("login.jsp").forward(req,resp); } } private void zhuce(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { // TODO Auto-generated method stub String username = req.getParameter("username"); String password = req.getParameter("password"); String tel = req.getParameter("tel"); String danwei = req.getParameter("danwei"); User user= new User(username,password,tel,danwei); if(dao.zhuce(user)) { req.setAttribute("message", "娉ㄥ唽鎴愬姛!"); req.getRequestDispatcher("login.jsp").forward(req, resp); }else { req.setAttribute("message", "璐﹀彿閲嶅锛岃閲嶆柊杈撳叆!"); req.getRequestDispatcher("zhuce.jsp").forward(req, resp); } } private void zhengji(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { // TODO Auto-generated method stub String name = req.getParameter("name"); String info = req.getParameter("info"); String[] values = req.getParameterValues("type"); String moshi = req.getParameter("moshi"); String touzi = req.getParameter("touzi"); String delStr = ""; for(int i=0 ;i < values.length ; i ++) delStr += values[i] +" "; jishu js = new jishu(name,info,delStr,moshi,touzi); if(dao.zhengji(js)) { req.setAttribute("message", "闇?姹傚緛闆嗗~鍐欐垚鍔?!"); req.getRequestDispatcher("index.jsp").forward(req, resp); }else { req.setAttribute("message", "闇?姹傚緛闆嗗~鍐欏け璐ワ紒"); req.getRequestDispatcher("register.jsp").forward(req, resp); } } }
//index.jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <% Object message = request.getAttribute("message");//放置一个字符串,并取出 if(message!=null && !"".equals(message)){ %> <script type="text/javascript"> alert("<%=request.getAttribute("message")%>"); </script> <%} %> <h2 align="center">主界面</h2> <p style="text-align:center" ><a href="login.jsp"> <font size="4">返回登陆界面</font></a></p> <div align="center"> <div class="a"> <p style="text-align:center"><a href="register.jsp">1.需求征集</a></p> </div> <div class="a"> <p style="text-align:center"><a href="Servlet?method=list">2.浏览需求</a></p> </div> <div class="a"> <p style="text-align:center"><a href="">3.需求审核</a></p> </div> <div class="a"> <p style="text-align:center"><a href="zhuce.jsp">4.用户注册</a></p> </div> <div class="a"> <p style="text-align:center"><a href="change.jsp">5.修改密码</a></p> </div> </div> </body> </html>
//login.jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <% Object message = request.getAttribute("message");//放置一个字符串,并取出 if(message!=null && !"".equals(message)){ %> <script type="text/javascript"> alert("<%=request.getAttribute("message")%>"); </script> <%} %> <div align="center"> <h1 style="color: black;">河北省重大技术需求</h1> <h1 style="color: black;">征集系统</h1> <form action="Servlet?method=denglu" method="post" onsubmit="return check()"> <div class="a"> <p style="text-align:center">账号<input type="text" size="20" id="username" name="username"/></p> </div> <div class="a"> <p style="text-align:center">密码<input type="password" size="20" id="password" name="password" /></p> </div> <div class="a"> <p style="text-align:center">验证码<input type="text" size="20" id="yanzhengma" name="yanzhengma"/></p> </div> <div class="a"> <button type="submit" class="b">登 陆</button> </div> </form> </div> <script type="text/javascript"> function check() { var username = document.getElementById("username");; var password = document.getElementById("password"); //非空 if(username.value == ‘‘) { alert(‘账号不能为空‘); username.focus(); return false; } if(password.value == ‘‘) { alert(‘密码不能为空‘); password.focus(); return false; } } </script> <p style="text-align:center"><a href="zhuce.jsp"><font SIZE="6" >注册</font></a></p> </body> </html>
//zhuce.jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <% Object message = request.getAttribute("message");//放置一个字符串,并取出 if(message!=null && !"".equals(message)){ %> <script type="text/javascript"> alert("<%=request.getAttribute("message")%>"); </script> <%} %> <div align="center"> <h1 style="color: black;">注册界面</h1> <a href="login.jsp">返回登陆界面</a> <form action="Servlet?method=zhuce" method="post" onsubmit="return check()"> <div class="a"> 姓名<input type="text" id="username" name="username"/> </div> <div class="a"> 密码<input type="password" id="password" name="password" /> </div> <div class="a"> 重复密码<input type="password" id="rpassword" name="rpassword" /> </div> <div class="a"> 手机号码<input type="text" id="tel" name="tel"/> </div> <div class="a"> 所属单位<input type="text" id="danwei" name="danwei"/> </div> <div class="a"> <button type="submit" class="b">注 册</button> </div> </form> </div> <script type="text/javascript"> function check() { var username = document.getElementById("username");; var password = document.getElementById("password"); var rpassword = document.getElementById("rpassword"); var tel = document.getElementById("tel"); var danwei = document.getElementById("danwei"); //非空 if(username.value == ‘‘) { alert(‘账号不能为空‘); username.focus(); return false; } if(password.value == ‘‘) { alert(‘密码不能为空‘); password.focus(); return false; } if(rpassword.value == ‘‘) { alert(‘确认密码不能为空‘); rpassword.focus(); return false; } if(rpassword.value!= password.value) { alert(‘两次密码不一致‘); rpassword.focus(); return false; } if(tel.value == ‘‘) { alert(‘手机号不能为空‘); tel.focus(); return false; } if(tel.length()!=11) { alert(‘手机号码必须为11位‘); tel.focus(); return false; } if(danwei.value == ‘‘) { alert(‘所属单位不能为空‘); danwei.focus(); return false; } } </script> </body> </html>
//register.jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <body> <% Object message = request.getAttribute("message");//放置一个字符串,并取出 if(message!=null && !"".equals(message)){ %> <script type="text/javascript"> alert("<%=request.getAttribute("message")%>"); </script> <%} %> <div align="center"> <h1 style="color: black;">河北省重大技术需求征集表</h1> <a href="index.jsp">返回主界面</a> <form action="Servlet?method=zhengji" method="post" > <div class="a"> 技术需求名称<input type="text" id="name" name="name"/> </div> <div class="a"> <textarea name="info" cols="35" rows="7">重大科技需求概述</textarea> </div> <div class="a"> 研究类型<br /><br /> <label><input name="type" type="checkbox" value="基础研究" />基础研究 </label> <label><input name="type" type="checkbox" value="应用研究" />应用研究 </label> <label><input name="type" type="checkbox" value="试验发展" />试验发展 </label> <label><input name="type" type="checkbox" value="研究发展与成果应用" />研究发展与成果应用 </label> <label><input name="type" type="checkbox" value="技术推广与科技服务" />技术推广与科技服务 </label> </div> <div class="a"> <p>技术需求合作模式<input name = "moshi" type = "radio" value="独立开发">独立开发<input name = "moshi" type = "radio" value="技术转让">技术转让<input name = "moshi" type = "radio" value="技术入股">技术入股<input name = "moshi" type = "radio" value="合作开发">合作开发</p> </div> <div class="a"> <p>计划总投资<input type="text" size="20" id="touzi" name="touzi"/>万元</p> </div> <div class="a"> <button type="submit" class="b">提 交</button> </div> </form> </div> </body> </html>
原文地址:https://www.cnblogs.com/my---world/p/10465843.html
时间: 2024-11-08 01:21:56