I am trying to run this code below while learning the java sql stuff, however, i have created the database and tried it from the terminal its fine.
1-I get this error
java.sql.SQLException: No suitable driver found for jdbc:sql://localhost/books
at java.sql.DriverManager.getConnection(DriverManager.java:602)
at java.sql.DriverManager.getConnection(DriverManager.java:185)
at Displayauthors.main(Displayauthors.java:20)
2-I downloaded “odbc manager” and i don’t know why and what to do with it or if it has any relation to what am doing here?
3-I have downloaded Connector/ODBC and Connector/J. I went to JRE reference library and added “mysql-connector-java-5.1.21-bin.jar” and tried the .tar.gz the multi-platform version. I don’t know exactly i kept trying anything and didn’t work.
4-I tried to go to database prospective from eclipse and add new connection MYSQL and include the .jar and stuff and still nothing changed.
5-tried to include the class.forName….. and nothing too.
CAN YOU PLEASE PLEASE, HELP ME UNDERSTAND THIS PROCESS OR GUIDE ME TO A CLEAR EXAMPLE ON SETTING THIS UP, I DO UNDERSTAND THE CODE BUT THE ENVIRONMENT SET UP IS GIVING ME HARD TIME.
import java.sql.*;
public class Displayauthors {
static final String DATABASE_URL = "jdbc:sql://localhost/books";
public static void main(String args[]){
Connection connection = null;
Statement statement = null;
ResultSet resultset = null;
try{
connection = DriverManager.getConnection(DATABASE_URL, "deitel", "deitel");
statement = connection.createStatement();
resultset=statement.executeQuery("SELECT AuthorID,FirstName,LastName FROM Authors");
ResultSetMetaData metadata = resultset.getMetaData();
int numberofcolumns = metadata.getColumnCount();
System.out.println("Authors table");
for(int i=1;i<=numberofcolumns;i++){
System.out.printf("%-8s\t", metadata.getColumnName(i));
System.out.println();
}
}
catch(SQLException sqlexception){
sqlexception.printStackTrace();
}
}
}
You should use the correct uri for a mysql database