Is there a way to get the name of a running database? - java

I'm working on Java with an application that persists in Oracle and PostgreSQL (not at the same time). I'd like to get the type of the database that is currently running because I need to work in a different way depending on whether it's oracle or it's Postgresql.

JDBC API should help: getDatabaseProductName(). There are other methods as well that allow to get the product version and the driver name and its version.

Related

I want to create one standalone java application product. is is feasible to use HSQL DB ? cause customer may have very historical data

I want to create a one stanalone product, i.e a standalone java application, and I need a database for that. So I was thinking to use HSQL db.
Can HSQLDB is fesible to very historical data like 5 years, 8 years?
Is HSQLDB is suitable for that? Or do you know any alternative? (I know MYSQL but I don't want to use MYSQL, I want to use embedded database)

How can I change the currently used Database just by changing the configurations in Struts2

I have developed an application in Struts2 using MySQL at backend. But now as per interest I want to use Oracle at the back end.
For that I changed the db configurations to Oracle but I found that some of the MySQL queries are unable to run on Oracle directly.
I have searched too much and tried a few thing but still have no solution. Can anyone help here?
You can use Hibernate, it has dialects for both MySQL and Oracle. But if you use native queries then you should use separate code for each dialect.
In the BaseHibernateDAO you can create a method
protected Dialect getDialect(){
return ((SessionFactoryImplementor) sessionFactory).getDialect();
}
Then you can use this dialect for any query you create.
In my opinion you've answered your own question.
MySQL queries are not ALL compatible with ORACLE (example : LIMIT in MySQL would be ROWNUM in ORACLE).
So if you have 'raw SQL' statements that involve specific terms for MYSQL you should change these to ORACLE.
However, if you are using Objects to generate the queries, this would/should be handled 'automagically' by the (for example) persistence solution you are using; just by changing the database driver in the settings.
For more details, we/I would need some examples of code that works on MYSQL and doesn't on ORACLE.
Let me know if that helps.

migrating java app that accesses mysql to code that uses SQL Server

I have with me a java app that uses mysql-- now I have converted the database to SQL Server- but I need to convert the code of the java app so that it now uses SQL Server.
This java app uses Struts and Hibernate
What all things do I have to keep in mind to make this conversion?
hibernate should make the transition seamless for you.
Make sure to choose the correct driver as some drivers might act differently.
I personally like jtds. It has vast documentation.
Look for native queries in your code because they might need some modifications.
Other than that, I don't think you should experience any problem.
I had an application that supported oracle, SQL-Server and mysql and we didn't have specific code for each one.
EDIT : as maba suggests in the comments, you will need to modify the hibernate configuration a bit (driver, dialect, url ).
For more hibernate specific configuration, I found a great stackoverflow answer

How to run sql scripts in order to update a Derby schema from java code?

The derby database installations of our customer sides have different schema versions. E.g. Customer1 has db schema version 4.1.5.0240, Customer2 has version 4.0.1.0330.
The idea is to update theses schemas to actual version when a new software is installed. There exists several sql scripts in order update a version to next level. E.g. update_4.1.5.0240_to_4.3.1.0020.sql.
The tool/procedure I am looking for should read the actual installed version from a derby database table. According to that version the appropriate sql script shall be run in order to update the schema to next level. This procedure must be repeated until no matching sql script can be found for the actual read version.
The installation at customer site should be done headless without any administrators help.
1.) I would prefer a java program that reads the version using JDBC. But how to run the sql scripts from within java? Should I call the ij commandline tool from java?
2.) Are there better alternatives?
Thanks
Viktor
You might find the ij.runScript method helpful: http://db.apache.org/derby/javadoc/publishedapi/jdbc4/org/apache/derby/tools/ij.html
One thing to be aware of is that it's not easy to automate the error handling inside the script, as there are no control flow (if/then/else) constructs in the ij scripting. But if you just need to perform a series of DDL operations in a clean manner, ij.runScript is a pretty good technique.
You could store the SQL in a text file:
From your java code you read in the SQL for updating/inserting data into the various tables:
Your java code could also perform checks on which 'SQL update file to run'.
something along these lines (in very pseudo code)
Installed = getCurrentInstalledVersion();//info stored in an admin table for current version
if (Installed < V4.1){//This would probably work better as a switch
String sql = updateWith(URL file:\\PathTov4.1\sql.txt);
}
else{
if(Installed = OtherVersions){
String sql = UpdateWith(URL file:\\PathToOtherVersion\sql.txt);
}
}
Connect.sendSQL(sql);//send the sql to the DB
I leave you to figure out the fuller details. If you already found a solution, why not post it here.
If you use a switch you could run updates to a newer version via other versions if required by placing the versions in the correct order, and inserting code before any break;

database in desktop application using swing

I am making a desktop application in java and using MSAccess in data base.
I want that if i run the setup of the application the database should be created on client machine because there can be different client using the application and how can i create the setup?
is there any tools available for this free of cost?
please explain me in detail..
thanks
Java 6 (enhanced for desktop application work) comes with a built-in database called JavaDb (formerly IBM's Derby). That will do what you want.
Here's a set of guides and tutorials on how to use it.
I would suggest that when your application first starts, it checks for the presence of the created database, and if it doesn't exist, it builds the database (via the appropriate SQL). I've used this approach before and it works quite well.
I prefer nullsoft. Take a look at Open Source Installers Generators in Java
#pratap: database should be created on client machine..
Add an empty access database to your setup.
Have a look at SQLite, which is used by Mozilla (Firefox stores all bookmarks and history in a database) and several other major applications.
When you say
access in database
do you mean Microsoft Access or access the data in a database.
I would advise against MS Access if that is the case. If not, you could either use the JavaDB or HSQLDB and the use SQL scripts to create the database. As a summary
Package the application in one of the installers (InnoSetup or NSIS are good ones)
When installing, extract all the files in proper folders
Execute the SQL scripts before first running the application to ensure the database is setup, you can do other housekeeping tasks along with this step (refer to installer documentation for after-install steps)
Your application is good to go
In the last distribution of NetBeans I used, there was a wizard to create such application. The application used the Java Persistence API to store the Data.
My option is HSQLDB since it's fast, reliable and easy to use.
In the documentation it's explained how to use the standalone database mode, this is primarily used for unit testing but it fits your case too. The good thing with this is that you just connect to the file based database without any special set up and if the files doesn't exist, they're created.
I would second the posters who recommend JavaDB.
It is absurdly easy to adminster from inside your application. Whats more because everything is native Java you dont get the char->unicode little-endian->big-endien and all the other conversion malarky you normally get when reading SQL into java.
The one tip is that with JavaDB is prepare your SQL statements. Prepared statements get cached and the resulting access program (similar to an access plan but actually a jvm program) is reused, the programs for "executed' statements are not cached.
If you are really set on MSAccess then I would suggest you package an "default.mdb" file with all your required tables defined and your classifcation tables populated. If the user's table does not exist then simply copy over the default .mdb file and open that.
I recommend the H2 database because it is simple, fast, pure Java, and small. See this page for how H2 compares to other Java databases, including those mentioned here in other answers. It has many features Derby/JavaDB and HSQLDB do not.

Categories