Java program to dump table as set of insert statements - java

Is there a Java program to dump a table as a set of insert statements. I want to save state of a database table.
All I have is JDBC URL to DB which is located on network. Don't have Oracle installed thus no utilities and no SSH access either.
Looking for something similar to mysqldump or CSVWRITE in H2.

To save a state of a table you have several options:
Make a copy of the table data in another table.
Export using the oracle EXP or EXPDMP utilities.
Export into insert statements using external tools such as Toad,
SQLDeveloper (free from Oracle), PLSQL Developer. Or look for an online example and/or write your own.
It all depends on how you want to use the copy of your data and how much it is. For quick testing work a copy of the data in another table works best for me. With a simple sql script your table is back to it's original state.

Related

Is it possible to access an SQLite database or (Sql browser for SqLite) from Java?

I want to work very portable, so I don't install a monster of Sql Server which has 2.7Gb size on my computer. I want to find an easier solution. I have for example a form application in Java which must to access a database (maybe a server), I thought it's easier for me to work with a portable database (Access or SqlLite).
It can be an installer solution but not very big such as 1Gb size or more. Initially thought to use a text file or Excel for keep the data but this way is hard too because I can't easily simulate the constraints and relationships between lines and tables.
Of course it is possible to access an SQLite database from Java! As you can read here:
A database in SQLite is a single disk file¹.
So you just need to use the driver to read/write from/to this database (file).
The code in Java would look like the following (taken from here):
How to Specify Database Files
Here is an example to select a file C:\work\mydatabase.db (in Windows)
Connection connection = DriverManager.getConnection("jdbc:sqlite:C:/work/mydatabase.db");
A UNIX (Linux, Mac OS X, etc) file /home/leo/work/mydatabase.db
Connection connection = DriverManager.getConnection("jdbc:sqlite:/home/leo/work/mydatabase.db");

Using HSQLDB as a portable database with Spring, Hibernate

I am newbie in hsqldb.
In the project I am using Spring 4, Hibernate 5 and HSQLDB.
I am having some specific task and I am trying to use HSQLDB as a portable database, which can be transferred to a flash drive or another computer.
I already have an sql-script with all tables and basic-needed data.
I have four questions that are haunting me.
(I'm sorry in advance if these questions are very stupid):
I need to make the script run at the first launch of the program, and in the other launсhes it must to check if database already exist and (if it already exists)only update data in it. (the program would be used in many computers and the database must be created after the first launch).
How can I do this? Is it possible? Can you give some basic advice or example about how I can to do that?
I am trying to find some information about saving all database info in some file in the file-system. Can you give me please some valid examples about saving hsqldb data in file and about using this file after another launch.
Can I place this file in my project.jar file and to work with all data from it update it e.t.c. ?
What is the best practice to make my database portable(for specific tasks) and where should I keep it? In file, in my project jar.e.t.c.?
Thanks in advance for your answers!
For data storage, you use a file: database. The JDBC connection URL is in the form jdbc:hsqldb:file:<file path>. HSQLDB will save all the data to file.
After connecting to the database you execute the SQL statements in your script one by one. If the tables already exist, the CREATE TABLE statements throw an error. This shows you don't have to execute them.
Because it's up to you when to keep the existing data and when to update it based on the existing contents of the database, you execute some SQL statements to decide. There is no automatic way to do this.
You can put an HSQLDB database in a jar but it cannot be updated. Jars are read-only.
The databases are fully portable. You can place them in a subdirectory of the user's home directory with the ~ symbol. See http://hsqldb.org/doc/2.0/guide/dbproperties-chapt.html#dpc_variables_url and the rest of this page for details.

Execute sql server script from java

I am using sql server, my issue is when I change any stored procedure or database table changes I need to make track of it and need to update it on each and every server when my related java code is uploaded.
So I am finding something tool or something if possible with java code that if I give stored procedure or database changes file(i.e .sql file) it executes it on sql server.
May be my question is silly but I really want to know about is this possible with any tool or through code?
If I understand you correctly, the scenario would be:
Read .sql file using one of the Readers
Execute SQL commands using JDBC and Statement class.

Creating a database in distributed Java programs

I cannot understand how distributing Java programs that use a database works.
Let's say I am using Derby as RDBMS and I want to store tasks and calendar entries in a database.
I want each user of the program to have a local database.
But I don't understand how in-memory databases are supposed to work. Should I write a script so that the first time my program is launched it creates a database and empty tables? Or will they be already created during the installation of the program?
If your program wants to store the user's tasks and calendar entries in a database, you probably don't want to use an in-memory database, because the in-memory database disappears when your program exits.
Rather, you want to use an ordinary persistent Derby database, which will store the user's data in files in a folder on the filesystem.
You do indeed have to create the database and issue the CREATE TABLE etc. statements to create the tables in that database. You could provide that as a separate script, or you could have your program issue those statements itself.
Tables are not automatically created, though; you have to issue the CREATE TABLE statements one way or another.

Copying MySql tables to Sqlite with java

I need to have few tables contained in a MySql db transferred to an Sqlite database through Java code.
For this, in my application, I do the following:
create an Sqlite db
select * from each table(of mysql db) , fetch data corresponding to each field from resultset, and insert data of each field to the corresponding sqlite table.
Is this the correct approach? Are there better methods of doing this?
Any help is appreciated.
Export the schema from MySQL with insert scripts, edit it (remove the MySQL-specific syntax), then execute it in SQLite.
If this is something that only happens once, you might consider using sqlite .import command (http://www.sqlite.org/sqlite.html) and using mysqldump to dump the data into a csv file that can be read by .import
Look at this python approach. may this can help you
https://gist.github.com/2972461

Categories