我正在尝试使用JDBC将数据从filemaker pro 11传输到MySQL。
我已经处理了建立到每一个的连接,并进行了有效的查询,并将数据安全地插入到MySQL中。
try {
results =
query.executeQuery("SELECT \"field one\", \"field two\" from table");
Connection con = DriverManager.getConnection("jdbc:mysql://website.com/database","user","password");
// Iterate through the results and print them to standard output
while (results.next()) {
String fname = results.getString("field one");
String lname = results.getString("field two");
System.out.println("Found user \"" + fname + " " + lname + "\"");
stmt = con.prepareStatement("INSERT ignore INTO table (idtable, name) values (?, ?)");
// some of the data I've been provided with is pretty horrific,
// so inserting safely is of large concern.
stmt.setString(1, fname);
stmt.setString(2, lname);
stmt.executeUpdate();
}
}
catch (SQLException e) {
System.out.println("Error retrieving data from database.");
e.printStackTrace();
//System.exit(1);
}
对于较小的表(4分钟内大约有100000条记录)来说,这是可行的,但其中一些表非常大,会导致应用程序崩溃:(。
这需要能够运行至少一次,做一个完整的人口,但在那之后,我可以限制产量,以拿起在说,上周的变化。
我以前在VB.net中写过这个,并构造了大的插入,但是我做了一个切换——我真的需要那个prepare语句,因为当前的数据库中有各种疯狂的字符。
谢谢,保罗
Error:
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
at java.lang.StringCoding$StringDecoder.decode(StringCoding.java:151)
at java.lang.StringCoding.decode(StringCoding.java:191)
at java.lang.String.<init>(String.java:451)
at java.util.jar.Attributes.read(Attributes.java:401)
at java.util.jar.Manifest.read(Manifest.java:199)
at java.util.jar.Manifest.<init>(Manifest.java:69)
at java.util.jar.JarFile.getManifestFromReference(JarFile.java:182)
at java.util.jar.JarFile.getManifest(JarFile.java:163)
at sun.misc.URLClassPath$JarLoader$2.getManifest(URLClassPath.java:710)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:238)
at java.net.URLClassLoader.access$000(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:212)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:435)
at com.mysql.jdbc.PreparedStatement.getInstance(PreparedStatement.java:872)
at com.mysql.jdbc.ConnectionImpl.clientPrepareStatement(ConnectionImpl.java:1491)
at com.mysql.jdbc.ConnectionImpl.prepareStatement(ConnectionImpl.java:4250)
at com.mysql.jdbc.ConnectionImpl.prepareStatement(ConnectionImpl.java:4149)
at datasync2.FMProConnection.companyQuoteInsert(FMProConnection.java:686)
at datasync2.DataSync2View.jButton1ActionPerformed(DataSync2View.java:220)
at datasync2.DataSync2View.access$800(DataSync2View.java:22)
at datasync2.DataSync2View$4.actionPerformed(DataSync2View.java:124)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2012)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2335)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:404)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:253)
at java.awt.Component.processMouseEvent(Component.java:6268)
最佳答案
分批做。
您可以从100K的批处理大小开始并增加它,直到性能下降。
1)尝试从源表的未处理行中选择有限数量的行。
2)批量插入
:
Statement stmt = con.createStatement();
stmt.addBatch("INSERT INTO employees VALUES (1000, 'Joe Jones')");
stmt.addBatch("INSERT INTO departments VALUES (260, 'Shoe')");
int[] insertCounts = stmt.executeBatch();
就像这里http://download.oracle.com/javase/1.3/docs/guide/jdbc/spec2/jdbc2.1.frame6.html
3)同时,记录哪些记录已成功处理。
(更新源行上的标志,或按特定顺序处理并保存最后一个标志)
4)处理错误,提交对数据库的更改,释放资源(关闭语句等)
循环1-4,直到成功处理源表中的所有记录。