KeanuHester
KeanuHester

Reputation: 1

how import database using Java

I have to import a database through java, I have the database in file and I have to be able to read the file and directly import this database to mySQL, how can I do it? The code below is to read the sql file, but I don't know how to import it to mysql once the file is read.

public static void importBD (Connection connection) throws SQLException{
        Statement s= connection.createStatement();
        s.executeUpdate("USE example");
        s.close();
}
    
public static void importDataBase(Connection connection) throws SQLException, IOException{
        BufferedReader bf = new BufferedReader(new FileReader("example.sql"));
        Statement s = connection.createStatement();
        String line="", db="";
        while((line=bf.readLine())!=null){
            if(!line.startsWith("--") && !line.equals("")){
                db += line;
                if(db.contains(";")){
                    s.execute(db);
                    db = "";
                }
            }
        }
        s.close();
        bf.close();
}

How can I do it?

Upvotes: 0

Views: 26

Answers (0)

Related Questions