标签:sys final generated 英文 hold com inpu asc text
这次测试是实现网页链接数据库,实现添加功能。其实我的代码没有什么问题(我把代码发给其它电脑测试就可以运行),但是一直出现404网页查找不到的问题,但是没有保任何的问题,只有一条警告(如下)。
我一开始以为是tomcat的配置有问题,就重新下载了最新的tomcat9,但还是出现这个原因。然后我把eclipse重装也是解决不了,最后重置了电脑把所有的配置重新安装,运行了一个最简单的登入注册的web项目,然后能够实现,那就不是tomcat的配置原因,然后就去测试能否连接上数据库,也能连接实现增添功能。然后我在一个博客里看到了有关jar包导入的文章。
将这两个jar文件拷贝到我们的web project中的lib文件夹下(没有这个文件的话,自己创建一个folder)
然后需要将我们的jar文件加到我们的路径下,这里注意一下,我们开发的是web project,除了将jar文件加到java中,还需要将路径加到tomcat服务器中,不然以后web project运行起来可能会出现:无法找到驱动的情况。路径加载如下:
a.java中导入jar1.右击要导入jar包的项目,点properties
2.左边选择java build path,右边选择libraries
3.选择add External jars 4.选择jar包的按照路径下的确定后就行了。
b.tomcat导入jar:project->properties-->myeclipse-->DeploymentAssembly-->选择需要复制到lib下的libraries说明:在这里jar包的导入,如果你自定义了一个lib文件,然后copy我们的jar包,使用a步骤导入还不够,也许你的数据库能够在控制台显示输出,但是在java web程序运行的时候会出现找不到驱动这样的情况,这是因为你没有在tomcat服务器下面导入jar,导致tomcat在运行的时候找不到驱动。
我发现是tomcat的服务器下没有导入jar包,导致tomcat在运行时找不到驱动。
我的代码如下:
首先是建一个user的包,在里面定义一个Ueserben的类,便于将网页上输入的数据添加到数据库中
package text.jsp.bean; public class UserBean { private String id; private String password; private String sex; private String name; private String number; private String mail; private String yuan; private String xi; private String classes; private String time; private String place; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getNumber() { return number; } public void setNumber(String number) { this.number = number; } public String getMail() { return mail; } public void setMail(String mail) { this.mail = mail; } public String getYuan() { return yuan; } public void setYuan(String yuan) { this.yuan = yuan; } public String getXi() { return xi; } public void setXi(String xi) { this.xi = xi; } public String getClasses() { return classes; } public void setClasses(String classes) { this.classes = classes; } public String getTime() { return time; } public void setTime(String time) { this.time = time; } public String getPlace() { return place; } public void setPlace(String place) { this.place = place; } public UserBean(String id,String password,String sex,String name,String number,String mail,String yuan,String xi,String classes,String time,String place){ this.id=id; this.password=password; this.sex=sex; this.name=name; this.number=number; this.mail=mail; this.yuan=yuan; this.xi=xi; this.classes=classes; this.time=time; this.place=place; } }
然后是建立数据库连接的包:
package text.jsp.util; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class DBUtil { private static Connection con; private static Statement stm; private static ResultSet rs; private static String classname="com.mysql.cj.jdbc.Driver"; private static String url="jdbc:mysql://localhost:3306/login?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT&&useSSL=false&&allowPublicKeyRetrieval=true"; public Connection getCon(){ try{ Class.forName(classname); System.out.println("驱动加载成功"); } catch(ClassNotFoundException e){ e.printStackTrace(); } try{ con=DriverManager.getConnection(url,"root","xp20010307.."); System.out.println("数据库连接成功"); } catch(Exception e){ e.printStackTrace(System.err); con=null; } return con; } public static void close(Statement stm, Connection conn) { if(stm!=null) { try { stm.close(); } catch (SQLException e) { e.printStackTrace(); } } if(conn!=null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } public static void close(ResultSet rs, Statement stm, Connection con) { if(rs!=null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if(stm!=null) { try { stm.close(); } catch (SQLException e) { e.printStackTrace(); } } if(con!=null) { try { con.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
然后是往数据库中实现添加功能的包
package text.jsp.dao; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import text.jsp.bean.UserBean; import text.jsp.util.DBUtil; public class UserDao { public boolean add(UserBean user) { String sql = "insert into text1021(id,password,sex,name,number,mail,yuan,xi,classes,time,place) values(‘" + user.getId() + "‘,‘" + user.getPassword() + "‘,‘" + user.getSex() + "‘,‘" + user.getName() + "‘,‘" + user.getNumber() + "‘,‘" + user.getMail() + "‘,‘" + user.getYuan() +"‘,‘"+user.getXi() + "‘,‘" +user.getClasses() + "‘,‘" +user.getTime() + "‘,‘" + user.getPlace()+"‘)"; DBUtil db=new DBUtil(); Connection conn = db.getCon();// ÷ Statement state = null; boolean f = false; int a = 0 ; try { state = conn.createStatement(); a = state.executeUpdate(sql); } catch (Exception e) { e.printStackTrace(); } finally { DBUtil.close(state, conn); } if (a > 0) { f = true; } return f; } }
还有一个是从网页中获取数据传递给dao包的包:
package text.jsp.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 text.jsp.dao.UserDao; import text.jsp.bean.UserBean; /** * Servlet implementation class textservlet */ @WebServlet("/textservlet") public class textservlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public textservlet() { super(); // TODO Auto-generated constructor stub } UserDao userDao=new UserDao(); /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); response.setContentType("textml;charset=UTF-8"); response.setHeader("content-type", "textml;charset=UTF-8"); String id=request.getParameter("gs1"); String password=request.getParameter("gs2"); String sex=request.getParameter("p"); String name=request.getParameter("gs4"); String number=request.getParameter("gs5"); String mail=request.getParameter("gs6"); String yuan=request.getParameter("gs7"); String xi=request.getParameter("gs8"); String classes=request.getParameter("gs9"); String time=request.getParameter("p2"); String place=request.getParameter("gs11"); UserBean userbean=new UserBean( id, password, sex, name, number, mail, yuan, xi, classes, time, place); if(userDao.add(userbean)) { request.getRequestDispatcher("success.jsp").forward(request,response); } else { request.getRequestDispatcher("fail.jsp").forward(request,response); } } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
然后是jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <form method="post" action="textservlet" onsubmit="return check()"> <div > <label for="gs1">登录账号:</label> <input type="text" id="gs1" placeholder="请输入用户名" name="gs1"> </div> <div > <label for="gs2">登录密码:</label> <input type="password" id="gs2" placeholder="请输入密码" class="input-text input-long" name="gs2"> </div> <div > <label for="gs3">姓 别:</label> <select name="p"> <option value ="nan">男</option> <option value ="nv">女</option> <select> </div> <div > <label for="gs4" >姓 名:</label> <input type="text" id="gs4" placeholder="请输入姓名" class="input-text input-long" name="gs4"> </div> <div > <label for="gs5" >学 号:</label> <input type="text" id="gs5" placeholder="请输入学号" class="input-text input-long" name="gs5"> </div> <div > <label for="gs6" >电子邮件:</label> <input type="text" id="gs6" placeholder="请输入邮件" class="input-text input-long" name="gs6"> </div> <div > <label for="gs7" >所在学院:</label> <input type="text" id="gs7" placeholder="请输入所在学院" class="input-text input-long" name="gs7"> </div> <div > <label for="gs8" >所在系:</label> <input type="text" id="gs8" placeholder="请输入所在系" class="input-text input-long" name="gs8"> </div> <div > <label for="gs9" >所在班级:</label> <input type="text" id="gs9" placeholder="请输入所在班级" class="input-text input-long" name="gs9"> </div> <div > <label for="gs10">入学年份(届):</label> <select name="p2"> <option value ="2014">2014</option> <option value ="2015">2015</option> <option value ="2016">2016</option> <option value ="2017">2017</option> <option value ="2018">2018</option> <option value ="2019">2019</option> <select> </div> <div > <label for="gs11" >生源地:</label> <input type="text" id="gs11" name="gs11"> </div> <div > <label for="gs12" >备注:</label> <input type="text" id="gs12" name="gs12"> </div> <div> <input type="submit" id="xuan" name="xuan" value="添加"> </div> </form> <!-- 输入字段验证部分 --> <script type="text/javascript"> function check(){ var username=document.getElementById("gs1"); var password=document.getElementById("gs2"); var number=document.getElementById("gs5"); var mail=document.getElementById("gs6"); var sReg = /[_a-zA-Z\d\-\.]+@[_a-zA-Z\d\-]+(\.[_a-zA-Z\d\-]+)+$/; //正则表达式 //判断用户名位数 if((username.value).length<6||(username.value).length>12){ alert(‘账号请输入6到12位英文字符或数字,以英文字母开头‘); gs1.focus(); return false; } //判断用户名是否包含汉字 if(/.*[\u4e00-\u9fa5]+.*$/.test(username.value)){ alert(‘账号用户名不能包含汉字‘); gs1.focus(); return false; } //判断用户名是否以英文字母开头 if(!isNaN(username.value[0])){ alert(‘登录账号请以英文字母开头‘); gs1.focus(); return false; } //判断密码位数 if((password.value).length!=8){ alert(‘密码应为8位英文或数字‘); gs2.focus(); return false; } //判断学号是否以2018开头 if(number.value<"20180000"|| number.value>"20189999") { alert(" 学号由2018开头的八位组成"); gs5.focus(); return false; } //判断邮箱格式是否正确 if(! sReg.test(mail.value)){ alert(‘邮箱格式错误‘); gs6.focus(); return false; } } </script> <!-- 验证结束 --> </body> </html>
标签:sys final generated 英文 hold com inpu asc text
原文地址:https://www.cnblogs.com/chenaiiu/p/11747357.html