Ad

Java: Database Connection. Where Is My Mistake?

- 1 answer

Hy!

I really need your help. I'm trying to connect a database to a java program. This is my first time, so I have no idea what i'm doing.

My sql server: Sql server

I want to connect the Test database to the java program.

I tried the following code:

Class.forName("oracle.jdbc.driver.OracleDriver");
    Connection conn = null;
    conn = DriverManager.getConnection("jdbc:mysql:WIN2CNG9\\SQLEXPRESS:3306/Test", "win2cng9\\kmim1437", "");
    conn.close();

The error: Exception in thread "main" java.sql.SQLException: No suitable driver found for jdbc:mysql:WIN2CNG9\SQLEXPRESS:3306/Test at java.sql.DriverManager.getConnection(DriverManager.java:689) at java.sql.DriverManager.getConnection(DriverManager.java:247) at javaapplication42.JavaApplication42.main(JavaApplication42.java:24) Java Result: 1

What is my mistake?

Thank you for any help.

Ad

Answer

Class.forName("oracle.jdbc.driver.OracleDriver");

DriverManager.getConnection("jdbc:mysql:

Why your use Oracle driver with mysql database? Use following code:

Class.forName("com.mysql.jdbc.Driver");
Connection conn = null;
    conn = DriverManager.getConnection("jdbc:mysql:WIN2CNG9\\SQLEXPRESS:3306/Test", "win2cng9\\kmim1437", "");
    conn.close();
Ad
source: stackoverflow.com
Ad