Derby Embedded Database Create Table Structure - java

I am trying create a swing application with embedded database using netbeans IDE. First in the netbeans services tab i have created a database and connected using embedded driver. Then i have created all the tables and relationships there.
In the swing application i connected embedded database like this
private static String dbURL = "jdbc:derby:derbysample;create=true;user=root;password=root";
Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
//Get a connection
conn = DriverManager.getConnection(dbURL);
This creates the database and i could able to connect. But i doesn't create tables that i created. It's is standalone application. So for different machines it need to create database and tables. There are many solution?
Create programatically tables [Add another java file to create tables and relationship]
Get Schema of Database and build it.
My Question is "is there any easy way to add database structure to embedded database in standalone application?
Abstract of question is:-
Using a derby embedded database with large number of tables and relations. What is the best solution?
Create tables Programaticallly - How can i make it as runs only one
time for each install
Use ant build tools to generate tables
Is there any other ways?

Am assuming what you want is to create a database with specific tables during installation and use the same afterwards.
Derby is light weight database stored in single directory. Having said that creating tables is as simple as executing create table sql queries. Run this while you install.
create=true; in connection string means create an empty database if a matching one is not found (at the given path). This should be done only done at the time of installation.
You mentioned that you created the database with tables. While connecting from application you got an empty database.
Looks the application is not able to access the previously created database at given path (derbysample directory at working directory) and is creating a new database. Try giving the full path without create=true in connection string
private static String dbURL = "jdbc:derby:d:\\somepath\\derbysample;user=root;password=root";
This will force application to use an existing (previously created) database and throw an exception if the same is not found.

Related

AS400 DB2 SQL7008 : Table name not valid for operations when inserting a record via Spring Boot

We have schemas / libraries created directly by OS/400 commands in DB2. Hence journaling will not be enabled by default for any physical file (table) If we would create newly. We are using DB Migration tool like liquibase for all DB changes like table / view creation in spring boot. while trying to insert or update, I am getting error "java.sql.SQLException: [SQL7008] X in TABLE_NAME not valid for operation". This error is due to the journaling not done on the newly created table via liquibase. Now, I am trying to find the below possibilities If available
Is there any possibility of creating table (SQL) under the DB2 library 9created in OS/400) so that the journaling is not required while inserting or updating ?
Is there any possibility of creating a journal on a table via Java/Spring Boot?
or any suggestions rather than journaling the table everytime in DB2 side ?
Please give your comments
When commitment control (transaction isolation) is used, journaling of the tables is required.
You have two options:
turn off commitment control
Turn on journaling for the tables
For option 1, you can include
transaction isolation=none;
in the connection string, see this question for more detail
For option 2, if you use the SQL CREATE SCHEMA and CREATE TABLE commands, to create the library and files, then the tables will be automatically journaled.
You can also use the Start Journal Library (STRJRNLIB) command after creating a library via the Create Library (CRTLIB) command. Thereafter, when you create a table or physical file in the library it will be journaled automatically.
I had this error and fixed it using iAccess client tool.
You can add DB2 journal using IBM i Access client tool.
Steps
Open the schema ,
Right click on Tables , then click include on the right click menu.
MYTABLE (the table I want to add journal) will be appeared on the list, then right click on the table name then and go to journaling, now add the values for Journal and library

HSQLDB data storage as file in Spring Boot Application, unable to visualize

