-1

I'm trying to establish a connection to my database (MySQL) and I have searched a code online that looks like this, but what he uses is an Oracle database, not a MySQL database which is what I have used.
Can somebody help? This is my code...

public static Connection DB()
{
    try {
        Class.forName("oracle.jdbc.driver.OracleDriver"); //This is my problem
        Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/Image","root","");
        return con;
    } catch (ClassNotFoundException | SQLException e) {
        JOptionPane.showMessageDialog(null, e);
    }
    return null;
}

[This is the image of my error message.][1]

Stacy
  • 1
  • 3
  • 1
    change the `Class.forName("com.mysql.jdbc.Driver")` – Lokesh Pandey Aug 21 '18 at 13:52
  • Here is the link for mysql connectivity https://www.javatpoint.com/example-to-connect-to-the-mysql-database – Lokesh Pandey Aug 21 '18 at 13:53
  • 1
    The duplicate contains a lot of information on how to connect to MySQL. Be careful, some might be outdated, so read the comments too. – Kayaman Aug 21 '18 at 13:53
  • The current documentation contains good examples and is located here: https://dev.mysql.com/doc/connector-j/8.0/en/connector-j-usagenotes-connect-drivermanager.html#connector-j-examples-connection-drivermanager – JakeRobb Aug 21 '18 at 13:54
  • The next time you're searching online you might want to *keep searching* if the first one isn't what you're looking for. There are hundreds of examples on how to connect to MySQL, so it's ridiculous that you chose one that connects to Oracle. – Kayaman Aug 21 '18 at 13:56
  • Remember a java Class file can be decompiled with a Java decompiler which exposes your MySQL username and password.. So if this is a public program you better off making a webinterface on a other server which talks with the SQL server. And use a protocol like REST, SOAP, XML-RPC, JSON to talk between the Java program and the webinterface – Raymond Nijland Aug 21 '18 at 13:57

2 Answers2

0

replace this

Class.forName("oracle.jdbc.driver.OracleDriver");  
Connection 
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/Image","root","");

with this

Class.forName("com.mysql.jdbc.Driver");  
Connection con=DriverManager.getConnection(  
"jdbc:mysql://localhost:3306/Image","username","password"); 
Ashok Kumar N
  • 573
  • 6
  • 23
-1
Class.forName("com.mysql.jdbc.Driver");  
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/Image","root","");