如何通过eclipse连接到mysql数据库

时间:2014-07-28 11:09:05

标签: java mysql eclipse tomcat amazon-web-services

我有一个在eclipse kepler中运行的应用程序 我的数据库在AWS RDS MySQL类型上。 我已导入: mysql-connector-java-gpl-5.1.31-.msi和 AWS-java的SDK-1.8.5.jar

我想通过java代码执行诸如创建新表或在我的RDS实例上插入更新数据等操作,但是AWS RDS还没有提供相同的API。所以我一直在尝试以下方式:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.sql.DriverManager;
import com.amazonaws.AmazonClientException;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.rds.AmazonRDSClient;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;

public class try_AWS_RDS {

static AmazonRDSClient client;

private static void init() throws Exception {

AWSCredentials credentials = null;
try {
    credentials = new ProfileCredentialsProvider("default").getCredentials();
} catch (Exception e) {
    throw new AmazonClientException(
            "Cannot load the credentials from the credential profiles file.";
}    
client = new AmazonRDSClient(credentials);
Region r = Region.getRegion(myRegion);
client.setRegion(r);    
}
public static void main(String[] args) throws Exception {

    init();

    String s = new String();  
    StringBuffer sb = new StringBuffer(); 

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

    String userName = "myUserName";
    String password = "myPassword";
    String url = "jdbc:mysql://myDataBaseURL";

    Connection c = (Connection) DriverManager.getConnection(url, userName, password);  
    Statement st = (Statement) c.createStatement();  

    FileReader fr = new FileReader(new File("C:/Users/.../src/createTable.sql"));
    BufferedReader br = new BufferedReader(fr);  

    while((s = br.readLine()) != null)  
    {  
        sb.append(s);  
    }  
    br.close();  

    // here is our splitter ! We use ";" as a delimiter for each request  
    // then we are sure to have well formed statements  
    String[] inst = sb.toString().split(";");          

    for(int i = 0; i<inst.length; i++)  
    {  
        // we ensure that there is no spaces before or after the request string  
        // in order to not execute empty statements  
        if(!inst[i].trim().equals(""))  
        {  
            st.executeUpdate(inst[i]);  
            System.out.println(">>"+inst[i]);  
        }  
    } 


} catch (Exception e)
{
    e.printStackTrace();
}
}
}

它显示以下错误:

com.mysql.jdbc.JDBC4ResultSet@7baf7d com.mysql.jdbc.JDBC4ResultSet@7baf7d java.io.FileNotFoundException:mySQLFile.sql(系统找不到指定的文件)     at java.io.FileInputStream.open(Native Method)com.mysql.jdbc.JDBC4ResultSet@7baf7d

at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileReader.<init>(Unknown Source)
at try1_S3Sample.main(try1_S3Sample.java:96)

请帮忙。 还建议是否有任何其他方式来访问/更新AWS RDS中的表

3 个答案:

答案 0 :(得分:3)

你搞乱了一切。无需导入aws-java-sdk-1.8.5.jar。这是aws-sdk java jar。 用于连接到具有MySql数据库的RDS实例。您只需要导入mysql-connector-java-5.1.18-bin.jar并拥有在RDS中启动的数据库的访问权限和凭据。 获取从IP(将运行代码的计算机)连接到数据库的权限 此代码将连接到数据库,您可以根据您的凭据访问权限执行查询。

public static void main(String[] args) 
{
    try{
        String host = "jdbc:mysql://rds-ip:3306/rds-database-name";
        String uName = "rds-database-username";
        String uPass = "rds-database-pasword";
        Connection con = DriverManager.getConnection( host, uName, uPass );
        Statement stmt = con.createStatement();
        String sql = "SELECT * FROM tablename";
        ResultSet rs = stmt.executeQuery(sql);

        rs.next();
        int id_col = rs.getInt("id");
        String column1 = rs.getString("column1");
        String column2 = rs.getString("column2");
         }
         catch(SQLException err){
        System.out.println(err.getMessage());
    }
}

答案 1 :(得分:0)

因为错误只是一个FileNotFoundException,为了澄清它是否只是文件路径的问题(大写字母,反斜杠/斜杠...),首先尝试将文件放在根目录中,如C:\ file.sql

答案 2 :(得分:0)

很可能找不到C:/Users/.../src/createTable.sql