I am trying my hands on spring boot application. I planned to use HSQLDB for the database.
Purpose: Create User Table, Insert, Update, Delete data
I created User Entity, User dao, and saved user data in the user entity.
Everything is working fine.
What I want is to see the data in the table as we can see it for MySQL.
I tried to use razorSQL, Dbeaver but I can't see tables.
application.properties
spring.jpa.hibernate.ddl-auto: update
spring.jpa.hibernate.dialect=org.hibernate.dialect.HSQLDialect
spring.jpa.database: HSQL
spring.jpa.show-sql: true
spring.hsql.console.enabled: true
spring.datasource.url=jdbc:hsqldb:file:data/mydb
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driverClassName=org.hsqldb.jdbcDriver
I can see the User table data in the browser:
Files created in the data folder:
I have googled a lot but nothing helps.
Questions:
Can we see the data stored in HSQLDB(when running) as we can see for the MySQL in PHPMyAdmin?
In which file data is stored, I have seen that in the script file it saves all the statements (insert, delete etc). Do we have a separate file to store the data?
what is the use of tmp folder created?
Let me know if you need more details. I need to be clear on this. It has taken a lot of time still I am not satisfied
I am able to visualize the data of hsqldb with the help of hsqldb.jar
Assuming
Database folder named "data" is created which contains files with mydb.log, mydb.properties, mydb.script, mydb.tmp
Steps to visualize when using it as fileDb.
1. Download HSQLDB jar.
2. Extract in the folder where we have "data" folder(it contains database files) database files generated ("data" folder).
3. Now we are in the folder, where database folder is created. Run this command "java -cp hsqldb-2.4.1/hsqldb/lib/hsqldb.jar org.hsqldb.util.DatabaseManagerSwing" here "hsqldb-2.4.1" is the downloaded hsqldb folder. It will open up a UI.
4. In this UI, make a new connection, select type as "HSQL Database Engine Standalone" put URL as "jdbc:hsqldb:file:data/mydb" (here data is the folder and mydb is the DB name), give user and password as defined in application properties, then say ok. It should connect. (Maken sure the path to the file DB is relative to the folder from where we opened the UI)
Let me know in case anyone is getting errors
You can run HSQLDB as a Server and connect to it simultaneously from your Spring app and from a database utility such as dBeaver. The connection URL will look like jdbc:hsqldb:hsql://localhost/mydb. This is very similar to the way MySQL is used.
Detailed coverage is here: http://hsqldb.org/doc/guide/listeners-chapt.html but see the introduction to the Guide first. You can also consult various step-by-step HSQLDB tutorials on the web.

Database migration from Mysql To SQLServer using hibernate

I want to write a script which will transfer daily data from Mysql to SQL server.
I have decided to do this using hibernate.
There are three databases in mysql called db1, db2 & db3. I want to transfer data from all these databases to SQL server. Table structure on SQL server is identical to MYSQL. Simply we can say this script is to take backup of mysql data on sql server.
Now my question is:
Is there any other simple method to do this?
How can I connect to 3 different databases (db1,db2,db3) of mysql as well as sql server using hibernate?
in hibernate.cfg.xml url property takes one database name only:
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/db1</property>
So in this way I need to write 6 hibernate.cfg.xml files to connect to mysql as well as sql server.
One of the constructors for Configuration does take a file path:
public Configuration configure(String resource) throws HibernateException {
standardServiceRegistryBuilder.configure( resource );
// todo : still need to have StandardServiceRegistryBuilder handle the "other cfg.xml" elements.
// currently it just reads the config properties
properties.putAll( standardServiceRegistryBuilder.getSettings() );
return this;
}
So... what you'll need to do is copy your hibernate.cfg.xml file as many times as you need and configure different sessions using the standard way.

How to see all tables in my h2 database at localhost:8082?

