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:
connect "jdbc:derby:sample";
Create table employee (name varchar(30),address varchar(50));
show connections;
java �jar derbyrun.jar ij
CREATE TABLE Employee ( Employee_ID int NOT NULL, NAME varchar (50) NOT NULL, DESCRIPTION varchar (250) NOT NULL ) //////////////////////////////////////////////////////// public class Employee { public int employeeId; public String name; public String description; public String toString () { StringBuilder buf = new StringBuilder (); buf.append ("\n\tEmployee ID = ") .append (employeeId); buf.append ("\n\tName = ").append (name); buf.append ("\n\tDescription = ").append (description); return buf.toString (); } } /////////////////////////////////////////////////////// import java.sql.BaseQuery; import java.sql.DataSet; import java.sql.Select; public interface EmployeeQueries extends BaseQuery { // Select all employees @Select (sql = "SELECT Employee_ID, NAME, DESCRIPTION FROM Employee", readOnly=false, connected=false) DataSet<Employee> getAllEmployees(); // Select employee by name @Select ("SELECT Employee_ID, NAME, DESCRIPTION FROM Employee where NAME = ?") DataSet<Employee> getEmployeeByName(String name); } import java.sql.Connection; import java.sql.DataSet; import java.sql.DriverManager; import java.sql.SQLException; public class QueryDemo { public static void main (String [] args) { Connection connection = null; try { String url = "jdbc:inetdae7:localhost:1433?database=Adaptor"; String login = "sa"; String password "admin"; connection = DriverManager.getConnection(url, login,password); EmployeeQueries qo = connection.createQueryObject(EmployeeQueries.class); // Get all employees DataSet<Employee> rows = qo.getAllEmployees (); for (Employee employee : rows) { System.out.println (employee); } // Create new Employee object if (! rows.isReadOnly () ) { System.out.printIn ("\nCreate new employee"); Employee r = new Employee (); r.Employee_ID = 12345; r.name = "Supervisor"; r.description = "Do monitoring job"; boolean insertResult = rows. insert (r); rows.sync (connection); System.out.println ("\tInserted: " + insertResult); } // Retrieve Employee by name System.out.println ("\nGet employee by name:"); DataSet<Employee> rows2 = qo.getEmployeeByName ("Supervisor"); Employee employee = rows2.get (0); System.out.println (Employee); if (Employee != null) { // Modify Role System.out.println ("\nModify current Employee:"); employee.description = "Do supervising job"; boolean modifyResult = rows2.modify (employee); rows2.sync (connection); System.out.println ("\tModified: " + modifyResult); } } catch (SQLException e) { for (Throwable t : e) { t.printStackTrace (); } } finally { // Close connection try { connection.close (); } catch (SQLException e) { e.printStackTrace (); } } } }
java �jar derbyrun.jar server ping java �jar derbyrun.jar server ping 192.168.0.1 1555
jdbc:derby:<databaseName>;<URLAttributes>
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(); } } }
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(); } }
SavingsAccountEJB
example with a Cloudscape database. The Cloudscape software is included with the J2EE SDK download bundle.cloudscape -start.
(When you are ready to shut down the server, type cloudscape -stop
.)savingsaccount
database table.j2eetutorial/examples/sql/savingsaccount.sql
script to create the savingsaccount
table.deploytool
, open the j2eetutorial/examples/ears/SavingsAccountApp.ear
file (FileOpen).SavingsAccountApp
application (ToolsDeploy). In the Introduction dialog box, make sure that you select the Return Client JAR checkbox. j2eetutorial/examples/ears
directory.APPCPATH
environment variable to SavingsAccountAppClient.jar
.runclient -client SavingsAccountApp.ear -name SavingsAccountClient -textauth
guest
for the user name and guest123
for the password.balance = 68.25 balance = 32.55 456: 44.77 730: 19.54 268: 100.07 836: 32.55 456: 44.77 4.00 7.00