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


this question has answer here:

i have java program: mysqlconnectexample.java

import java.sql.*; import java.util.properties;  public class mysqlconnectexample {     public static void main(string[] args) {         connection conn1 = null;         connection conn2 = null;         connection conn3 = null;          try {             string url1 = "jdbc:mysql://localhost:3306/aavikme";             string user = "root";             string password = "aa";              conn1 = drivermanager.getconnection(url1, user, password);             if (conn1 != null)                 system.out.println("connected database test1");              string url2 = "jdbc:mysql://localhost:3306/aavikme?user=root&password=aa";             conn2 = drivermanager.getconnection(url2);             if (conn2 != null) {                 system.out.println("connected database test2");             }              string url3 = "jdbc:mysql://localhost:3306/aavikme";             properties info = new properties();             info.put("user", "root");             info.put("password", "aa");              conn3 = drivermanager.getconnection(url3, info);             if (conn3 != null) {                 system.out.println("connected database test3");             }         } catch (sqlexception ex) {             system.out.println("an error occurred. maybe user/password invalid");             ex.printstacktrace();         }     } } 

i compile this:

e:\java mysql code driver>javac mysqlconnectexample.java  e:\java mysql code driver>java -cp mysql-connector-java-3.0.11-stable-bin.jar;. mysqlconnectexample 

i error:

an error occurred. maybe user/password invalid java.sql.sqlexception: no suitable driver found jdbc:mysql://localhost:3306/ aavikme         @ java.sql.drivermanager.getconnection(drivermanager.java:596)         @ java.sql.drivermanager.getconnection(drivermanager.java:215)         @ mysqlconnectexample.main(mysqlconnectexample.java:20) 

what doing wrong?

make sure run first:

class.forname("com.mysql.jdbc.driver"); 

this forces driver register itself, java knows how handle database connection strings.

for more information, see mysql connector reference.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

android - Keyboard hides my half of edit-text and button below it even in scroll view -

css - Make div keyboard-scrollable in jQuery Mobile? -