无法使用jsp将数据插入到mysql表中

时间:2016-03-26 15:16:06

标签: mysql jsp

这是我的html代码“register-1.jsp”

enter code here    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"     "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head> 
<title> Register </title>
</head>
<body>
  <div style="height:70%;width:100%">
   <img src="back.png">

   <br><br><br><br>
   <form action="insert.jsp" method="post">
   <div style="height:200px;width:200px;float:left;clear:none;position:relative;margin-top:-500px;margin-left:200px">
 <ul style="list-style:none;font-size:30px;font-family:Berlin Sans FB">
 <li> First Name: </li>
 <li> Last Name: </li>
 <li> Email: </li>
 <li> Phone no:</li>
 <li> Username: </li>
 <li> DOB: </li>
 <li> Gender: </li>
 <li> Password: </li>
 <li> Confirm Password: </li>
</ul>
</div>
<div style="height:200px;width:400px;float:right;margin-right:500px;clear:none;position:relative;margin-top:-500px">
<ul style="list-style:none;font-size:28px">
 <li> <input type="text" name="fname"> </li>
 <li> <input type="text" name="lname"> </li>
 <li> <input type="text" name="email"> </li>
 <li> <input type="text" name="phone"> </li>
 <li> <input type="text" name="username"> </li>
 <li> <select name="day">
      <option value="1">1</option>
      <option value="2">2</option>
        <option value="3">3</option>
        <option value="4">4</option>
        <option value="5">5</option>
        <option value="6">6</option>
        <option value="7">7</option>
        <option value="8">8</option>
        <option value="9">9</option>
        <option value="10">10</option>
        <option value="11">11</option>
        <option value="12">12</option>
        <option value="13">13</option>
        <option value="14">14</option>
        <option value="15">15</option>
        <option value="16">16</option>
        <option value="17">17</option>
        <option value="18">18</option>
        <option value="19">19</option>
        <option value="20">20</option>
        <option value="21">21</option>
        <option value="22">22</option>
        <option value="23">23</option>
        <option value="24">24</option>
        <option value="25">25</option>
        <option value="26">26</option>
        <option value="27">27</option>
        <option value="28">28</option>
        <option value="29">29</option>
        <option value="30">30</option>
        <option value="31">31</option>
        </select> 
        <select name="month">
        <option value="Jan">Jan</option>
        <option value="Feb">Feb</option>
        <option value="Mar">Mar</option>
        <option value="Apr">Apr</option>
        <option value="May">May</option>
        <option value="June">June</option>
        <option value="July">July</option>
        <option value="Aug">Aug</option>
        <option value="Sept">Sept</option>
        <option value="Oct">Oct</option>
        <option value="Nov">Nov</option>
        <option value="Dec">Dec</option>
        </select>
        <select name="year">
         <option value="1990">1990</option>
         <option value="1991">1991</option>
         <option value="1992">1993</option>
         <option value="1994">1994</option>
         <option value="1995">1995</option>
         <option value="1996">1996</option>
         <option value="1997">1997</option>
         <option value="1998">1998</option>
         <option value="1999">1999</option>
         <option value="2000">2000</option>
         </select>
        </li>
        <li style="font-size:20px;padding:5px 5px 3px 5px"> <input type="radio" value="Male" name="gender">Male 
             <input type="radio" value="Female">Female
        </li>
        <li> <input type="password" name="temp"> </li>
        <li> <input type="password" name="password"></li>
    </ul>
 </div>
 <br>
 <p style="clear:both;margin-left:350px;margin-top:-150px"> <input type="submit" value="Submit" style="font-size:23px;background-color:#aaaaaa"></p>
 </form>
 </div>
 </body>
</html>

这是我的“insert.jsp”文件

enter code here  <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
  <%@page import="java.sql.*,java.io.*,java.lang.String,java.util.*" %>
  <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"      "http://www.w3.org/TR/html4/loose.dtd">
<%
 String site = "" ;
 Connection conn;
 Statement st;

  ResultSet rs;
  String fname=request.getParameter("fname");

  String lname=request.getParameter("lname");

  String email=request.getParameter("email");

  String phone=request.getParameter("phone");

  String username=request.getParameter("username");
  String gender=request.getParameter("gender");

  String password=request.getParameter("password");

  String temp=request.getParameter("temp");

  String day=request.getParameter("day");

  String month=request.getParameter("month");

  String year=request.getParameter("year");
  int i=0;
  try{
    Class.forName("com.mysql.jdbc.Driver");
    conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/dbms_project","root","kanishk");
  st=conn.createStatement();
  if(temp.compareTo(password)==0)
  {
     i=st.executeUpdate("insert into   signup(fname,lname,email,phone,username,gender,password,day,month,year) values('"+fname+"','"+lname+"','"+email+"','"+phone+",'"+username+"','"+gender+"','"+password+"','"+day+"','"+month+"','"+year+")");
  }

  if(i>0){
       site = "http://www.google.com" ;
       response.setStatus(response.SC_MOVED_TEMPORARILY);
       response.setHeader("Location", site); 
       System.out.println(i);
   }
  else
  { 
   System.out.println(i);
  }
 }
 catch(Exception e)
  {
    e.printStackTrace();
  }
 %>

