[英]JDBC connecting to MySQL on Amazon ec2
我當時還比較陌生,但是在啟動ec2實例並通過RDS安裝MySQL實例之后,我設法使用ssh(.pem文件)通過MySQL Workbench連接到它。
我的問題是,當我嘗試與jdbc連接時,似乎無法正確完成該操作,該如何進行身份驗證呢?
這是我的代碼,希望有人能給我一些提示:
public void create_table(){
Connection c = null;
Statement stmt = null;
try {
Class.forName("com.mysql.jdbc.Driver");
c = DriverManager.getConnection ("jdbc:mysql://127.0.0.1:3306/test","root", "password");
// c = DriverManager.getConnection ("jdbc:mysql://mydatabase.us-east-1.rds.amazonaws.com:3306/test","user="+"root"+"password=root", "");
System.out.println("Opened database successfully");
stmt = c.createStatement();
String sql = "CREATE TABLE USERS " +
"(ID INT PRIMARY KEY ," +
" DEVICE TEXT NOT NULL, " +
" NAME TEXT NOT NULL)";
stmt.executeUpdate(sql);
stmt.close();
c.close();
} catch ( Exception e ) {
System.err.println( e.getClass().getName() + ": " + e.getMessage() );
System.exit(0);
}
編輯
我忘記了一些重要的細節...
現在,在Web應用程序和MySQL服務器都在同一實例上運行之后, 我想建立通信。
謝謝!
您的SQL Syntax
不正確。 Text
值作為VARCHAR
類型插入SQL
。 您可以根據需要通過在VARCHAR(HERE)
中的小括號中更改來更改文本值的長度。 試試這個代碼。
//STEP 1. Import required packages
import java.sql.*;
public class JDBCExample {
// JDBC driver name and database URL
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost:3306/STUDENTS";
// Database credentials
static final String USER = "username";
static final String PASS = "password";
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try{
//STEP 2: Register JDBC driver
Class.forName("com.mysql.jdbc.Driver");
//STEP 3: Open a connection
System.out.println("Connecting to a selected database...");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
System.out.println("Connected database successfully...");
//STEP 4: Execute a query
System.out.println("Creating table in given database...");
stmt = conn.createStatement();
String sql = "CREATE TABLE USERS " +
"(ID INTEGER not NULL," +
" DEVICE VARCHAR(255) not NULL," +
" NAME VARCHAR(255) not NULL,"+
"PRIMARY KEY (ID))";
stmt.executeUpdate(sql);
System.out.println("Created table in given database...");
}catch(SQLException se){
//Handle errors for JDBC
se.printStackTrace();
}catch(Exception e){
//Handle errors for Class.forName
e.printStackTrace();
}finally{
//finally block used to close resources
try{
if(stmt!=null)
conn.close();
}catch(SQLException se){
}// do nothing
try{
if(conn!=null)
conn.close();
}catch(SQLException se){
se.printStackTrace();
}//end finally try
}//end try
System.out.println("Goodbye!");
}//end main
}//end JDBCExample
身份驗證應該如何完成?
您可以使用示例中使用的一種形式,但是兩者都存在錯誤
c = DriverManager.getConnection ("jdbc:mysql://127.0.0.1:3306/test","root", "password");
您連接到本地主機,如果您在RDS上有測試數據庫,則需要像第二個示例一樣引用RDS的端點
c = DriverManager.getConnection ("jdbc:mysql://mydatabase.us-east-1.rds.amazonaws.com:3306/test","user="+"root"+"password=root", "");
此處端點將是正確的,但是連接的字符串是錯誤的。 您可以使用以下表格
String jdbcUrl = "jdbc:mysql://mydatabase.us-east-1.rds.amazonaws.com:3306/test?user=root&password=password";
Connection con = DriverManager.getConnection(jdbcUrl);
要么
String url = "jdbc:mysql://mydatabase.test.us-east-1.rds.amazonaws.com:3306/";
String userName = "root";
String password = "password";
String dbName = "test";
Connection connection = DriverManager.getConnection(url + dbName, userName, password);
要精確地找到您的數據庫端點,請登錄到RDS控制台 (如果不是us-east-1,請確保選擇正確的區域),選擇數據庫,端點就在那里
您可能運行的另一個潛在問題是在安全組上
數據庫實例是使用安全組創建的,該安全組未授權來自運行MySQL應用程序或實用程序的設備或Amazon EC2實例的連接。 如果數據庫實例是在VPC中創建的,則它必須具有一個授權連接的VPC安全組。 如果數據庫實例是在VPC外部創建的,則它必須具有授權連接的數據庫安全組。
檢查RDS數據庫和ec2實例的安全組規則,並確保可以連接ec2實例有權訪問RDS服務器
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.