-1

Guys I am trying to develop a simple authentication application in java by using RESTful and MySQL.

For the Server i'm using the following code

import java.sql.Connection;
import java.sql.DriverManager;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;    

    //  http://localhost:8080/2.ServerUserAPI/rest/User?Username=david
    @Path("/User")
    public class Main{

        @GET
        @Produces(MediaType.TEXT_PLAIN)
        public String sayPlainTextHello(@QueryParam("Username") String Username) throws Exception{              

            try{        
                Connection con = DriverManager.getConnection("jdbc:mysql://localhost/CloudDB", "root", "eusgr1");
                    PreparedStatement statement = con.prepareStatement("SELECT Username FROM UserAuthentication");
            }
            catch (Exception e){
                throw e;
            }

            return "qwer";            
            }
        }

But I get the following error: No suitable driver found for jdbc:mysql://localhost/CloudDB

I have included the jar file for mysql Also i have included the jars for jersey.

When i try this by placing comments for the:

//Connection con = DriverManager.getConnection("jdbc:mysql://localhost/CloudDB", "root", "eusgr1");

It works great on the server. Also when in a different class to make the connection by using public static void main{ ... connection ...} again it works.

How can i combine these two (RESTful and MySQL) ??

Thank you for you help.

user3358377
  • 145
  • 3
  • 16
  • What I posted has a detail, it is NOT only for MySQL connectivity ... !!! As i have written above I try to connect MySQL through a RESTful web service. Also i have written that i am able to connect Java with MySQL in a simple application. I have this problem with RESTful! – user3358377 May 19 '14 at 22:22

1 Answers1

0

I've noticed similarly odd behavior when I'm using mysql from a webapp, specifically one deployed to Tomcat. JDBC driver jars are supposed to register themselves with the DriverManager on startup, but I think due to some oddity of the application classloader, this isn't happening. The "fix" seems to be to either a) include the mysql jar at the common classloader level, or b) inside your app, somewhere before trying to get a JDBC connection, call Class.forName("com.mysql.jdbc.Driver").

David
  • 2,602
  • 1
  • 18
  • 32
  • Hello my friend, I have added the MySQL jar as external .... this is what i usually do. If i try add it to the web folder with the jersey jars then the REST won't work. A) What do u mean to add the jar to the common class loader level?? how can i do that?? B) I have tried the Class.forName("com.mysql.jdbc.Driver") but i get the following error: HTTP Status 500- java.lang.ClassNotFoundException: com.mysql.jdbc.Driver I have build the path of the MySQL.jar and it is down of the jre System Library. Do i have to modify the command Class.forName("com.mysql.jdbc.Driver")?? and if yes do u know how? – user3358377 May 19 '14 at 22:48
  • 1
    Just found it m8 .... i had to include the jar file to my Webserver's lib folder and now is working like a charm. So ignore me previous comment. Thank you very much – user3358377 May 19 '14 at 23:07