开发者

connecting to MySQL using JDBC in eclipse

开发者 https://www.devze.com 2022-12-27 10:54 出处:网络
So I want to create a JDBC connection to MySQL server that is installed on my pc, here are the steps,

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


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
0

精彩评论

暂无评论...
验证码 换一张
取 消