JDBC-Mysql Amazon EC2 Connection

此生再无相见时 提交于 2021-02-08 03:25:50

问题


I'm new to servers and databases. I've been trying to test out a java application that connects, reads and modifies a Mysql database that is hosted on amazon ec2.

System.out.println("-------- MySQL JDBC Connection Testing ------------");
    String DNS = "MYDNS";
            String myDBname = "DBNAMe"
            String MYSQLUSER = "USER";
            String MYSQLPW = "PW";
    try {

        Class.forName("com.mysql.jdbc.Driver");

    } catch (ClassNotFoundException e) {

        System.out.println("Where is your MySQL JDBC Driver?");
        e.printStackTrace();
        return;

    }

    System.out.println("MySQL JDBC Driver Registered!");
    Connection connection = null;

    try {
        connection = DriverManager.getConnection("jdbc:mysql://" +DNS+@"/"+myDBname, MYSQLUSER, MYSQLPASS)


    } catch (SQLException e) {
        System.out.println("Connection Failed! Check output console");
        currentDir = "broke";
    }

    if (connection != null) {
        System.out.println("You made it, take control your database now!");
    } else {
        System.out.println("Failed to make connection!");
    }

The issue is that despite being able to connect using the credentials via Mysql workbench, adding 3306 to my security group, binding 0.0.0.0 to mysql config, I still can't get a connection going (after doing some searching of other similar problems on SO)

Anyone have any insight as to what could be wrong? I have tried many variations of the getConnection() arguments.


回答1:


connection = DriverManager.getConnection("jdbc:mysql://username@ec2-host/myDBname, MYSQLUSER, MYSQLPASS)



来源:https://stackoverflow.com/questions/13428077/jdbc-mysql-amazon-ec2-connection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!