//***************************************************************************
// (c) Copyright IBM Corp. 2007 All rights reserved.
//
// The following sample of source code ("Sample") is owned by International
// Business Machines Corporation or one of its subsidiaries ("IBM") and is
// copyrighted and licensed, not sold. You may use, copy, modify, and
// distribute the Sample in any form without payment to IBM, for the purpose of
// assisting you in the development of your applications.
//
// The Sample code is provided to you on an "AS IS" basis, without warranty of
// any kind. IBM HEREBY EXPRESSLY DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR
// IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. Some jurisdictions do
// not allow for the exclusion or limitation of implied warranties, so the above
// limitations or exclusions may not apply to you. IBM shall not be liable for
// any damages you suffer as a result of using, copying, modifying or
// distributing the Sample, even if IBM has been advised of the possibility of
// such damages.
//***************************************************************************
//
// SOURCE FILE NAME: CreateEmployee.sqlj
//
// SAMPLE: Create an employee record
//
// SQL Statements USED:
// INSERT
//
// Classes used from Util.sqlj are:
// Db
// SqljException
//***************************************************************************
//
// For more information on the sample programs, see the README file.
//
// For information on developing SQLJ applications, see the Application
// Development Guide.
//
// For information on using SQL statements, see the SQL Reference.
//
// For the latest information on programming, compiling, and running DB2
// applications, visit the DB2 Information Center at
// http://publib.boulder.ibm.com/infocenter/db2luw/v9r7/index.jsp
//**************************************************************************/
import java.lang.*;
import java.io.*;
import java.sql.*;
import sqlj.runtime.*;
import sqlj.runtime.ref.*;
class CreateEmployee
{
public static void main(String argv[])
{
try
{
Db db = new Db(argv);
System.out.println();
System.out.println("THIS SAMPLE CREATES A NEW EMPLOYEE RECORD.");
// connect to the 'sample' database
db.getDefaultContext();
Employee user = new Employee();
setEmployeeInfo(user);
// add the employee record to the database
employeeAdd(user);
// disconnect from the 'sample' database
db.disconnect();
}
catch (Exception e)
{
SqljException sqljExc = new SqljException(e);
sqljExc.handle();
}
} // main
static void employeeAdd(Employee user)
{
String empno=null;
String firstnme=null;
String midinit=null;
String lastname=null;
String workdept=null;
String phoneno=null;
String hiredate=null;
String job=null;
String edlevel=null;
String sex=null;
String birthdate=null;
String salary=null;
String bonus=null;
String comm=null;
empno = user.getEmpno();
firstnme = user.getFirstnme();
lastname = user.getLastname();
job = user.getJob();
birthdate = user.getBirthdate();
salary = user.getSalary();
try
{
System.out.println();
System.out.println(
" INSERT INTO EMPLOYEE\n" +
" VALUES( '" + empno + "','" + firstnme + "','" + lastname + "',\n" +
" 'F00','3978','1965-01-01','" + job + "',\n" +
" 18 ,'F','" + birthdate + "'," + salary + ",\n" +
" 1000,4220)");
#sql {INSERT INTO EMPLOYEE VALUES(:empno,:firstnme,'',
:lastname,'A00','3978','1965-01-01',:job,18 , 'F', :birthdate,
:salary,1000,4220) };
}
catch (Exception e)
{
SqljException sqljExc = new SqljException(e);
sqljExc.handle();
}
}
// employeeAdd
static String getEmployeeInfo(String text)
{
System.out.println();
//prompt the user to enter information
System.out.print(text);
// open up standard input
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String data = null;
try
{
data = br.readLine();
}
catch (IOException ioe)
{
System.out.println("IO error trying to read input!");
System.exit(1);
}
return data;
} //getEmplyeeInfo
static void setEmployeeInfo(Employee user)
{
String empno=null;
String firstnme=null;
String midinit=null;
String lastname=null;
String workdept=null;
String phoneno=null;
String hiredate=null;
String job=null;
String edlevel=null;
String sex=null;
String birthdate=null;
String salary=null;
String bonus=null;
String comm=null;
/////////////////////////////////////////////////////////////////
//This program only requires user to enter Employee ID, firstname,
//lastname, job, birthday and salary. The rest data use default
//in this program.
/////////////////////////////////////////////////////////////////
empno = getEmployeeInfo("Employe ID:");
user.setEmpno(empno);
firstnme = getEmployeeInfo("First Name:");
user.setFirstnme(firstnme);
//midinit = getEmployeeInfo("Middle Name Initial:");
//user.setMidinit(midinit);
lastname = getEmployeeInfo("Last Name:");
user.setLastname(lastname);
//workdept = getEmployeeInfo("Work Department:");
//user.setWorkdept(workdept);
//phoneno = getEmployeeInfo("Phone Number:");
//hiredate = getEmployeeInfo("Hire Date:");
job = getEmployeeInfo("Job:");
user.setJob(job);
//edlevel = getEmployeeInfo("Education Level:");
//sex = getEmployeeInfo("Sex:");
birthdate = getEmployeeInfo("Birthday(yyyy-mm-dd):");
user.setBirthdate(birthdate);
salary = getEmployeeInfo("Salary:");
user.setSalary(salary);
//bonus = getEmployeeInfo("Bonus:");
//comm = getEmployeeInfo("Comm:");
} //setEmployeeInfo
} //CreateEmployee
class Employee
{
private String empno=null;
private String firstnme=null;
private String midinit=null;
private String lastname=null;
private String workdept=null;
private String phoneno=null;
private String hiredate=null;
private String job=null;
private String edlevel=null;
private String sex=null;
private String birthdate=null;
private String salary=null;
private String bonus=null;
private String comm=null;
//--------set method----------------
public void setEmpno(String empno) {
this.empno = empno;
}
public void setFirstnme(String firstnme) {
this.firstnme = firstnme;
}
public void setMidinit(String midinit) {
this.midinit = midinit;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public void setWorkdept(String workdept) {
this.workdept = workdept;
}
public void setPhoneno(String phoneno) {
this.phoneno = phoneno;
}
public void setHiredate(String hiredate) {
this.hiredate = hiredate;
}
public void setJob(String job) {
this.job = job;
}
public void setEdlevel(String edlevel) {
this.edlevel = edlevel;
}
public void setSex(String sex) {
this.sex = sex;
}
public void setBirthdate(String birthdate) {
this.birthdate = birthdate;
}
public void setSalary(String salary) {
this.salary = salary;
}
public void setBonus(String bonus) {
this.bonus = bonus;
}
public void setComm(String comm) {
this.comm = comm;
}
//-----------get method -----------------
public String getEmpno() {
return empno;
}
public String getFirstnme() {
return firstnme;
}
public String getMidinit() {
return midinit;
}
public String getLastname() {
return lastname;
}
public String getWorkdept() {
return workdept;
}
public String getPhoneno() {
return phoneno;
}
public String getHiredate() {
return hiredate;
}
public String getJob() {
return job;
}
public String getEdlevel() {
return edlevel;
}
public String getSex() {
return sex;
}
public String getBirthdate() {
return birthdate;
}
public String getSalary() {
return salary;
}
public String getBonus() {
return bonus;
}
public String getComm() {
return comm;
}
//Employee
}