views:

66

answers:

1

Hello, So I want to create a JDBC connection to MySQL server that is installed on my pc, here are the steps, I installed MySQL with the username and password "root", downloaded mysql-connector-java and from theere I coped the JAR "mysql-connector-java-5.1.12-bin" to "C:\Sun\SDK\jdk\jre\lib\ext", I then added it as an external JAR in my project in eclipse, now in my class I have this code:

public void initialiseDatabase()
        {
            try { 
            // Load the Driver class. 
            Class.forName("com.mysql.jdbc.Driver"); 

            //Create the connection using the static getConnection method 
            databaseConnection = DriverManager.getConnection (databaseUrl+databaseName, 
                    dbUserName, dbPassword);
            sqlStatement = databaseConnection.createStatement();
            }
            catch (SQLException e) {e.printStackTrace();} 
            catch (Exception e) {e.printStackTrace();}
        }

(this is going to be psuedocode cause I am reading from a properties file and don't want the one helping me reading through long lines of code from main to figure out all the variables), where databaseUrl = "127.0.0.1" dbUserName = "root" dbPassword = "root" databaseName = "MySQL" //this one I am not sure of, do I need to create it or is it set inherenrly?

now the MySQL server is up and running, but when I call the method initialiseDatabase the following exception is thrown: "java.sql.SQLException: No suitable driver found for rootroot at java.sql.DriverManager.getConnection(Unknown Source) at java.sql.DriverManager.getConnection(Unknown Source) at Proxy$JDBCConnection.initialiseDatabase(Proxy.java:721)"

when line 721 is: sqlStatement = databaseConnection.createStatement();

Where have I gone wrong?

thanks

+1  A: 

Your database url should look like this:

jdbc:mysql://host:port/database

Example, if you use localhost, the default port and a database named cachedb, your url would be:

jdbc:mysql://localhost/cachedb
Guillaume
what about the databasename?databaseName = "MySQL" //this one I am not sure of, do I need to create it or is it set inherenrtly?
Noona
You need to create the database on the MySQL server that you want to access.
nos
Yeah I needed to start MYSQL command client and type therecreate database cachedb
Noona
Ok, if i want to execute this query in the database cachedb:CREATE TABLE Cache( String URL PRIMARY KEY NOT NULL, Response TEXT, LastModified VARCHAR(40))how do I tell it to create it on the database cachedb?because I am getting the error "No database selected"
Noona
and I found out that I need to type this in MySQL client to select my database:mysql> use cachedb
Noona