0

Hello im trying to connect to a mysql database using JDBC my code is below.I get an error as such No suitable driver found.Searching around I found that the usual error is syntax or missing the jar file from the class path.I tried both of these solutions and dont know what to do next it wont connect.Also to manage the databases I have WAMP and mySQL workbench installed not sure if its related.

package test.jdbc;
import java.sql.*;


public class jdbctester {

public static void main(String[] args)
{

    try
    {
        Connection myconn=DriverManager.getConnection("jdbc:mysql://localhost:3306/voting","root","Vanquish123");
        Statement myStmt=myconn.createStatement();
        ResultSet myRs=myStmt.executeQuery("select * from electoral");
        /*
        while(myRs.next())
        {
            System.out.println(myRs.getString("state")+","+myRs.getString("perDem"));
        }
        */
    }
    catch(Exception exc)
    {
    exc.printStackTrace();  
    }
}

}

lolsharp
  • 69
  • 1
  • 11

2 Answers2

0

Try this:

Class.forName("fully qualified driver class name");

Java Class.forName, JDBC connection loading driver

this post states that you should not need it but it will not hurt you to try.

Community
  • 1
  • 1
bichito
  • 1,406
  • 2
  • 19
  • 23
  • 1
    Not required since 2007, and if the driver wasn't found without it, it won't be found with it. – user207421 Apr 08 '17 at 16:35
  • Class.forName("fully qualified driver class name"); worked for me but why do I need this all the examples I looked at didint have this – lolsharp Apr 08 '17 at 16:38
  • Why downvoting? If you state that you checked the class path why are accepting an answer that tells you to do what you already did? – bichito Apr 08 '17 at 16:38
  • Could please accept the answer then? It seems absurd that you say my suggestion works and then it is downvoted – bichito Apr 08 '17 at 16:40
  • I got you but can you explain why its needed? – lolsharp Apr 08 '17 at 16:42
  • It has to do with initialization of the class itself not an instance of the driver – bichito Apr 08 '17 at 16:42
  • According to that post it shouldn't be needed with jdbc 4 and Java 1.6 and above but I have found it Is not the case – bichito Apr 08 '17 at 16:43
  • This would only fix the problem if you are working with an extremely old driver version, or if you are working with a web application and deploying the driver with the web application, instead of in the application server itself. Given this is not a web application, you must be using a very old driver. – Mark Rotteveel Apr 09 '17 at 07:37
0

you have to add "com.mysql.jdbc_5.1.5.jar" in to your project build path... go to project property>build Path> library> add external jar and add jar file.

Connection conn = null;  
    try {
        // Register JDBC driver
        Class.forName(DRIVER).newInstance();

        // Open a connection
        conn = DriverManager.getConnection(Local_URL + , USERNAME, PASSWORD);
        System.out.println("Connected Database Successfully...\n\n");

    } catch (Exception se) {
        throw new AppException("Failed to create Local Database connection", se);
    }
    return conn;
Anshul Sharma
  • 3,432
  • 1
  • 12
  • 17
  • Class.forName("fully qualified driver class name"); worked for me but why do I need this all the examples I looked at didint have this – lolsharp Apr 08 '17 at 16:37
  • "com.mysql.jdbc.Driver" – Anshul Sharma Apr 08 '17 at 16:38
  • I also watched this video and it didint use it https://www.youtube.com/watch?v=2i4t-SL1VsU any idea why it wouldent work without it for me? – lolsharp Apr 08 '17 at 16:39
  • MySQL Connector/J 5.1.5 is extremely old, (September 2007). You'd really need to use a newer version (latest at this time is 5.1.41). Also calling `newInstance()` was a workaround for an even older bug in the MySQL driver. – Mark Rotteveel Apr 09 '17 at 07:39