Richard Parker
Richard Parker

Reputation: 70

Java Wont connect to database no matter what driver

import java.sql.*;

public class Connect
{
   public static void main (String[] args)
   {
       Connection conn = null;

       try
       {
           String userName = "root";
           String password = "password123!";
           String url = "jdbc:oracle:thin:@localhost:3306:procomport";
           //Class.forName ("oracle.jdbc.driver.OracleDriver");
           conn = DriverManager.getConnection(url, userName, password);
                //Connection connection = DriverManager.getConnection(url , userName, password);
           System.out.println ("Database connection established");
       }
       catch (Exception e)
       {
           System.err.println ("Cannot connect to database server");
       }
       finally
       {
           if (conn != null)
           {
               try
               {
                   conn.close ();
                   System.out.println ("Database connection terminated");
               }
               catch (Exception e) { /* ignore close errors */ }
           }
       }
   }
}

This is my code I have multiple different databases but it wont connect to any of them what's the problem with this? I keep getting the error it cannot connect to the database. Although I can connect to it using other management tools is it a driver issue? How would I be able to tell if I had the drivers necessary?

Upvotes: 0

Views: 163

Answers (3)

Luke Woodward
Luke Woodward

Reputation: 64959

The code you've provided to connect to the database won't connect to either MySQL nor Oracle as it stands because it's a mish-mash of attempts to connect to both.

For Oracle, the code should look something like:

       String userName = "root";
       String password = "password123!";
       String url = "jdbc:oracle:thin:@localhost:1521:procomport";
       Class.forName("oracle.jdbc.driver.OracleDriver");
       conn = DriverManager.getConnection(url, userName, password);

(assuming you have a user called root on Oracle, and the Oracle SID is procomport). Note in particular the change of port number: MySQL typically uses 3306, Oracle uses 1521.

For MySQL the connection code should look like:

       String userName = "root";
       String password = "password123!";
       String url = "jdbc:mysql://localhost:3306/procomport";
       Class.forName("com.mysql.jdbc.Driver");
       conn = DriverManager.getConnection(url, userName, password);

(assuming your MySQL database is called procomport). Note the different style of connection URL and the driver class name.

The Oracle driver is typically in a JAR file named ojdbc6.jar, and the MySQL in a JAR named something like mysql-connector-java-5.1.18-bin.jar.

Finally, when you write something like

   catch (Exception e)
   {
       System.err.println ("Cannot connect to database server");
   }

you really aren't helping yourself. The exception e will almost certainly contain the reason why your database connection code isn't working, but by deliberately ignoring it you're making it much harder for yourself to figure out what has gone wrong.

To be honest with you, I'd be tempted to declare the main method throws Exception (by adding this to the end of the public static void main... line), and then you can delete your unhelpful catch block. If an exception is thrown and not handled within main, the JVM will print the stack trace for you before it exits.

Upvotes: 1

Suraj Chandran
Suraj Chandran

Reputation: 24791

  1. Uncomment the line Class.forName("oracle.jdbc.driver.OracleDriver");
  2. Make sure you have the Oracle dirver "oracle.jdbc.driver.OracleDriver" in the classpath

Upvotes: 0

sourcerebels
sourcerebels

Reputation: 5180

After your:

System.err.println();

Place a:

e.printStacktrace();

Then you will see real error message. Probably the driver classes are not in the classpath.

Hope this will help you

Upvotes: 0

Related Questions