简体   繁体   English

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

[英]how to connect to a mysql database through eclipse

I have an application running in eclipse kepler my database is on the AWS RDS MySQL type. 我有一个在eclipse kepler中运行的应用程序,我的数据库是AWS RDS MySQL类型。 I have imported : mysql-connector-java-gpl-5.1.31-.msi and aws-java-sdk-1.8.5.jar 我已经导入了:mysql-connector-java-gpl-5.1.31-.msi和aws-java-sdk-1.8.5.jar

I want to perform operations such as to create a new table, or insert update data etc on my RDS instance through java code, but AWS RDS hasnt provided APIs for the same. 我想通过Java代码在RDS实例上执行诸如创建新表或插入更新数据等操作,但是AWS RDS尚未提供相同的API。 So I have been trying the following way : 所以我一直在尝试以下方式:

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();
}
}
}

it is showing me the following error : 它向我显示以下错误:

com.mysql.jdbc.JDBC4ResultSet@7baf7d com.mysql.jdbc.JDBC4ResultSet@7baf7d java.io.FileNotFoundException: mySQLFile.sql (The system cannot find the file specified) at java.io.FileInputStream.open(Native Method)com.mysql.jdbc.JDBC4ResultSet@7baf7d com.mysql.jdbc.JDBC4ResultSet@7baf7d com.mysql.jdbc.JDBC4ResultSet@7baf7d java.io.FileNotFoundException:mySQLFile.sql(系统找不到指定的文件),位于java.io.FileInputStream.open(本机方法)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)

please help. 请帮忙。 Also suggest if there's any other way to access/update the tables in AWS RDS 还建议是否还有其他方法可以访问/更新AWS RDS中的表

You are messing everything. 你搞砸了一切。 No need to have aws-java-sdk-1.8.5.jar imported. 无需导入aws-java-sdk-1.8.5.jar。 This is aws-sdk java jar. 这是aws-sdk Java jar。 For connecting to the RDS instance having MySql database. 用于连接到具有MySql数据库的RDS实例。 All you need is to import mysql-connector-java-5.1.18-bin.jar and have the access and credentials for the database launched in the RDS. 您所需要做的就是导入mysql-connector-java-5.1.18-bin.jar并具有在RDS中启动的数据库的访问权限和凭据。 Get the permission to connect to the database from your IP(Machine where code will run) And this code will connect to the database and you can execute the queries based on your credentials access. 从您的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());
    }
}

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

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

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

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