Thursday, December 8, 2011

Netbeans installation in Windows 7


                                                       Personally I faced a problem while installing netbeans for the first time in my windows 7 os.But at the same time there were no issues while installing it from my workplace which was also on windows 7. I don't know why that happened anyway I wanted to share the solution to all.
                                                      First case is when you have your environment variable set correctly and even after restarting the system you get an error message while tryng to install netbeans which says jdk not installed .It also suggests to use the --javahome command ,so here it is

  • Start the command prompt and set the path to where your netbeans.exe file exists(Copy it to any of your drives if it is on cd).It may look like D:\Aparna\Softwares>netbeans-7.0.1-ml-windows.exe after this add a 'space' and type --javahome 'space' your path to jdk bin.

D:\Aparna\Softwares>netbeans-7.0.1-ml-windows.exe --javahome "C:\ProgramFiles\Java\                        jdk1.7.0_01\bin"
  • Press enter and your netbeans will start installation.

                                                      The second way as everybody is familiar with is by setting environment variable for java(This didnt work for me in first case ).There are many available resources in the internet explaining how to do it especially the http://java.sun.com .Anyway here is it in my words...

  • Right click on MyComputer and  select Properties->Advanced System settings.From the bottom right corner select Environment Variables.
  • Under system variables select variable with name path and select edit if its existing else create a new variable named path .In variable value paste your path to jdk bin 
  • C:\Program Files\Java\jdk1.7.0_01\bin;.    (Dont forget the ;. at the end)

HAVE A GREAT DAY !

Thursday, December 1, 2011

JavaDB (Client or Network database)


                                           Previously we created an embedded javadb with netbeans.Today i will show you how to create a JavaDB Network database which is very simple


  • Choose  services from the left pane of netbeans(or you can find it in windows menu)Expand  Databases and you will find JavaDB.Right click on that and choose create database
  • Here enter the database name:NEW_DB                                                                       Username:app                                                                                                                Password:app also change the database location if required.Press OK.You can now see a new database and its url along with sample db.
  • Right click on the newly created db url and choose connect.Now that the db is connected we will move on to creating a table in it.
  • On expanding the db url you will see APP in bold.This means that APP is set as the default schema.If not bold right click on it and select set as Default Schema


  1. Expand APP and you will see tables which currently is empty.Right click on tables and choose create tables
  2. Provide table name:Fruits. 
  3. Press Add column button there and create a new column in the table.                                      column name:Name                                                                                                       Type:varchar                                                                                                                                          size:10    Uncheck null and click OK                                                                                                    Create 1 more column with Name: Colour                                                                             Type:varchar                                                                                                                           size:10 .Uncheck all and click OK
  4. Now you can see 2 columns being created Click OK 

  • Inserting data into table.


  1. Right click on the newly created table(Fruits) and select execute command                         INSERT INTO FRUITS VALUES('apple','red')  press run

You can view the table data either using the select * from fruits statement or right click fruits and select view data


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Statement;



public class DB
{
    public static void main(String args [])
    {
     
            try {
               
                Connection con= DriverManager.getConnection("jdbc:derby://localhost:1527/NEW_DB","app","app");
             
             
               Statement stmt=con.createStatement();
           
             
                ResultSet rs=stmt.executeQuery("select * from Fruits");
                System.out.println("Fruit   Colour");
                while(rs.next())
                {
                   System.out.println(rs.getString(1)+"  "+rs.getString(2));
                }
             
            } catch (SQLException err) {
             
               System.out.println(err.getMessage()+ "No matching word in database");
            }
        }
    }
 
As did for the embedded db we have to Add Jar file in libraries(Here use the derbyclient.jar file since its network db)

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.