jdbc - java.sql.SQLException: No suitable driver found -
i trying execute simple query using below dbquery.java class uses dbconnector connection drivermanager.
note:
- i have included "mysql-connector-java-5.1.25-bin.jar" on classpath via: export classpath=$classpath:/home/me/ocpjp/chapter-10/mysql-connector-java-5.1.25/mysql-connector-java-5.1.25-bin.jar
- i able connect mysql "mysql -uroot -ptcial addressbook", if matters.
- have tried running '-cp' argument no avail.
- i able #3 dbconnect.java class "database connection established".
- also #4 dbqueryworking.java has no issues , provides expected output .
can please me understand issue here ?
1) dbconnector.java
package com.me.ocpjp.chapter10; import java.sql.connection; import java.sql.drivermanager; import java.sql.sqlexception; public class dbconnector{ public static connection connecttodb() throws sqlexception{ string url = "jdbc:mysql//localhost:3306/"; string db = "addressbook"; string username = "root"; string password = "tcial"; return drivermanager.getconnection(url+db, username, password); } }
2) dbquery.java
package com.me.ocpjp.chapter10; import java.sql.connection ; import java.sql.statement ; import java.sql.resultset ; import java.sql.sqlexception ; import com.me.ocpjp.chapter10.dbconnector; public class dbquery{ public static void main(string[] args){ try(connection connection = dbconnector.connecttodb(); statement statement = connection.createstatement(); resultset resultset = statement.executequery("select * contact")){ system.out.println("id \tfname \tlname \temail \t\tphoneno"); while(resultset.next()){ system.out.println(resultset.getint("id") + "\t" + resultset.getstring("firstname") + "\t" + resultset.getstring("lastname") + "\t" + resultset.getstring("email") + "\t" + resultset.getstring("phoneno") ); } }catch(sqlexception sqle){ sqle.printstacktrace(); system.exit(-1); } } }
3) dbconnect.java
package com.me.ocpjp.chapter10; import java.sql.connection; import java.sql.drivermanager; public class dbconnect{ public static void main(string[] args){ string url = "jdbc:mysql://localhost:3306/"; string database = "addressbook"; string username = "root"; string password = "tcial"; try(connection connection = drivermanager.getconnection(url+database, username, password)){ system.out.println("database connection established"); }catch(exception e){ system.out.println("database connectioni not established"); e.printstacktrace(); } } }
4) dbqueryworking.java
package com.me.ocpjp.chapter10; import java.sql.connection ; import java.sql.statement ; import java.sql.resultset ; import java.sql.sqlexception ; import java.sql.drivermanager; public class dbquery{ public static void main(string[] args){ string url = "jdbc:mysql://localhost:3306/"; string database = "addressbook"; string username = "root"; string password = "tcial"; try(connection connection = drivermanager.getconnection(url + database, username, password); statement statement = connection.createstatement(); resultset resultset = statement.executequery("select * contact")){ system.out.println("id \tfname \tlname \temail \t\tphoneno"); while(resultset.next()){ system.out.println(resultset.getint("id") + "\t" + resultset.getstring("firstname") + "\t" + resultset.getstring("lastname") + "\t" + resultset.getstring("email") + "\t" + resultset.getstring("phoneno") ); } }catch(sqlexception sqle){ sqle.printstacktrace(); system.exit(-1); } } }
it looks url in dbconnector.java
wrong. colon missing. url must be:
jdbc:mysql://localhost:3306/
and not
jdbc:mysql//localhost:3306/
Comments
Post a Comment