I have to write JUNIT for repository class using HSQL DB.
I am not supposed to use any SQL command to load the data into HSQL DB.
Instead I need to use one external jar called abc.jar which will load all the schema and data from production database into HSQL.
abc.jar was working fine as standalone i.e. exporting schema and table from real database into HSQL .
In the current package I added abc.jar as dependent. When I call a method in abc.jar to export the data in #before of repository jUnit I'm getting HSQL connection exception.
Is it a possible approach?
It is a possible approach.
The connection URL in both the standalone setup and the test setup must be an absolute URL. And the export of data must occur in the same JVM process. If you are using a file: database, the path to database files must be absolute. If you are using a mem: database, the paths are absolute, provided they are used in the same JVM process.
Related
It is necessary to restore the h2 database. I have a file to recover, but its extension is .db, not .zip. I run h2 through the java -jar ./h2-1.4.190.jar -browser command. In the JDBC URL I indicate: jdbc:h2:file:/opt/backup/my.h2.db . I specify the password and login. The connection is made, I get to the database, but I don’t see my scheme. What should I do?
You shouldn't include a file name extension into JDBC URL. The valid URL for such file is jdbc:h2:/opt/backup/my;MV_STORE=FALSE;MVCC=FALSE.
Please note that 1.4.190 is a very old version, use it only if the database was really created with that version. (For the latest version of H2 exclude MVCC setting from the URL, it was removed from H2.)
Use a copy of your file. If it can't be opened normally, use a Recover Tool.
https://h2database.com/html/advanced.html#using_recover_tool
I am trying to run select query in jmeter. I am getting this response in listners "Cannot create PoolableConnectionFactory" I have attached my myphpadmin page which I use to create db. Thanks in advance.
.png
JDBC jar should be in lib folder:
Fully qualified name of driver class. (Must be in JMeter's classpath - easiest to copy .jar file into JMeter's /lib directory).
I assume you use your jar for MySQL DB:
The list of the validation queries can be configured with jdbc.config.jdbc.driver.class property and are by default:
MySQL
com.mysql.jdbc.Driver
You need to put the .jar into the "lib" folder of your JMeter installation (or other folder which is in JMeter Classpath
You need to restart JMeter to pick the .jar
Check out MySQL Database and JMeter - How to Test Your Connection for more information
On JMeter side check:
driver of MySQL (correct version) is in lib folder. The Jar not a zip.
The URL format is correct based on this document, there seems to be a space before database name in your URL
Login / password are also correct, check for strange characters or bad copy/paste
If all those are ok, then you're most probably facing same issues as the ones described here:
https://stackoverflow.com/a/2985169/460802
I've developed a small application using Spring Boot, which uses HSQLDB as a database to store users, for the time being. What I want to do is store the database within Maven's resource folder. My application.properties file contains the following configuration:
#...
spring.datasource.url=jdbc:hsqldb:file:src/main/resources/database/dashboard
#...
So, whenever I start the application with Java (right click on my Application's main() and Run As Spring Boot App), the database is stored right.
But, after packaging a WAR file with mvn clean install spring-boot:repackage, deploying it into Tomcat, e.g., and running it, the database is not stored within the exploded WAR.
My question is how can I tell spring.datasource.url=jdbc:hsqldb:file:* property to store the database within Maven Resources directory (which will point still after exploding the WAR)? Could I use some sort of environment variable as used in pom.xml?
EDIT
Given the answers by #Steve C and #fredt, I've realised that the database shouldn't be stored within the war. Instead, I'll store it within the user's Home dir spring.datasource.url=jdbc:hsqldb:file:~/tomcat_webapp_data/dashboard/database.. Thank you so much!
The directories and contents of a WAR file are read-only.
You can set the read_only flag in the HSQLDB database .properties file before including in the WAR. You access this kind of database with a jdbc:hsqldb:res:<path> URL.
If you want to store data in a persistent and updatable database, connect to the (at first non-existent) database within your application and set up its tables if they don't yet exist with data from a resource. You can then store data. The database path should be outside the directories that are used for jars and resources.
Contrary to one of the comments, HSQLDB is not limited to storing data in memory and can have disk-based tables, called CACHED tables.
You can include a variable in the database URL to pick up a pre-defined property from the web server. For example:
jdbc:hsqldb:file:${mydbpath}
See http://hsqldb.org/doc/2.0/guide/dbproperties-chapt.html#dpc_variables_url
Maven's src/main/resources directory is a build time path, not a runtime path.
If you're building a jar file, then everything in src/main/resources is copied to the root of the jar.
If you're building a war file, then everything in src/main/resources is copied to the root of the /WEB-INF/classes directory in the war.
Now, your JDBC URL jdbc:hsqldb:file:src/main/resources/database/dashboard is indicating a file with a relative path. At runtime this path is relative to the current working directory - and it's unlikely to exist at runtime anyway.
If you really want to build the database within your WAR, then given:
you're deploying an exploded WAR file (it's impossible to write to the content of the WAR file itself);
you really really want to store the database within the exploded WAR;
you want to put it in WEB-INF/database/dashboard (there's security implications if you store outside of the WEB-INF directory);
then you can compute the JDBC url using something like:
...
String databaseDirectoryName = servletContext.getRealPath("WEB-INF/database");
File databaseDirectory = new File(databaseDirectoryName);
if (!databaseDirectory.exists()) {
if (!databaseDirectory.mkdirs()) {
throw new RuntimeException("Failed to create database directory");
}
}
File databaseFile = new File(databaseDirectory, "dashboard");
String jdbcURL = "jdbc:hsqldb:" + databaseFile.toURI();
...
Getting that into your Spring configuration is an exercise for you; but using #Configuration and #Bean springs to mind as a way to do this - you just need to get access to the servletContext at Spring configuration time.
I want to create some applications in Java netbeans using sqLite manager. i had done the following steps.
I have done plugin to Mozilla Firefox a sqLite-manger database
I have create database calling "mydb".
I have create a table with 2 values fname , lname.
I got mydb.sqlite file.
In netbeans library i have add jar file calling sqlite-jdbc 3.7.2 jar
then i copy the file mydb.sqlite from the folder and paste into netbeans project folder .
I want to connect with my project calling "test" in netbeans.
how to connect with netbeans application
Here is example http://www.tutorialspoint.com/sqlite/sqlite_java.htm
Most important part is jdbc url to create connection:
jdbc:sqlite:mydb.sqlite
This url assumes that test.db located in same directory, u application starts from. But u can put path to certain db.
For example:
jdbc:sqlite:c:/temp/sqlite/mydb.sqlite
If u distribute u application when there u manage some initialization. Actually u have to set environment for u application before start. Let's say u have good working application in netbeans and u copy application to another machine.
Configure environment for application to make it same as actual working application instance in netbeans. Question u should think about
What path to database to use relative or absolute?
Where database location on filesystem (absolute path to database)?
Where directory from which application starts (relative path to database)?
Useful information:
jdbc driver will create database mysql.sqlite if it doesn't exists (path to directory "c:/temp/sqlite" should be real) and after u can recreate necessary structure of database.
u can use System Properties https://docs.oracle.com/javase/tutorial/essential/environment/sysprop.html especially user.dir and user.home to set predefined location of u database (create database in there or copy existing database file)
I'm using H2 embedded database for my application. I would like to contain everything the application needs in it's own Jar, including it's database if possible. My app does not need to create temp files or anything, so basically the user just runs the Jar.
Is it possible to embed a database inside a Jar, and be able to INSERT new records as well as just SELECT out?
EDIT: Just to clarify, I'm not looking to embed the H2 driver jar inside my distributable jar, I'm looking to embed the h2 database file (someDatabase.h2.db file) inside a Jar and still be able to write/read from that database.
If you wish to embed the myDatabase.h2.db file inside the .jar, you can do so, but you'll have read-only access to the database. As .jar files are read-only, you can't modify them and therefore can't execute INSERT, DELETE or any DDL command.
That being said, below is an explanation on how to embed it read-only.
According to H2's documentation:
The JDBC URL "jdbc:h2:~/myDatabase" tells the H2 Engine to look for a database file named myDatabase.h2.db in the home directory of the current user.
The JDBC URL "jdbc:h2:file:/myDatabase" tells the H2 Engine to look for a database file named myDatabase.h2.db in the current directory (where the java program was executed).
If you embed the h2.db file inside a .jar, it is not accessible in a plain way. It is only accessible as a file inside a zip file.
In order to make H2 uset it, you have to use a zip as URL:
jdbc:h2:zip:~/data.zip!/test
See more in "Read Only Databases in Zip or Jar File".
When you embed the file as a resource in the jar, you may get it's relative url. Using...
MyClass.class.getClassLoader().getResource("myDatabase.h2.db")
...you'll get something like:
jar:file:/C:/folder1/folder2/myJar.jar!/myDatabase.h2.db
You can then manipulate it as a String and pass as JDBC URL connection to H2.