No Suitable Driver Found for 'Jdbc:Mysql://Localhost:3306/Mysql

How to fix: No suitable driver found for jdbc:mysql://localhost/dbname error when using pools?

Try putting the driver jar in the server lib folder. ($CATALINA_HOME/lib)

I believe that the connection pool needs to be set up even before the application is instantiated. (At least that's how it works in Jboss)

No suitable driver found for 'jdbc:mysql://localhost:3306/mysql

In this particular case (assuming that the Class#forName() didn't throw an exception; your code is namely continuing with running instead of throwing the exception), this SQLException means that Driver#acceptsURL() has returned false for any of the loaded drivers.

And indeed, your JDBC URL is wrong:

String url = "'jdbc:mysql://localhost:3306/mysql";

Remove the singlequote:

String url = "jdbc:mysql://localhost:3306/mysql";

See also:

  • Mini tutorial on MySQL + JDBC connectivity

No suitable driver found for jdbc:mysql/localhost:3306/world

Syntax for MySQL JDBC URL is:

The general format for a JDBC URL for connecting to a MySQL server is as follows, with items in square brackets ([ ]) being optional:

jdbc:mysql://[host1][:port1][,[host2][:port2]]...[/[database]][?propertyName1=propertyValue1[&propertyName2=propertyValue2]...]

Here is a simple example for a connection URL:

jdbc:mysql://localhost:3306/sakila?profileSQL=true

Your URL jdbc:mysql/localhost:3306/world is missing a :/ after mysql.

Port number defaults to 3306, so your URL should be:

jdbc:mysql://localhost/world

java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/mydatabase Android Studio Java MySQL

The problem is that the JDBC driver just isn't there at runtime. All the things you are trying are just different ways of loading it, and that's why none of it works.

If the driver is there, you don't need to do any of these things - no need to Class.forName, for example, and all these things you are trying would all work fine.

The first question is: Why in the blazes are you loading a mysql JDBC driver on your phone? Your phone doesn't have a mysql database server. The JDBC driver is just a way to make java connect to an already existing mysql server. It is not, itself, the mysql server - just the thing that can connect to it.

So, most likely, the right answer here is: You are thoroughly confused; mysql has no business running on a phone.

You may possibly want to connect straight from your phone to a mysql server someplace that is directly reachable by the phone. But this too is somewhat unlikely - that only works if the phone is on the internal network (so, your local wifi), or, if your mysql server is connected straight to the wide open internet (That's a bad idea), or, if your phone is on a VPN and in that way it can get to the mysql server which is opened up to the entire VPN. These ideas are not completely insane, but close to it. If you really want this (I don't think you do), all you need to fix is [A] the mysql URL obviously cannot be localhost, there is no mysql server on your phone, and [B] you need the mysql JDBC driver to be available as part of the app on your phone. This should not be particularly difficult (it's just code that connects over TCP, nothing that is inherently incompatible with android), but you may have a hard time finding tutorials, as nobody* does this.

A far more likely setup is that there is some software running on a server someplace (can be written in java as well, of course!), and it talks to the mysql server, running locally or someplace else within the network, and this server exposes an HTTP-based API that your phone then connects to. In that case, you don't want the phone to have JDBC whatsoever, and the phone won't even be talking any SQL. The server code would.

Note that android phones do have DBs in them, but it's SQLite, and every app running on android gets its own SQLite based DB for free if it wants. There are tons of tutorials out there on how you can interact with it.

*) Rounded down.

java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/database

Before get connection you have to load your driver with:

Class.forName("com.mysql.jdbc.Driver");

And the corresponding JAR must be in your classpath (in the lib of your server or packaged in your WAR file).

Anyway I suggest you to use a connection's pool instead of DriverManager

The main benefits to connection pooling are:

  1. Reduced connection creation time.

Although this is not usually an issue with the quick connection setup that MySQL offers compared to other databases, creating new JDBC connections still incurs networking and JDBC driver overhead that will be avoided if connections are recycled.


  1. Simplified programming model.

When using connection pooling, each individual thread can act as though it has created its own JDBC connection, allowing you to use straightforward JDBC programming techniques.


  1. Controlled resource usage.

If you create a new connection every time a thread needs one rather than using connection pooling, your application's resource usage can be wasteful, and it could lead to unpredictable behaviors for your application when it is under a heavy load.

Java-Integration with JDBC - No suitable driver found for jdbc:mysql error

MySQL Connector/J 8 requires Java 8, while you're trying to run it on Java 7 (as shown by the "JRE System Library [JavaSE-1.7]" in your IDE screenshot). Either upgrade or switch your project to Java 8 (or higher), or downgrade to an older MySQL Connector/J version (e.g. MySQL Connector/J 5.1.49, which supports Java 5 or higher).

If you add Class.forName("com.mysql.cj.jdbc.Driver") to the start of your main method, you probably get a NoClassDefFoundError with cause UnsupportedClassVersionError.

For the general causes of error "java.sql.SQLException: No suitable driver found for jdbc:mysql://...", see also Connect Java to a MySQL database

SQLException: No suitable driver found for jdbc:mysql. SQLState: 08001. VendorError: 0

SQLException: No suitable driver found for
jdbc:mysql://localhost:3306user=webstudent&password=webstudent

Your connection string is incorrect. You've missed the database name. So, update the database name in the following string and use it.

jdbc:mysql://localhost:3306/database_name?user=webstudent&password=webstudent

Also, place the following statement prior to connection stmt

Class.forName("com.mysql.jdbc.Driver"); 

Updated:

Add the serverTimezone explicitly in the connection string
Eg:

jdbc:mysql://localhost:3306/database_name?user=webstudent&password=webstudent&serverTimezone=UTC

How to solve: No suitable driver found for jdbc:mysql://localhost:3306/sampledb

You need to load the class driver before getting a connection:

Class.forName("com.mysql.jdbc.Driver").newInstance();

Here is an example.

and the working code:

Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection("jdbc:mysql://localhost/sampledb?" + "user=root&password=14701");
} catch (SQLException ex) {
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
} catch (Exception e) {
e.printStackTrace();
} finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}


Related Topics



Leave a reply



Submit