How to load a SQL file (stored in the source folder of my Java project) into MySQL from a Java Application?

穿精又带淫゛_ 提交于 2019-12-24 00:26:15

问题


I want to load an SQL file (which is stored in the source folder of my NetBeans Java Project) into MySQL from my Java Application during runtime. How can I do this?

The SQL file is 15.15 MB in size and I am wondering whether I can copy into a String or not? Or if I anyhow manage to copy it to a String (though it may throw out of memory error), how to execute multiple commands in one go (because the SQL file contains many commands).

I want to do it without using any additional tools. Just using pre-existing libraries and classes in java (that are bundled with JDK).


回答1:


I found that it is probably impossible to do it yet without adding additional tools and libraries.

We can do it by splitting the SQL file into smaller SQL files, each containing just one SQL command and then initiating a loop to execute all those files at once. I'd tried it and it works. It certainly does.

The following is the code I used to do that :

import javax.swing.* ;
import java.sql.* ;
import java.io.* ;

public class LoadSQLFile {
    public static void main(string args[ ]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    String password = JOptionPane.showInputDialog(null, "We need your MySQL Password to run the application. Please enter it here.", " MySQL Password ?", JOptionPane.QUESTION_MESSAGE) ;
                    Class.forName("java.sql.Driver") ;
                    Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/MyDB", "root", password) ;
                    Statement stmt = conn.createStatement() ;
                    int i = 0 ;
                    for(i=1;i<=16;i++) {
                        FileReader fr = new FileReader("src//sql_files//BCK"+i+".sql") ;
                        BufferedReader br = new BufferedReader(fr) ;
                        stmt.execute(br.readLine()) ;
                    }
                    stmt.close();
                    conn.close();
                    JOptionPane.showMessageDialog(null, " Records Successfully Inserted into database !", "Success !", 1) ;
                } catch(Exception e) {
                    JOptionPane.showMessageDialog(null, e, "ERROR", JOptionPane.ERROR_MESSAGE) ;
                }
            }
        });
    }
}

I was to execute SQL files stored in my project's source folder inside a package named "sql_files". The names of the files are - BCK1.sql, BCK2.sql, ...., BCK16.sql. Each file contained just one SQL file in just first line. It worked just fine for me.

I used the MySQL JDBC Driver for this.




回答2:


I think ScriptUtils.executeSqlScript can help you.



来源:https://stackoverflow.com/questions/41759298/how-to-load-a-sql-file-stored-in-the-source-folder-of-my-java-project-into-mys

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!