I use JDBC and created h2 database called usaDB from sql script. Then I filled all tables with jdbc.
The problem is that after I connect to usaDB at localhost:8082 I cannot see on the left tree
my tables. There is only INFORMATION_SCHEMA database and rootUser which I specified creating usaDB.
How to view the content of tables in my h2 database?
I tried query SELECT * FROM INFORMATION_SCHEMA.TABLES.
But it returned many table names except those I created. My snapshot:
I had the same issue and the answer seems to be really stupid: when you type your database name you shouldn't add ".h2.db" suffix, for example, if you have db file "D:\somebase.h2.db" your connection string should be like "jdbc:h2:file:/D:/somebase". In other way jdbc creates new empty database file named "somebase.h2.db.h2.db" and you see what you see: only system tables.
You can use the SHOW command:
Using this command, you can lists the schemas, tables, or the columns of a table. e.g.:
SHOW TABLES
This problem drove me around the twist and besides this page I read many (many!) others until I solved it.
My Use Case was to see how a SpringBatch project created in STS using :: Spring Boot :: (v1.3.1.RELEASE) was going to behave with the H2 database; to do the latter, I needed to be able to get the H2 console running as well to query the DB results of the batch run.
This is what I did and found out:
Created an Web project in STS using Spring Boot:
Added the following to the pom.xml of the latter:
Added a Spring configuration file as follows to the project:
This solves the Web project deficiencies in STS. If you run the project now, you can access the H2 console as follows: http://localhost:8080/console
Now create a SpringBatch project in STS as follows (the alternative method creates a different template missing most of the classes for persisting data. This method creates 2 projects: one Complete, and the other an initial. Use the Complete in the following.):
The SpringBatch project created with STS uses an in memory H2 database that it CLOSES once the application run ends; once you run it, you can see this in the logging output.
So what we need is to create a new DataSource that overrides the default that ships with the project (if you are interested, just have a look at the log messages and you will see that it uses a default datasource...this is created from:
o.s.j.d.e.EmbeddedDatabaseFactory with the following parameters:
Starting embedded database: url='jdbc:hsqldb:mem:testdb', username='sa')
So, it starts an in memory, and then closes it. You have no chance of seeing the data with the H2 console; it has come and gone.
So, create a DataSource as follows:
You can of course use a properties file to map the parameters, and profiles for different DataSource instances...but I digress.
Now, make sure you set the bit that the red arrow in the picture is pointing to, to a location on your computer where a file can be persisted.
Running the SpringBatch (Complete project) you should now have a db file in that location after it runs (persisting Person data)
Run the Web project you configured previously in these steps, and you WILL :=) see your data, and all the Batch job and step run data (et voila!):
Painful but rewarding. Hope it helps you to really BOOTSTRAP :=)
I have met exactly this problem.
From what you describe, I suppose that you connect your jdbc with the "real" h2 server, but you are connecting on web application to database by the wrong mode (embedded-in-memory mode, aka h2mem). It means that h2 will create a new database in-memory, instead of using your true database stored elsewhere.
Please make sure that when you connect to this database, you use the mode Generic H2 (Server), NOTGeneric H2 (Embedded). You can refer to the picture below.
Version of jar file and installed h2 database should be same.
If in case you have created and populated H2 database table using maven dependency in spring boot, then please do change the JDBC URL as jdbc:h2:mem:testdb while connecting to H2 using web console.
It is an old question, but I came across the same problem. Eventually I found out that the default JDBC URL is pointing a test server rather than my application. After correcting it, I could access the right DB.
I tried with both Generic H2 (Embedded) and the Generic H2 (Server) options, both worked as long as the JDBC URL: is provided correctly.
In grails 4.0.1 the jdbc URL for development is jdbc:h2:mem:devDb. Check your application.yml file for the exact URL.
For the people who are using H2 in embedded(persistent mode) and want to "connect" to it from IntelliJ(other IDEs probably apply too).
Using for example jdbc url as follows: jdbc:h2:./database.h2
Note, that H2 does not allow implicit relative paths, and requires adding explicit ./
Relative paths are relative to current workdir
When you run your application, your workdir is most likely set to your project's root dir
On the other hand, IDE's workdir is most likely not your project's root
Hence, in IDE when "connecting" to your database you need to use absolute path like: jdbc:h2:/Users/me/projects/MyAwesomeProject/database.h2
For some reason IntelliJ by default also adds ;MV_STORE=false. It disables MVStore engine which in fact is currently used by default in H2.
So make sure that both your application and your IDE use the same store engine, as MVStore and PageStore have different file layouts.
Note that you cannot "connect" to your database if your application is using it because of locking. The other way around applies too.
In my case the issue was caused by the fact that I didn't set the h2 username, password in java. Unfortunatelly, Spring didn't display any errors to me, so it was not easy to figure out. Adding this lines to dataSource method helped me fix the issue:
dataSource.setUsername("sa");
dataSource.setPassword("");
Also, I should have specified the schema when creating tables in schema.sql
Selecting Generic H2 (Server) solved for me. We tempted to use default Generic H2 (Embedded) which is wrong.

How to identify the script file from where HSQL datatbase is populated?

There is an already existing Java project that extensively uses Hibernate and Spring. And for unit testing it uses an in-memory HSQL data base. I am trying to figure out WHERE this database is getting populated from.
Can someone tell whence I can start looking? Or how to find the file containing the script for creation the tables?
Btw, the tables DO get created. That I confirmed by opening the Database Manager.
If your connection URL begins with jdbc:hsqldb:mem: then this is not populated by HSQLDB but by Hibernate or Spring.
If the connection URL begins with jdbc:hsqldb:file: then the specified file path is the location of the HSQLDB .script file.

Categories