The SQL script for creating the Employee Table : Java DB Derby « Database « Java Tutorial

Home
Java Tutorial
1.Language
2.Data Type
3.Operators
4.Statement Control
5.Class Definition
6.Development
7.Reflection
8.Regular Expressions
9.Collections
10.Thread
11.File
12.Generics
13.I18N
14.Swing
15.Swing Event
16.2D Graphics
17.SWT
18.SWT 2D Graphics
19.Network
20.Database
21.Hibernate
22.JPA
23.JSP
24.JSTL
25.Servlet
26.Web Services SOA
27.EJB3
28.Spring
29.PDF
30.Email
31.J2ME
32.J2EE Application
33.XML
34.Design Pattern
35.Log
36.Security
37.Apache Common
38.Ant
39.JUnit
Java Tutorial » Database » Java DB Derby 
20.37.19.The SQL script for creating the Employee TablePrevious/Next
CREATE TABLE Employee (
    Employee_ID int NOT NULL,
    NAME        varchar (50NOT NULL,
    DESCRIPTION varchar (250NOT 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 ();
            
         }
    }
}
20.37.Java DB Derby
20.37.1.New to Java 6 is a lightweight database called Derby.
20.37.2.The JavaDB Directory Structure
20.37.3.The JavaDB Environments
20.37.4.JavaDB Drivers
20.37.5.JavaDB URLs
20.37.6.JavaDB Tools
20.37.7.You can use the server tool to start JavaDB in a network client environment.
20.37.8.Ping JavaDB.
20.37.9.To view run time information for JavaDB, you can use the following command:
20.37.10.To stop JavaDB, you can use the following command:
20.37.11.Using the ij Tool
20.37.12.You can use the connect command of ij to establish a connection with a database
20.37.13.Viewing and Switching Connections
20.37.14.Working with Tables
20.37.15.To view the structure of the Employee table
20.37.16.Using the sysinfo Tool
20.37.17.Connect to Java DB (Derby) with org.apache.derby.jdbc.EmbeddedDriver
20.37.18.Connect to Derby database
20.37.19.The SQL script for creating the Employee Table
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.