2

I have a situation where I need to copy data from HSQLDB text tables to MYSQL using java and INSERT into...as SELECT.... Not sure how to go from here.

...
Connection hcon = DriverManager.getConnection(param1); #hsql connection
...
Connection mscon = DriverManager.getConnection(param2); #mysql connection
...
Statement htmt = hcon.createStatement();
Statement mstmt = mscon.createStatement();
...
#How do I reference MySQL here?
ResultSet r = hstmt.executeQuery("insert into mysqlemp as select * from hsqlemp"); 

I am aware there is a LOAD DATA INFILE... to load text into table and its not an option for me.

2
  • HSQLDB comes with a utility which can copy tables between different databases. If you just need this one time, I suggest use that. Commented Aug 22, 2014 at 10:12
  • Possible duplicate of How to copy table from one database to another? Commented Sep 17, 2018 at 16:34

1 Answer 1

1

You are making the wrong approach. You can not access the MySQL database with the connection that was made to the HSQL database. You could do something like this:

PreparedStatement loadStatement = hcon.prepareStatement("SELECT data FROM table");
PreparedStatement storeStatement = mscon.prepareStatement("INSERT INTO table (data) VALUES (?)");

ResultSet loadedData = loadStatement.executeQuery();

while (loadedData.next()) {
    storeStatement.setString(1, loadedData.getString(1));
    storeStatement.executeUpdate();
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.