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

我有一个在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(系统找不到指定的文件),位于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)

请帮忙。还建议是否还有其他方法可以访问/更新AWS RDS中的表

回答:

你搞砸了一切。无需导入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());

}

}

以上是 如何通过eclipse连接到mysql数据库 的全部内容, 来源链接: utcz.com/qa/423708.html

回到顶部