Friday, November 25, 2011

JavaDB (Embedded) and NetBeans

updated...
Good day ALL..!
           I am sharing this for all those who find it a bit difficult starting with the Java DB (embedded) or the derby.There are a lot of tutorials and stuff available but only a very few are productive.In the beginning I was also mislead by several sources in the internet and finally when I found out the real solution it was very easy...Hope this will be helpful for many
              I myself use netbeans and finds it very simple and user friendly especially for beginners.So I personally recommend using netbeans.Also that derby ships with netbeans and nothing has to be installed further.You just have to start creating your db.
         
  • Open netbeans and select the services tab (left pane).You can also find it in the windows menu.
  • Once you expand DATABASES you will find JavaDB->sample which is the sample db created by netbeans.You can also find a db connection url for it ,something like                   jdbc:derby://localhost:1527/sample [app on APP]
  • Now right click on JavaDB and choose properties there you can see the location of your javadb and the location to which the database you create is stored.If its not set specify the folders by browsing.Normally javadb location is C:\Program Files \glassfish-3.1.1\javadb that is inside the glassfish installation which comes along with netbeans.
  • Now expand the Drivers folder and there you will see JavaDB(Embedded).Right click on it and open customise,where you can see the Driver files and driver classes filled.If  not set then click on add button and add all jar files in the db->lib folder of javaDB
  • Select org.apache.derby.jdbc.EmbeddedDriver as the driver class name it Java DB (Embedded) and press ok.
  • Once all this is set we have to create a db .
  1. Right click on JavaDB(Embedded) under the Drivers folder and select connect using .Now the connection wizard opens ..
  2. Select JavaDB(Embedded) as driver name.
  3. In the database field give the name of the database which should look like YourDatabaseName;create=true
  4. Type a username and password  and press Next.(dont forget the password cz everytime you connect the database you will be asked for password)
  5. Select schema as App (which is usually the default schema)and finish.
  • Now you have your db url along with sample db.Right click on it and connect.
  • Expand it and right click on APP->tables .Select create table and fill in details.                                For eg: table name : fruits.Now click on add column then give  name : id , type : numeric, size:5 check primary key and ok.You have 1 column now.Add one more column name: fruitname, type: varchar,size:20, and press ok.
  • Add values to the table by right clicking and selecting execute command. For sample just enter a statement like insert into fruits values(1,'apple') and choose run query.Right click and view data allows you to see table data .
  • Now locate the database you just created on your drive.It usually is stored in                                           C: / ../ .netbeans/7.0/derby / YourDatabaseName   Once you find the location copy that path 
  • Create a new project under Java application and use your db as below ..


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Fruits
{
    public static void main(String args [])
    {
        try {
                Connection con=      DriverManager.getConnection("jdbc:derby:C:/Users/Aparna/.netbeans/7.0/derby/ YourDatabaseName "); /* Note use' / 'and not' \'  The url above will be different in your system*/
               PreparedStatement stmt=con.prepareStatement("select * from fruits ");
                ResultSet rs=stmt.executeQuery();
                if(rs.next())
                {
                   System.out.println("Id : "+rs.getInt(1) +" "+" Fruitname :"+rs.getString(2));
                }
                else
                {
                   System.out.println("No word matching in database");
                }
            } catch (SQLException err) {
               System.out.println(err.getMessage()+ "No matching word in database");
            }
        }
    }
  • After writing your program expand your project in the left side pane.There you will find a Libraries folder.Right click on that and select Add jar/Folder.Browse to the folder containing the derby.jar file which will be inside the lib folder in glassfish installation (derbyclient.jar is used for client or network java db)
  •     Don't forget to right click and disconnect the db connection you made in netbeans once you are starting to run the program.This is bcause a dblock appears when you select connect  in netbeans.                                                                                                                 (You can visually see the dblock when it is connected in netbeans, ie open your database file in  C: / ../ .netbeans/7.0/derby / YourDatabaseName you will see a file there and once you choose disconnect that file disappears. )                                                                                                     To release the lock on db you will have to disconnect database from netbeans or it won't work.This is only for embedded db
Hope you all have a great time making your own db....thanks all who visited...please post in your feedbacks.