使用jsp在MySQL数据库中存储表单信息

时间:2016-02-10 08:59:44

标签: java mysql jsp

<%@page import="javax.servlet.*"%>
<%@page import="java.sql.*"%>
<%@page import="java.util.*"%>
<%@page import="java.lang.Object.*"%>
<%@page import="java.text.*"%>
<% 
String empid=request.getParameter("Emp.Id");
String sname=request.getParameter("sname");
String designation=request.getParameter("designation");
String category=request.getParameter("category");
String  dept=request.getParameter("dept");
java.util.Date d=new SimpleDateFormat("yyyy-MM-   dd").parse(request.getParameter("dob"));
java.sql.Date dob=new java.sql.Date(d.getTime());
String age=request.getParameter("age");
java.util.Date dj=new SimpleDateFormat("yyyy-MM-dd").parse(request.getParameter("doj"));
java.sql.Date doj=new java.sql.Date(dj.getTime());
String gender=request.getParameter("gender");
String community=request.getParameter("community");
String education=request.getParameter("education");
String yoe=request.getParameter("yoe");
String pdoor=request.getParameter("pdoor");
String padd1=request.getParameter("padd1");
String padd2=request.getParameter("padd2");
String pcity=request.getParameter("pcity");
String ptaluk=request.getParameter("ptaluk");
String pdistrict=request.getParameter("pdistrict");
String ppcode=request.getParameter("ppcode");
String pstate=request.getParameter("pstate");
String cdoor=request.getParameter("cdoor");
String cadd1=request.getParameter("cadd1");
String cadd2=request.getParameter("cadd2");
String ccity=request.getParameter("ccity");
String ctaluk=request.getParameter("ctaluk");
String cdistrict=request.getParameter("cdistrict");
String cpcode=request.getParameter("cpcode");
String cstate=request.getParameter("cstate");
String email=request.getParameter("email");
String ms=request.getParameter("ms");
String noc=request.getParameter("noc");
String fsname=request.getParameter("fsname");
String fsocc=request.getParameter("fsocc");
String fsadd=request.getParameter("fsadd");
String fsmob=request.getParameter("fsmob");
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql",
        "root", "mysql23");
Statement st = con.createStatement();
 int i=st.executeUpdate("insert into staffdetail(empid,sname,designation,category,dept,dob,age,doj,gender,community,education,yoe,pdoor,padd1,padd2,pcity,ptaluk,pdistrict,ppcode,pstate,cdoor,cadd1,cadd2,ccity,ctaluk,cdistrict,cpcode,cstate,emailid,mstatus,nchild,fsname,fsocc,fsaddress,fsmobile) values("+empid+",'"+sname+"','"+designation+"','"+category+"','"+dept+"','"+dob+"','"+age+"','"+doj+"','"+gender+"','"+community+"','"+education+"','"+yoe+"','"+pdoor+"','"+padd1+"','"+padd2+",'"+pcity+"','"+ptaluk+"','"+pdistrict+"','"+ppcode+"','"+pstate+"','"+cdoor+"','"+cadd1+"','"+cadd2+"','"+ccity+"','"+ctaluk+"','"+cdistrict+"','"+cpcode+"','"+cstate+"','"+email+"','"+ms+"','"+noc+"','"+fsname+"','"+fsocc+"','"+fsadd+"','"+fsmob+"')");
 if(i>0)
 {
response.sendRedirect("over.jsp");
  }
 else
{
 response.sendRedirect("over1.jsp");
  }

%>

错误是

  

javax.servlet.ServletException:   com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:你有一个   SQL语法错误;查看与您的手册相对应的手册   MySQL服务器版本

2 个答案:

答案 0 :(得分:0)

mysql-jdbc-connector.jar应该添加到你的项目中。 所以你的jsp ll与MySql数据库进行通信

答案 1 :(得分:0)

MySQL JDBC驱动程序不在您的运行时类路径上。您首先需要从这里下载JAR:

https://dev.mysql.com/downloads/connector/j/

然后,您需要将其复制到服务器/容器上的共享库文件夹中(而不是将其与您的应用程序捆绑在一起 - Why must the JDBC driver be put in TOMCAT_HOME/lib folder?)。

具体取决于您的服务器:

对于Tomcat 8,将JAR复制到TOMCAT_HOME / lib

对于Jetty 9,将JAR复制到JETTY_HOME / lib / ext。