开发者

Java Connect to database Error: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

开发者 https://www.devze.com 2023-03-25 19:40 出处:网络
Hey stupid question but I\'m having a hard time connecting my java program to a mysql database. Throwing an exception when I hit this line.

Hey stupid question but I'm having a hard time connecting my java program to a mysql database. Throwing an exception when I hit this line.

Class.forName(driverName).newInstance();

The driver name is com.mysql.jdbc.Driver. I've searched around a bit on google and found something about a mysql-connector.jar file that I'm apparently supposed to have but I really haven't looked into it yet. Thanks.

Entire code:

Connection connection = null;
    try
    {
        String driverName = "com.mysql.jdbc.Driver"; // MySQL MM JDBC driver 
        Class.forName(driverName).newInstance();

        String serverName = "*********";
        String database = "canteen_web3";
        String url = "jdbc:mysql://" + serverName + "/" + database;
        final String username = "*****";
        final String password = "******";
        connection = DriverManager.getConnection(url,username,password);
        System.out.println("Connected!");
    }

    catch(Exception ex)
    {
        throw new ICExce开发者_如何学JAVAption(ex.getMessage());
    }


Start your app with

java -classpath .:mysql-connector.jar MyClass

The colon separates two paths. The . is the directory you are in (and hopefully the class or the base package), the latter jar is the driver.

For further information refer to the various sources of documentation http://download.oracle.com/javase/1.5.0/docs/tooldocs/windows/classpath.html

0

精彩评论

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