一次性将数据插入多个数据库和表中

时间:2014-03-05 15:00:30

标签: java mysql jdbc

我正在尝试使用预准备语句插入到多个数据库和表中。是否可以插入多个数据库和表格? 这是我试过的代码,你能告诉我接下来应该做什么,因为我被困在这一部分。数据正在从文本框中捕获。

Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost   /mirroreddatabase","root","");
Connection con1 = (Connection) DriverManager.getConnection("jdbc:mysql://localhost/breastcancerdatabase","root","");
Connection con2 = (Connection) DriverManager.getConnection("jdbc:mysql://localhost/genedetailsdatabase","root","");
 String sql= "Insert into omimmirrored(id,genesymbol,sequencelinks)values (?,?,?) ";
 PreparedStatement pst=(PreparedStatement) con.prepareStatement(sql);

 String sql2 = "Insert into breastcancer_genename(id,symbol,geneName)values (?,?,?) ";
 PreparedStatement pst1=(PreparedStatement) con.prepareStatement(sql2);

 String sql3 = "Insert into genedetails(id,symbol,GeneDetailsLinks)values (?,?,?) ";
 PreparedStatement pst2=(PreparedStatement) con.prepareStatement(sql3);

 String sql4 = "Insert into breastcancer_synonym(id,symbol,geneName)values (?,?,?) ";
 PreparedStatement pst3=(PreparedStatement) con.prepareStatement(sql4);

1 个答案:

答案 0 :(得分:0)

您只能使用一个连接,并在所有数据库上向用户授予此连接权限,以便您可以插入所有数据库。

对于您可以执行的所有数据库的root权限:

GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;

您只需要将数据库作为前缀(即代替insert into breastcancer_genename放置insert into breastcancerdatabase.breastcancer_genename)。

Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost   /mirroreddatabase","root","");

 String sql= "Insert into omimmirrored(id,genesymbol,sequencelinks)values (?,?,?) ";
 PreparedStatement pst=(PreparedStatement) con.prepareStatement(sql);

 String sql2 = "Insert into breastcancerdatabase.breastcancer_genename(id,symbol,geneName)values (?,?,?) ";
 PreparedStatement pst1=(PreparedStatement) con.prepareStatement(sql2);

 String sql3 = "Insert into genedetailsdatabase.genedetails(id,symbol,GeneDetailsLinks)values (?,?,?) ";
 PreparedStatement pst2=(PreparedStatement) con.prepareStatement(sql3);

 String sql4 = "Insert into breastcancerdatabase.breastcancer_synonym(id,symbol,geneName)values (?,?,?) ";
 PreparedStatement pst3=(PreparedStatement) con.prepareStatement(sql4);

关于GRANT句子(来自MySQL文档):

  

GRANT语句授予MySQL用户帐户权限。 GRANT   还用于指定其他帐户特征,例如使用   安全连接和访问服务器资源的限制。使用   GRANT,你必须拥有GRANT OPTION特权,你必须拥有   你授予的特权。

有关详细信息:http://dev.mysql.com/doc/refman/5.1/en/grant.html