Showing posts with label java db. Show all posts
Showing posts with label java db. Show all posts

Tuesday, 27 December 2011

Ping JavaDB.

The command to ping JavaDB is:


java �jar derbyrun.jar server ping
java �jar derbyrun.jar server ping 192.168.0.1 1555

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();
    }
  }
}