Showing posts with label connect to derby db. Show all posts
Showing posts with label connect to derby db. Show all posts

Tuesday, 27 December 2011

Server tool to start JavaDB in a network client environment

To start JavaDB:
Set the Path environment variable to include the <JAVA_HOME>/db/lib directory.
Open the command prompt and type the following command:


java �jar derbyrun.jar server start
 
Ping JavaDB 
Stop JavaDB 

Connect command of ij to establish a connection with a database

The connect command loads a JDBC driver to establish the connection.
The command to establish a connection with a sample database is:


connect "jdbc:derby:sample";

Connect to Derby database

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
public class MainClass {
  static Connection conn;
  public static void main(String[] args) throws Exception {
    if (args.length != 2) {
      System.out.println("Usage: java JavaDBDemo <Name> <Address>");
      System.exit(1);
    }
    String driver = "org.apache.derby.jdbc.EmbeddedDriver";
    String dbName = "AddressBookDB";
    String connectionURL = "jdbc:derby:" + dbName + ";create=true";
    String createString = "CREATE TABLE ADDRESSBOOKTbl (NAME VARCHAR(32) NOT NULL, ADDRESS VARCHAR(50) NOT NULL)";
    Class.forName(driver);
    conn = DriverManager.getConnection(connectionURL);
    Statement stmt = conn.createStatement();
    stmt.executeUpdate(createString);
    PreparedStatement psInsert = conn
        .prepareStatement("insert into ADDRESSBOOKTbl values (?,?)");
    psInsert.setString(1, args[0]);
    psInsert.setString(2, args[1]);
    psInsert.executeUpdate();
    Statement stmt2 = conn.createStatement();
    ResultSet rs = stmt2.executeQuery("select * from ADDRESSBOOKTbl");
    System.out.println("Addressed present in your Address Book\n\n");
    int num = 0;
    while (rs.next()) {
      System.out.println(++num + ": Name: " + rs.getString(1) + "\n Address"
          + rs.getString(2));
    }
    rs.close();
  }
}