0

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: enter image description here

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;
    }

    
}
    
    
    
    
aero
  • 372
  • 1
  • 4
  • 18
  • 1
    Your connection creation is failing. Try printing stack trace in `get_conn()` method's catch block `e.printStackTrace()` and look at server console for the actual reason. – RP- Jul 13 '16 at 23:29
  • @RP here is what i see in the Tomcat log : java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/login – aero Jul 13 '16 at 23:43
  • 1
    Uh I overlooked.. First thing you need to do is register or load the driver. `Class.forName("com.mysql.jdbc.Driver");` should be first line in your try block. This will load the Driver class in jvm so that it will be available for the `DriverManager`. – RP- Jul 13 '16 at 23:53

1 Answers1

2

In your Record class file
Change the package import from com.mysql.jdbc.Connection to java.sql.Connection.

The createStetment() and other jdbc related methods you need belong to the Connection object in java.sql package.

After some comments, it looks like mysql jdbc driver is not in your classpath.
You can download it here:
https://dev.mysql.com/downloads/connector/j/
Include the jar file in your classpath. One simplest way to do it is to copy the jar in your WEB-INF/lib folder.

Just to be safe, I always add the following line right before you get your connection, like so:

Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection(connectionUrl, username, password);
Minjun Yu
  • 3,497
  • 4
  • 24
  • 39
  • Same error message, no change. – aero Jul 13 '16 at 23:08
  • 1
    Try change **ALL** the import to java.sql.Connection in your project. Make sure there is no com.mysql.jdbc.Connection **anywhere**. If that still does not work, please post the code where you get your Connection object. – Minjun Yu Jul 13 '16 at 23:15
  • I changed the import in every one including the connecting file, did not work, i posted the class Conneting.java as an Update. – aero Jul 13 '16 at 23:25
  • 1
    @DE_CR if you changed all the import to the correct one and it still does not work. Please call `e.printStackTrace();` in your every catch clause, like RP suggests. Post the full StackTrace, I am willing to help further. – Minjun Yu Jul 13 '16 at 23:40
  • I just did, and i was looking at the Tomcat log in netbeans, here is an error message : java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/login – aero Jul 13 '16 at 23:42
  • 1
    @DE_CR I updated the answer, you need to include mysql jdbc driver in your classpath. – Minjun Yu Jul 13 '16 at 23:47
  • that's awesome, in addition to doing what you told me, i also did what @RP suggested in his comment, and it worked, Thanks alot for your help – aero Jul 13 '16 at 23:59