For 5 Hours I tried solving this problem and yeiled no result, I am getting this Error message When clicking Submit, it suppose to record information in the fields into SQL, but It does not:
MY index.jsp code is here:
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>
My Servlet file :
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);
}
}
The problem is with my insert function that is suppose to be inserting data to SQL, it's inside the "Record" class which is here:
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;
}
}
and the web.xml code :
<?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>
The Connecting class is what contains the actual connection and can return "conn" of connection type, it works fine so no need to post its' code.
So, here is what I tired:
- Checking that SQL connection is good by creating a Main class in the project and run it with manual variables and insert them to the function insert inside the Record class....OK
Print information from the Fields coming from the JSP back to the JSP by response.out.println() to make sure that the variables are not null....OK
Asked others if I have to define the SQL in the web.xml file.....(I don't need to define it)
So, What I know so far that the error is starting from this line which is calling the function :
boolean reg= Record.insert(2, first, last, email, username, pass);
That is ofcorse according to the Error code on the website indicating that this is where the error is coming, and Also the error messages says that this line is also making an error (or not executing ):
Statement s = c.createStatement();
I would be amazed to know why these error are generating since I feel that i've been trying many things and nothing is working, so this was my last resort.
UPDATE: Here is the Connecting class code where i get my "conn" :
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;
}
}