这是我找到的日志:

 Mar 30, 2016 4:45:00 PM org.apache.catalina.core.AprLifecycleListener init
 INFO: The APR based Apache Tomcat Native library which allows optimal    performance in production environments was not found on the java.library.path:   C:\Program Files   (x86)\Java\jre7\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:/Program Files (x86)/Java/jre1.8.0_73/bin/client;C:/Program Files (x86)/Java/jre1.8.0_73/bin;C:/Program Files (x86)/Java/jre1.8.0_73/lib/i386;C:\WinAVR-20100110\bin;C:\WinAVR-20100110\utils\bin;C:\ProgramData\Oracle\Java\javapath;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Common Files\lenovo\easyplussdk\bin;C:\Program Files   (x86)\GTK2-Runtime\lib;C:\Program Files (x86)\Java\jdk1.7.0_51\bin;E:\Eclipse for all\Eclipse for all\eclipse;;.
 Mar 30, 2016 4:45:00 PM org.apache.tomcat.util.digester.SetPropertiesRule begin
 WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:J2EE' did not find a matching property.
 Mar 30, 2016 4:45:00 PM org.apache.tomcat.util.digester.SetPropertiesRule begin
 WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:Web' did not find a matching property.
 Mar 30, 2016 4:45:00 PM org.apache.tomcat.util.digester.SetPropertiesRule begin
 WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:Aptech' did not find a matching property.
 Mar 30, 2016 4:45:00 PM org.apache.tomcat.util.digester.SetPropertiesRule begin
 WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:DBMS_Project' did not find a matching property.
 Mar 30, 2016 4:45:00 PM org.apache.coyote.AbstractProtocol init
 INFO: Initializing ProtocolHandler ["http-bio-8080"]
 Mar 30, 2016 4:45:00 PM org.apache.coyote.AbstractProtocol init
 INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
 Mar 30, 2016 4:45:00 PM org.apache.catalina.startup.Catalina load
 INFO: Initialization processed in 607 ms
 Mar 30, 2016 4:45:00 PM org.apache.catalina.core.StandardService startInternal
 INFO: Starting service Catalina
 Mar 30, 2016 4:45:00 PM org.apache.catalina.core.StandardEngine startInternal
 INFO: Starting Servlet Engine: Apache Tomcat/7.0.40
 Mar 30, 2016 4:45:01 PM org.apache.coyote.AbstractProtocol start
 INFO: Starting ProtocolHandler ["http-bio-8080"]
 Mar 30, 2016 4:45:01 PM org.apache.coyote.AbstractProtocol start
 INFO: Starting ProtocolHandler ["ajp-bio-8009"]
 Mar 30, 2016 4:45:01 PM org.apache.catalina.startup.Catalina start
 INFO: Server startup in 500 ms
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at   org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1714)
at  org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559)
  at org.apache.jasper.servlet.JasperLoader.loadClass(JasperLoader.java:126)
  at org.apache.jasper.servlet.JasperLoader.loadClass(JasperLoader.java:63)
  at java.lang.Class.forName0(Native Method)
  at java.lang.Class.forName(Unknown Source)
  at org.apache.jsp.insert_jsp._jspService(insert_jsp.java:98)
  at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
  at   org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
  at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
  at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
  at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
  at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
  at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
  at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
  at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
  at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
  at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
  at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
  at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
  at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
  at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1008)
  at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
  at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
  at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
  at java.lang.Thread.run(Unknown Source)

没有编译错误,html代码运行正常。一旦我点击“提交”,以html格式,表“注册”不会得到更新,我无法重定向到另一个页面。请帮助我。

1 个答案:

答案 0 :(得分:1)

下载Mysql-Connector-java - * .jar,将其复制并粘贴到WEB-INF / lib /目录下(在eclipse中)...然后单击该项目,选择Properties并选择Java Build Path,然后添加jar ...因为&#34; java.lang.ClassNotFoundException:com.mysql.jdbc.Driver&#34;你有这个错误。我想这会解决你的问题......