|
|
|
|
|
|
S*******C 发帖数: 822 | 1 我已经可以用windows命令行成功连接远端Amazon EC2 MySQL server,并且设置完数据
库和表格。
接下来要解决怎么在装上Amazon AWS插件的Eclipse IDE开发的JAVA代码中配置连接这
个MySQL数据库。
按照AWS官方教程
http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create_de
Establish a database connection in your code using your Amazon RDS DB
Instance's connectivity information. You can access your connectivity
information using environment variables. The following shows how you would
connect to the database on an RDS instance.
String dbName = System.getProperty("RDS_DB_NAME");
String userName = System.getProperty("RDS_USERNAME");
String password = System.getProperty("RDS_PASSWORD");
String hostname = System.getProperty("RDS_HOSTNAME");
String port = System.getProperty("RDS_PORT");
Build your JDBC connection string:
String jdbcUrl = "jdbc:mysql://" + hostname + ":" + port + "/" + dbName + "?
user=" + userName + "&password=" + password;
用以上方法不需要输入自己的数据名称、用户名、密码和端口。
但我怎么也连接不上啊。
直接用下面的URL也连接不上
jdbc:mysql://mydbinstance.abcdefghijkl.us-east-1.rds.amazonaws.com:3306/
employees?user=sa&password=mypassword
跪求大牛指教!谢谢 | S*******C 发帖数: 822 | 2 package com.cdd.jdbc;
import java.sql.*;
public class CreateConnection {
public Connection conn=null;
public ResultSet rs=null;
public Statement stmt=null;
public CreateConnection() { //构造方法
}
//获取数据库连接方法
public Connection getConnection() {
try {
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://mydbinstance.XXXXXXX.
us-east-1.rds.amazonaws.com:3306/db_database05","root","11111");
//不管用上面这句,还是下面注释掉的部分都无法连接!
/*
String dbName = System.getProperty("RDS_DB_NAME");
String userName = System.getProperty("RDS_USERNAME");
String password = System.getProperty("RDS_PASSWORD");
String hostname = System.getProperty("RDS_HOSTNAME");
String port = System.getProperty("RDS_PORT");
String jdbcUrl = "jdbc:mysql://" + hostname + ":" + port + "/" +
dbName + "?user=" + userName + "&password=" + password;
conn=DriverManager.getConnection(jdbcUrl);
*/
} catch (Exception e) {
e.printStackTrace();
}//异常处理
return conn;//按发法要求返回个Connection对象
}
/*
*功能:关闭数据库的连接
*/
public void close() {
try {
if (rs != null) {
rs.close();
}
}
catch (Exception e) {
e.printStackTrace(System.err);
}
try {
if (stmt != null) {
stmt.close();
}
}
catch (Exception e) {
e.printStackTrace(System.err);
}
try {
if (conn != null) {
conn.close();
}
}
catch (Exception e) {
e.printStackTrace(System.err);
}
}
}
求救! | x*******6 发帖数: 262 | |
|
|
|
|
|