尝试使用ajax将数据添加到mysql数据库时出现500内部服务器错误

时间:2017-03-01 15:53:14

标签: javascript java mysql ajax netbeans

我正在创建一个简单的登录页面,因为我正在使用html ajax,javascript,java servlets和mysql.But当我试图通过单击提交按钮i'将用户的数据添加到数据库中时得到500内部服务器错误。 这是我的代码。    这是使用ajax和javascript将数据传递给servlet的代码。

<script language="JavaScript" type="text/javascript">
    var get1=new XMLHttpRequest();
     function login()
     {
       console.log("Signup");
       var username=document.getElementById("username").value;
       var pword=document.getElementById("pword").value;
       var confpwd=document.getElementById("confpwd").value;
       var email=document.getElementById("email").value;
       var fname=document.getElementById("fname").value;
       var lname=document.getElementById("lname").value;
       var gender=document.getElementById("gender").value;
       var data=username+" "+pword+" "+confpwd+" "+email+" "+fname+" "+lname+" "+gender;
       console.log("here"+" "+data);
       get1.open("get","/loginpage/NewServlet?id1="+ data,true);
       get1.onreadystatechange=process;
       get1.send(null);
     }

     function process()
     {
        var JSONtopicobject=eval( "("+get1.responseText + ")" );
        console.log("Signup successful");
     }

这是我用于将数据添加到数据库的java servlet代码

 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
 {

     PrintWriter uout=response.getWriter();
     String textp=request.getParameter("data");
     String[] t=textp.split(" ");
     try{
        //get a connection


      con=DriverManager.getConnection("jdbc:mysql://localhost/login","root","ATul1996@@");

      //create a statement
      Statement st=con.createStatement();
      //Execute a query
      String exec="INSERT INTO login "+" (username,pwd,email,fname,lname,gender)" + " VALUES('as','op','py','po','lp','ll')";
      st.executeUpdate(exec);
      uout.println("insertion done");
      g="insert complete";
      }catch(SQLException e)
       {
        System.out.println(e);
       }
      finally{

       try {
           con.close();
           } catch (SQLException ex) {
           Logger.getLogger(NewServlet.class.getName()).log(Level.SEVERE, null, ex);
           }
     }
      response.setContentType("text/html");
      response.getWriter().write(g);
      response.getWriter().flush();
      response.getWriter().close();
}

0 个答案:

没有答案