简体   繁体   English

将Java连接到Amazon服务器中的MYSQL数据库

[英]Connecting java to MYSQL database in an amazon server

I am trying to connect my java code with mysql DB in amazon rds , i am using this code: 我正在尝试将我的Java代码与Amazon RDS中的 mysql DB连接起来,我正在使用以下代码:

        try {
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        connection = DriverManager.getConnection(
        "jdbc:mysql://HOST:port_number/DB_name","DB_username", "DB_pass");
        st = connection.createStatement();
        rs = st.executeQuery("SELECT * FROM table name WHERE id = 1");
    } catch (Exception e) {
        System.out.println("DB error : " + e);
    }

i am getting this error when i run it : 我在运行它时遇到此错误:

DB error : java.sql.SQLException: Unable to connect to any hosts due to exception: java.net.SocketException: java.net.SocketException: Permission denied: connect

** BEGIN NESTED EXCEPTION ** 

java.net.SocketException
MESSAGE: java.net.SocketException: Permission denied: connect

STACKTRACE:

java.net.SocketException: java.net.SocketException: Permission denied: connect
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:143)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:225)
at com.mysql.jdbc.Connection.createNewIO(Connection.java:1805)
at com.mysql.jdbc.Connection.<init>(Connection.java:452)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:411)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at DB.<init>(DB.java:15)
at subgroup.main(subgroup.java:72)


** END NESTED EXCEPTION **

any ideas !! 有任何想法吗 !!

You need to give access to your machine from where you are trying to read the database. 您需要从尝试读取数据库的位置授予对计算机的访问权限。

Also you should grant access to your IP using command prompt. 另外,您应该使用命令提示符授予对IP的访问权限。

http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/authorizing-access-to-an-instance.html http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/authorizing-access-to-an-instance.html

i rephrase your command because you set the connection variable as drivermanager but in your statement you use con ? 我改写您的命令,因为您将connection变量设置为drivermanager,但在声明中使用con and the string inside the connection replace it with the real value for example 连接内部的字符串用实际值替换,例如

connect = DriverManager.getConnection("jdbc:mysql://localhost:3306", "<your DBUsername>", "<your DB Password>");

dont just copy paste next time, learn how the codes work 下次不要复制粘贴,了解代码如何工作

try {
        Class.forName("com.mysql.jdbc.Driver");
        connection = DriverManager.getConnection(
        "jdbc:mysql://HOST:port_number/DB_name","DB_username", "DB_pass");
        st = connection.createStatement();
        rs = st.executeQuery("SELECT * FROM table name WHERE id = 1");
    } catch (Exception e) {
        System.out.println("DB error : " + e);
    }

In my case the problem was that I was using a connection from emulator to localhost. 就我而言,问题是我正在使用从仿真器到本地主机的连接。 If you use emulator to localhost dont use localhost,127.0.0.1 in connection String but use: 如果您使用模拟器将localhost用作本地主机,请不要在连接字符串中使用localhost,127.0.0.1,而应使用:

10.0.2.2 10.0.2.2

Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM