Showing posts with label connect to JavaDB. Show all posts
Showing posts with label connect to JavaDB. Show all posts

Tuesday, 27 December 2011

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

Working with Tables

Use the Create Table SQL command to create a new table in a database.


Create table employee (name varchar(30),address varchar(50));

Using the ij Tool

The ij tool enables you to connect to a running JavaDB server and execute scripts and queries against a database.
The command to use the ij tool is:


java �jar derbyrun.jar ij


Commands of the ij Tool
CommandDescriptionDriverLoads the specified JDBC driver.ProtocolSets the specified protocol to use for connecting to a database.ConnectEstablishes a connection with a database.Set ConnectionSwitches from the existing connection to a different connection specified with a connection name.Show ConnectionsLists all active connections.DisconnectDisconnects the current connection, a connection specified by a connection name, or all active connections.Show SchemasLists all schemas present in the current database.Show TablesLists all tables present in the current database.Show ViewsLists all views present in the current database.Show ProceduresLists all stored procedures present in the current database.Show IndexesLists all indexes present in a specified table.DescribeDisplays information about columns, such as column name and data type, for a specified table.rumitCommits the current transaction.RollbackRolls back the current transaction.

To view run time information for JavaDB

java �jar derbyrun.jar server runtime

Stop JavaDB

java �jar derbyrun.jar server shutdown

Ping JavaDB

The JavaDB Directory Structure

When you install Java SE 6, JavaDB installs in the db directory of JAVA_HOME.
The db directory contains the following subdirectories:
  1. demo: Contains sample programs and scripts to use JavaDB.
  2. lib: Contains the JavaDB JAR files. You need to use the JavaDB JAR files to start, stop, and access JavaDB.
  3. frameworks: Contains scripts for setting up the environment to use JavaDB in different environments.

JavaDB Tools

Java SE 6 contains various tools that you can use to start, stop, access, and administer JavaDB.
Some of the JavaDB tools are:
  1. server: Starts the JavaDB database.
  2. ij Represents a JDBC scripting tool.
  3. You can use ij to run scripts against a JavaDB database.
  4. You can also use ij to run SQL queries.
  5. You can use ij to access JavaDB running in both the embedded and client/server environments.
  6. sysinfo: Displays information about the JavaDB version and the environment.
  7. dblook: Generates and stores the Data Definition Language (DDL) to a file.
  8. In Java SE 6, the server, ij, sysinfo, and dblook tools are packaged in the derbyrun.jar file.
  9. You can locate the derbyrun.jar file in the <JAVA_HOME>/db/lib directory.

JavaDB Drivers

Based on the environment in which JavaDB is running, Java SE 6 contains two drivers for accessing JavaDB. They are:
Embedded driver enables you to access JavaDB databases running in the embedded environment.
The org.apache.derby.jdbc.EmbeddedDriver class implements the embedded driver.
Network client driver enables you to access the JavaDB database running in the client/server environment.
The org.apache.derby.jdbc.ClientDriver class implements the network client driver.

Connect to Java DB (Derby) with org.apache.derby.jdbc.EmbeddedDriver

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
public class JavaDBDemo {
  static Connection conn;
  public static void main(String[] args) {
    String driver = "org.apache.derby.jdbc.EmbeddedDriver";
    String connectionURL = "jdbc:derby:myDatabase;create=true";
    String createString = "CREATE TABLE Employee (NAME VARCHAR(32) NOT NULL, ADDRESS VARCHAR(50) NOT NULL)";
    try {
      Class.forName(driver);
    } catch (java.lang.ClassNotFoundException e) {
      e.printStackTrace();
    }
    try {
      conn = DriverManager.getConnection(connectionURL);
      Statement stmt = conn.createStatement();
      stmt.executeUpdate(createString);
      PreparedStatement psInsert = conn.prepareStatement("insert into Employee values (?,?)");
      psInsert.setString(1, args[0]);
      psInsert.setString(2, args[1]);
      psInsert.executeUpdate();
      Statement stmt2 = conn.createStatement();
      ResultSet rs = stmt2.executeQuery("select * from Employee");
      int num = 0;
      while (rs.next()) {
        System.out.println(++num + ": Name: " + rs.getString(1) + "\n Address" + rs.getString(2));
      }
      rs.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}