JSP / Servlet:尝试记录到SQL时导致Servlet错误的原因是什么?

时间:2016-07-13 22:43:27

标签: java sql-server jsp servlets web-deployment

5小时我尝试解决这个问题并且没有结果,我收到此错误消息当单击提交时,它假设将字段中的信息记录到SQL中,但它不会: enter image description here

我的index.jsp代码在这里:



contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        
       <body>

 
        
      <form action = "servlet3" method="get">
        Register : <br> <br>
         <input type="text" name ="first" placeholder="First Name">  <input type="text" name ="last" placeholder="Last Name"> <br> <br>
         <input type="email" name ="email" placeholder="Email"><br>
         <input type="text" name ="user" placeholder="Choose Username"><br>
         <input type="password" name ="pass" placeholder="Choose a Password"><br>
         <input type="password" name ="pass_ver" placeholder="Verify Password"><br> <br> <br>
        <input type="submit" value ="Submit">
    </form>
</body>
</html>
&#13;
&#13;
&#13;

我的Servlet文件:

&#13;
&#13;
package com.let;

import com.mysql.jdbc.Connection;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;



       @Override
       protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
         
        String first = request.getParameter("first");
        String last = request.getParameter("last");
        String email = request.getParameter("email");
        String username = request.getParameter("user");
        String pass = request.getParameter("pass");
        String pass_ver = request.getParameter("pass_ver");
        
        boolean reg= Record.insert(2, first, last, email, username, pass);
        
        
    
  
  
    
        } 
    }
       
&#13;
&#13;
&#13;

问题在于我的insert函数假设要将数据插入SQL,它位于&#34;记录&#34;这里的课程:

&#13;
&#13;
package com.let;

import com.mysql.jdbc.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;


public class Record {
    
    public Record(){
        
    }
    
    public static boolean insert(int id, String f, String l, String email,String user,String pass){
        boolean status = true;
        
        Connection c=null; 
      try{
          c=Connecting.conn; //Connecting class contains conn object which is of type Connection
        Statement s = c.createStatement();
     
        
       
      
        String u = "USE login;"; //login database, info is table
        s.executeUpdate(u);
        String insert = "INSERT INTO login.info(ID,FIRST,LAST,EMAIL,USER,PASS) VALUES ('"+id +"','"+f +"','"+l+"','"+email+"','"+user+"','"+ Formulation.encypt(pass) +"')";
        s.executeUpdate(insert);
        }catch (SQLException e){
            status =false;
        }
        
        
        return status;
        
    }
    
}
&#13;
&#13;
&#13;

和web.xml代码:

&#13;
&#13;
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <servlet>
        <servlet-name>E1Servlet</servlet-name>
        <servlet-class>com.let.serv</servlet-class>
    </servlet>
    <servlet>
        <servlet-name>E2Servlet</servlet-name>
        <servlet-class>com.let.serv2</servlet-class>
    </servlet>
    <servlet>
        <servlet-name>E3Servlet</servlet-name>
        <servlet-class>com.let.serv3</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>E1Servlet</servlet-name>
        <url-pattern>/servlet1</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>E2Servlet</servlet-name>
        <url-pattern>/servlet2</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>  E3Servlet</servlet-name>
        <url-pattern>/servlet3</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
</web-app>
&#13;
&#13;
&#13;

Connecting类是包含实际连接的东西,可以返回&#34; conn&#34;连接类型,它工作正常,所以不需要发布它的&#39;代码。

所以,这就是我累了:

  • 通过在项目中创建Main类并使用手动变量运行它并将它们插入到Record类中的函数insert中来检查SQL连接是否正常.... OK
  • 通过response.out.println()将来自JSP的字段打印回JSP,以确保变量不为空.... OK

  • 询问其他人是否必须在web.xml文件中定义SQL .....(我不需要定义它)

所以,到目前为止我所知道的错误是从这一行调用函数开始的:

boolean reg= Record.insert(2, first, last, email, username, pass);

根据网站上的错误代码表明这是错误的来源,并且错误消息表明此行也出错(或未执行):

Statement s = c.createStatement();

我很惊讶地知道为什么这些错误正在产生,因为我觉得我一直在尝试很多事情而且没有任何工作,所以这是我的最后一招。

更新:这是连接类代码,我得到了我的&#34; conn&#34; :

&#13;
&#13;
package com.let;
import com.mysql.jdbc.Connection;

import java.awt.EventQueue;

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JOptionPane;




public class Connecting {
    
    
    public final static String username="root";
    public final static String pass ="mypassword99";
    public final static String conn_str="jdbc:mysql://localhost:3306/login";
    public static Connection conn = ge_conn();
    
    public static Connection get_conn (){
         Connection conn=null;
         
        try{
            conn =(Connection) DriverManager.getConnection(conn_str,username,pass);
       conn =(Connection) DriverManager.getConnection(conn_str,username,pass);
    }catch (SQLException e){
            System.out.println("SQL ERR!!!!");
        }
 
       
       return conn;
    }

    
}
    
    
    
    
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:2)

在您的记录类文件中 将包导入从 com.mysql.jdbc.Connection 更改为 java.sql.Connection

您需要的createStetment()和其他jdbc相关方法属于java.sql包中的连接对象。

经过一些评论后,看起来mysql jdbc驱动程序不在你的类路径中 你可以在这里下载:
https://dev.mysql.com/downloads/connector/j/
在类路径中包含jar文件。最简单的方法是将jar复制到WEB-INF / lib文件夹中。

为了安全起见,我总是在你获得连接之前添加以下行,如下所示:

Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection(connectionUrl, username, password);