Java 一次将数据插入多个数据库和表
我试图使用prepared语句插入到几个数据库和表中。是否可以插入多个数据库和表? 这是我试过的代码,你能告诉我下一步该怎么做吗,因为我被困在这部分了。正在从文本框捕获数据Java 一次将数据插入多个数据库和表,java,mysql,jdbc,Java,Mysql,Jdbc,我试图使用prepared语句插入到几个数据库和表中。是否可以插入多个数据库和表? 这是我试过的代码,你能告诉我下一步该怎么做吗,因为我被困在这部分了。正在从文本框捕获数据 Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost /mirroreddatabase","root",""); Connection con1 = (Connection) DriverManager.get
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);
您可以只使用一个连接,并向该连接的用户授予所有数据库上的权限,以便可以插入所有数据库 要在所有数据库上授予root权限,可以执行以下操作:
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;
您只需要将数据库作为前缀(即,您需要将插入到breastcancer\u genename
而不是插入到breastcancerdatabase.breastcancer\u genename
)
关于GRANT语句(来自MySQL文档):
GRANT语句向MySQL用户帐户授予权限。授予
还用于指定其他帐户特征,例如使用
安全连接并限制对服务器资源的访问。使用
GRANT,您必须具有GRANT OPTION特权,并且必须具有
您正在授予的特权
有关详细信息:您没有使用
con1
或con2
。此外,您可能更喜欢使用批处理。如果使用这种预处理方法,我将如何保存数据。con1和con2是不同数据库的连接不确定您打算同时做什么,但我注意到您有con,con1,con2,您的prepare语句都只使用con,这是一个错误,对吗?我喜欢保持简单,我会有4种不同的方法,每种方法都会将数据插入到自己的数据库中。如果需要确保全部或无,可以将事务包装在其周围。但是请记住,每个连接语句都应该使用.close()方法完成,最好位于try catch的最后一个块中。我正在尝试将数据插入多个数据库。是否可以这样做?抱歉,我是新来授权的。你能解释一下我的密码吗?我使用的是NetBeansGRANT这句话,您必须在MySQL上使用,而不是在Javaok中使用。我明白这一点。但是如何建立连接?我无法保存我的记录
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);