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");
                   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)

1 comment:

Naviya Nair said...

I have read your blog its very attractive and impressive. I like it your blog.

Java Online Training Java EE Online Training Java EE Online Training Java 8 online training Core Java 8 online training

Java Online Training from India Java Online Training from India Core Java Training Online Core Java Training Online Java Training InstitutesJava Training Institutes