Showing posts with label database. Show all posts
Showing posts with label database. Show all posts

Tuesday, 22 July 2014

Liferay HSQL DB Access

        Normally at the time of development, we have fixed name of database which will be used for production environment. We configure the same DB in our local or development environment for the build. But in case of doing POC, preparing demo for PreSales or making some reusable component, we might not have access to such environment, or as per the system requirement, It might not be a good idea to spend time after configuring other DB.

        Liferay is using HSQL database by default. Here is the steps to access that HSQL database using DatabaseManager.


  1. Go to {server}\lib\ext , Make you have hsql.jar file under that location. If it's not there. Find the location of that jar.
  2. Open command prompt with the location of step-1
  3. Run this command in command prompt "java -cp hsql.jar org.hsqldb.util.DatabaseManager"
  4. Database manager will open as per the below image.
  5. Select "HSQL Database Engine Standalone" for dropbox of type.
  6. Now in URL text field you need to enter location of the DB file. So if you check in your Liferay bundle. There would be lportal HSql DB in "{liferay-portal}\data\hsql" directory. So you need to give path to DB as like "{liferay-portal}\data\hsql\lportal", here lportal is the DB.
  7. Username will be 'SA' and password will be blank. 
        Once the DB is connected. You will be able to see the list of the tables on the left hand side. As well you can execute any query using the same browser.


       One important point, If the Liferay instance is running. There would be lock on DB. So you won't be able to access the DB. 

Thursday, 15 November 2012

Java Blackberry database management

Blackberry java development allows to create a database on the device. There are many possible ways to create database. I have recently used Sqlite database for my application. Following is what I have done to use that with my application.

1) Create a database through through blackberry emulator. Following is the code to do that. 

 URI uri = URI.create("file:///SDCard/Databases/employee.db");
 Database sqliteDB = DatabaseFactory.create(uri);
 Statement st = sqliteDB.createStatement("CREATE TABLE Employee ("
 +"ID       INTEGER PRIMARY KEY,"
 +"name     TEXT,"
 +"number   TEXT,"
 );");
 st.prepare();
 st.execute();
 

2) A db file will be created in the file system. Open that db file. Insert as many data as you need. I have been required to insert data like this. As I had a large number of records that i needed in db on start up time only. I have tried to insert data through java program only. But i was facing issue about memory consumption. If you don't have too many data you can insert run time. 

3) Following is the code to retrieve data from database. 

 Statement st1 = sqliteDB.createStatement("select * from Employee;");
 st1.prepare();
 Cursor c = st1.getCursor();
 Row r;
 int count=0;
 while (c.next()) {
 r = c.getRow();
 String name = r.getString(1);
 }
 

If you have close database connection. You need to open the connection again before you go to retrieve data from the database. 

 sqliteDB = DatabaseFactory.open(uri);
 

This is just an example. Your implementation can be differ as per your requirement.