2

I am told that i do not need the JDBC connector. I have the database endpoint. I am making a JDBC call but am not able to connect to the DB. The piece of code that I have written is as follows:

package com.test;

import java.sql.*;

public class Test {

public static void main(String[] args) {
    String host = "jdbc:mysql://myaddress.us-west-2.rds.amazonaws.com";
    String uName = "admin";
    String uPass = "admin";
    System.out.println("-------- MySQL JDBC Connection Demo ------------");
    try
    {
        Class.forName("com.mysql.jdbc.Driver");
    }
    catch (ClassNotFoundException e) {
        System.out.println("MySQL JDBC Driver not found !!");
        return;
    }
    System.out.println("MySQL JDBC Driver Registered!");
    Connection connection = null;
    try {
        connection = DriverManager
            .getConnection(host, uName, uPass);
        System.out.println("SQL Connection to database established!");

    } catch (SQLException e) {
        System.out.println("Connection Failed! Check output console");
        return;
    } finally {
        try
        {
            if(connection != null)
                connection.close();
            System.out.println("Connection closed !!");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

}

  • What error do you get? – user432 May 13 '14 at 10:30
  • The output is:-------- MySQL JDBC Connection Demo ------------ MySQL JDBC Driver Registered! Connection Failed! Check output console Connection closed !! My question is how to we connect to the database with the database endpoint and password without using jdbc? Is jdbc causing the error here? I was told that i do not need jdbc and connect directly. – user3631986 May 13 '14 at 10:36
  • Can you give us the stacktrace? Print it out with `e.printStackTrace();` when you catch the Exception. – user432 May 13 '14 at 10:37
  • Access denied for user 'root'@'ip' (using password: YES) Connection closed !! at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3609) – user3631986 May 13 '14 at 10:44
  • at java.lang.reflect.Constructor.newInstance(Constructor.java:408) at com.mysql.jdbc.Util.handleNewInstance(Util.java:411) at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:381) at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:305) at java.sql.DriverManager.getConnection(DriverManager.java:664) at java.sql.DriverManager.getConnection(DriverManager.java:247) at com.test.Test.main(Test.java:24) – user3631986 May 13 '14 at 10:45
  • Include the exception in your question. And your problem is you are trying to access the database with the root user from a remote computer which is not allowed for security reasons. – user432 May 13 '14 at 10:46
  • Hey thanks a lot for your prompt replies but i was told that i could access it from a remote computer. I have a username and password but cannot share it on the public forum. I have a database table name but i am not sure how to append it to the url or do i even have to do that? – user3631986 May 13 '14 at 11:02
  • This is getting way too detailed for the comment section but from the exception it seems that you try to use the root user to access the database and that is not going to work from a remote PC without modifications. Your code is fine otherwise. Here is another question that may help: http://stackoverflow.com/questions/9766014/connect-to-mysql-on-amazon-ec2-from-a-remote-server – user432 May 13 '14 at 11:10

1 Answers1

0

This looks like the user isn't permitted to connect from the host you're using, or you've got the password wrong. Are you using the RDS? Try connecting using the master user.

If the DB is running on another EC2 instance, then connect locally, and try creating a new user with permission to connect from anywhere:

CREATE USER 'monty'@'%' IDENTIFIED BY 'some_pass';

http://dev.mysql.com/doc/refman/5.0/en/adding-users.html

Daniel Scott
  • 7,418
  • 5
  • 39
  • 58