java. sql. Sqlexception: no suitable driver found
I tried to use the following dbquery The Java class performs a simple query, which uses the dbconnector to obtain a connection from the drivermanager
be careful:
>I have added "MySQL connector-java-5.1.25-bin.jar" 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 can use "MySQL - uroot - ptcial" to connect to the MySQL address book. If it's important. > it's no use trying to run arguments using '- CP'. > I can make my #3 dbconnect.java class say "database connection established" ># 4 DbQueryWorking. Java has no problem and provides the expected output
Can you help me understand the problem 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 * from 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,password); Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery("select * from 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); } } }
Solution
It looks like dbconnector The URL in Java is wrong The colon is missing The web address must be:
jdbc:MysqL://localhost:3306/
Not at all
jdbc:MysqL//localhost:3306/