Spring+JDBC实例

1- 介绍

本篇教程是一个简单的Spring+JDBC的操作,作为一个入门级来实现读取MySQL(可选:Oracle或SQL Server)数据中Department表的所有数据。这篇文章是基于:
  • Spring 4
  • Eclipse 4.4 (LUNA)
在本文中,连接的是 MySQL数据库。您可以创建数据库DB类型,如Oracle,MySQL和SQLServer:

2- 创建Maven工程

  • File/New/Other...

输入:
  • Group Id: com.zyiz
  • Artifact Id: SpringJDBC

工程被创建后如下所示:

3- 配置Maven

在这篇文章中,将指导你访问几种常见数据库的类型,如下:
  • Oracle
  • MySQL
  • SQLServer
       所以在Maven中,将配置库用于以上三种类型的数据库。在现实中,只需要配置一个使用的数据库类型(如:MySQL)。
  • pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
                            http://maven.apache.org/xsd/maven-4.0.0.xsd">
                           
   <modelVersion>4.0.0</modelVersion>
   <groupId>com.zyiz</groupId>
   <artifactId>SpringJDBCTutorial</artifactId>
   <version>0.0.1-SNAPSHOT</version>

   <properties>
       <!-- Generic properties -->
       <java.version>1.7</java.version>
       <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
       <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

   </properties>

   <repositories>
       <!-- Repository for ORACLE ojdbc6. -->
       <repository>
           <id>codelds</id>
           <url>https://code.lds.org/nexus/content/groups/main-repo</url>
       </repository>
   </repositories>

   <dependencies>

       <!-- Spring framework -->
       <!-- http://mvnrepository.com/artifact/org.springframework/spring-core -->
       <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-core</artifactId>
           <version>4.1.4.RELEASE</version>
       </dependency>

       <!-- http://mvnrepository.com/artifact/org.springframework/spring-context -->
       <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-context</artifactId>
           <version>4.1.4.RELEASE</version>
       </dependency>

       <!-- http://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
       <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-jdbc</artifactId>
           <version>4.1.4.RELEASE</version>
       </dependency>

       <!-- MySQL database driver -->
       <!-- http://mvnrepository.com/artifact/mysql/mysql-connector-java -->
       <dependency>
           <groupId>mysql</groupId>
           <artifactId>mysql-connector-java</artifactId>
           <version>5.1.34</version>
       </dependency>

       <!-- Oracle database driver -->
       <dependency>
           <groupId>com.oracle</groupId>
           <artifactId>ojdbc6</artifactId>
           <version>11.2.0.3</version>
       </dependency>

       <!-- SQLServer database driver (JTDS) -->
       <!-- http://mvnrepository.com/artifact/net.sourceforge.jtds/jtds -->
       <dependency>
           <groupId>net.sourceforge.jtds</groupId>
           <artifactId>jtds</artifactId>
           <version>1.3.1</version>
       </dependency>

   </dependencies>

</project>

4- Java类



  • Department.java
package com.zyiz.springjdbc.bean;

public class Department {

   private Long deptId;
   private String deptNo;
   private String deptName;

   public Department() {

   }

   public Department(Long deptId, String deptNo, String deptName) {
       this.deptId = deptId;
       this.deptNo = deptNo;
       this.deptName = deptName;
   }

   public Long getDeptId() {
       return deptId;
   }

   public void setDeptId(Long deptId) {
       this.deptId = deptId;
   }

   public String getDeptNo() {
       return deptNo;
   }

   public void setDeptNo(String deptNo) {
       this.deptNo = deptNo;
   }

   public String getDeptName() {
       return deptName;
   }

   public void setDeptName(String deptName) {
       this.deptName = deptName;
   }
}
  • DepartmentDAO.java
package com.zyiz.springjdbc.dao;

import java.util.List;

import com.zyiz.springjdbc.bean.Department;

public interface DepartmentDAO {
   
   public List<Department> queryDepartment() throws Exception ;

}
  • DepartmentImplDAO.java
package com.zyiz.springjdbc.daoimpl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import com.zyiz.springjdbc.bean.Department;
import com.zyiz.springjdbc.dao.DepartmentDAO;

public class DepartmentImplDAO implements DepartmentDAO {

   private DataSource dataSource;

   public void setDataSource(DataSource dataSource) {
       this.dataSource = dataSource;
   }

   @Override
   public List<Department> queryDepartment() throws SQLException {
       Connection conn = dataSource.getConnection();

       String sql = "Select d.dept_id, d.dept_no, d.dept_name from department d";
       Statement smt = conn.createStatement();

       ResultSet rs = smt.executeQuery(sql);
       List<Department> list = new ArrayList<Department>();
       while (rs.next()) {
           Long deptId = rs.getLong("dept_id");
           String deptNo = rs.getString("dept_no");
           String deptName = rs.getString("dept_name");
           Department dept = new Department(deptId, deptNo, deptName);
           list.add(dept);
       }
       return list;
   }

}
  • MainDemo.java
package com.zyiz.springjdbc;

import java.util.List;

import com.zyiz.springjdbc.bean.Department;
import com.zyiz.springjdbc.dao.DepartmentDAO;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainDemo {

   public static void main(String[] args) throws Exception {
       ApplicationContext context = new ClassPathXmlApplicationContext(
               "spring-module.xml");

       DepartmentDAO deptDAO = (DepartmentDAO) context
               .getBean("departmentDAO");

       List<Department> depts = deptDAO.queryDepartment();

       for (Department dept : depts) {
           System.out.println("Dept ID " + dept.getDeptId());
           System.out.println("Dept No " + dept.getDeptNo());
           System.out.println("Dept Name " + dept.getDeptName());
       }
   }

}

5- 配置Spring


  • spring-datasource-oracle.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans.xsd">


   <bean id="dataSource"
       class="org.springframework.jdbc.datasource.DriverManagerDataSource">

       <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
       <property name="url" value="jdbc:oracle:thin:@localhost:1521:db11g" />
       <property name="username" value="simplehr" />
       <property name="password" value="1234" />
   </bean>

</beans>
  • spring-datasource-mysql.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans.xsd">


   <bean id="dataSource"
       class="org.springframework.jdbc.datasource.DriverManagerDataSource">

       <property name="driverClassName" value="com.mysql.jdbc.Driver" />
       <property name="url" value="jdbc:mysql://localhost:3306/zyiz" />
       <property name="username" value="root" />
       <property name="password" value="" />
   </bean>

</beans>
  • spring-datasource-sqlserver.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans.xsd">


   <bean id="dataSource"
       class="org.springframework.jdbc.datasource.DriverManagerDataSource">

       <!-- Using JDBC Driver: JTDS -->
       <property name="driverClassName" value="net.sourceforge.jtds.jdbc.Driver" />
       <property name="url"
           value="jdbc:jtds:sqlserver://localhost:1433/simplehr;instance=SQLEXPRESS" />
       <property name="username" value="sa" />
       <property name="password" value="1234" />
   </bean>

</beans>
  • spring-department.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans.xsd">

   <bean id="departmentDAO"
       class="org.o7planning.tutorial.springjdbc.daoimpl.DepartmentImplDAO">
       <property name="dataSource" ref="dataSource" />
   </bean>

</beans>
  • spring-module.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans.xsd">

   <!-- Using Oracle datasource -->
   <import resource="database/spring-datasource-oracle.xml" />
   <import resource="dao/spring-department.xml" />

</beans>

6- 运行MainDemo主程序

运行主类示例结果如下:



2.1- Oracle 创建 DB 脚本

  • ORACLE SCRIPT:
   create table DEPARTMENT (
       DEPT_ID number(10,0) not null,
       DEPT_NAME varchar2(255 char) not null,
       DEPT_NO varchar2(20 char) not null unique,
       LOCATION varchar2(255 char),
       primary key (DEPT_ID)
   );

   create table EMPLOYEE (
       EMP_ID number(19,0) not null,
       EMP_NAME varchar2(50 char) not null,
       EMP_NO varchar2(20 char) not null unique,
       HIRE_DATE date not null,
       IMAGE blob,
       JOB varchar2(30 char) not null,
       SALARY float not null,
       DEPT_ID number(10,0) not null,
       MNG_ID number(19,0),
       primary key (EMP_ID)
   );

   create table SALARY_GRADE (
       GRADE number(10,0) not null,
       HIGH_SALARY float not null,
       LOW_SALARY float not null,
       primary key (GRADE)
   );

   create table TIMEKEEPER (
       Timekeeper_Id varchar2(36 char) not null,
       Date_Time timestamp not null,
       In_Out char(1 char) not null,
       EMP_ID number(19,0) not null,
       primary key (Timekeeper_Id)
   );

   alter table EMPLOYEE
       add constraint FK75C8D6AE269A3C9
       foreign key (DEPT_ID)
       references DEPARTMENT;

   alter table EMPLOYEE
       add constraint FK75C8D6AE6106A42
       foreign key (EMP_ID)
       references EMPLOYEE;

   alter table EMPLOYEE
       add constraint FK75C8D6AE13C12F64
       foreign key (MNG_ID)
       references EMPLOYEE;

   alter table TIMEKEEPER
       add constraint FK744D9BFF6106A42
       foreign key (EMP_ID)
       references EMPLOYEE;
  • INSERT DATA (ORACLE)
insert into Department (DEPT_ID, DEPT_NAME, DEPT_NO, LOCATION)
values (10, 'ACCOUNTING', 'D10', 'NEW YORK');
 
insert into Department (DEPT_ID, DEPT_NAME, DEPT_NO, LOCATION)
values (20, 'RESEARCH', 'D20', 'DALLAS');
 
insert into Department (DEPT_ID, DEPT_NAME, DEPT_NO, LOCATION)
values (30, 'SALES', 'D30', 'CHICAGO');
 
insert into Department (DEPT_ID, DEPT_NAME, DEPT_NO, LOCATION)
values (40, 'OPERATIONS', 'D40', 'BOSTON');

-------------------------------------------------------------------------------------------------


insert into Employee (EMP_ID, EMP_NAME, EMP_NO, HIRE_DATE, JOB, SALARY, DEPT_ID, MNG_ID)
values (7839, 'KING', 'E7839', to_date('17-11-1981', 'dd-mm-yyyy'), 'PRESIDENT', 5000, 10, null);
 
insert into Employee (EMP_ID, EMP_NAME, EMP_NO, HIRE_DATE, JOB, SALARY, DEPT_ID, MNG_ID)
values (7566, 'JONES', 'E7566', to_date('02-04-1981', 'dd-mm-yyyy'), 'MANAGER', 2975, 20, 7839);
 
insert into Employee (EMP_ID, EMP_NAME, EMP_NO, HIRE_DATE, JOB, SALARY, DEPT_ID, MNG_ID)
values (7902, 'FORD', 'E7902', to_date('03-12-1981', 'dd-mm-yyyy'), 'ANALYST', 3000, 20, 7566);
 
insert into Employee (EMP_ID, EMP_NAME, EMP_NO, HIRE_DATE, JOB, SALARY, DEPT_ID, MNG_ID)
values (7369, 'SMITH', 'E7369', to_date('17-12-1980', 'dd-mm-yyyy'), 'CLERK', 800, 20, 7902);
 
insert into Employee (EMP_ID, EMP_NAME, EMP_NO, HIRE_DATE, JOB, SALARY, DEPT_ID, MNG_ID)
values (7698, 'BLAKE', 'E7698', to_date('01-05-1981', 'dd-mm-yyyy'), 'MANAGER', 2850, 30, 7839);
 
insert into Employee (EMP_ID, EMP_NAME, EMP_NO, HIRE_DATE, JOB, SALARY, DEPT_ID, MNG_ID)
values (7499, 'ALLEN', 'E7499', to_date('20-02-1981', 'dd-mm-yyyy'), 'SALESMAN', 1600, 30, 7698);
 
insert into Employee (EMP_ID, EMP_NAME, EMP_NO, HIRE_DATE, JOB, SALARY, DEPT_ID, MNG_ID)
values (7521, 'WARD', 'E7521', to_date('22-02-1981', 'dd-mm-yyyy'), 'SALESMAN', 1250, 30, 7698);
 
insert into Employee (EMP_ID, EMP_NAME, EMP_NO, HIRE_DATE, JOB, SALARY, DEPT_ID, MNG_ID)
values (7654, 'MARTIN', 'E7654', to_date('28-09-1981', 'dd-mm-yyyy'), 'SALESMAN', 1250, 30, 7698);
 
insert into Employee (EMP_ID, EMP_NAME, EMP_NO, HIRE_DATE, JOB, SALARY, DEPT_ID, MNG_ID)
values (7782, 'CLARK', 'E7782', to_date('09-06-1981', 'dd-mm-yyyy'), 'MANAGER', 2450, 30, 7839);
 
insert into Employee (EMP_ID, EMP_NAME, EMP_NO, HIRE_DATE, JOB, SALARY, DEPT_ID, MNG_ID)
values (7788, 'SCOTT', 'E7788', to_date('19-04-1987', 'dd-mm-yyyy'), 'ANALYST', 3000, 20, 7566);
 
insert into Employee (EMP_ID, EMP_NAME, EMP_NO, HIRE_DATE, JOB, SALARY, DEPT_ID, MNG_ID)
values (7844, 'TURNER', 'E7844', to_date('08-09-1981', 'dd-mm-yyyy'), 'SALESMAN', 1500, 30, 7698);
 
insert into Employee (EMP_ID, EMP_NAME, EMP_NO, HIRE_DATE, JOB, SALARY, DEPT_ID, MNG_ID)
values (7876, 'ADAMS', 'E7876', to_date('23-05-1987', 'dd-mm-yyyy'), 'CLERK', 1100, 20, 7698);
 
insert into Employee (EMP_ID, EMP_NAME, EMP_NO, HIRE_DATE, JOB, SALARY, DEPT_ID, MNG_ID)
values (7900, 'ADAMS', 'E7900', to_date('03-12-1981', 'dd-mm-yyyy'), 'CLERK', 950, 30, 7698);
 
insert into Employee (EMP_ID, EMP_NAME, EMP_NO, HIRE_DATE, JOB, SALARY, DEPT_ID, MNG_ID)
values (7934, 'MILLER', 'E7934', to_date('23-01-1982', 'dd-mm-yyyy'), 'CLERK', 1300, 10, 7698);

-------------------------------------------------------------------------------------------------

insert into Salary_Grade (GRADE, HIGH_SALARY, LOW_SALARY)
values (1, 9999, 3001);

2.2- MySQL创建数据库脚本

  • MYSQL SCRIPT:
create table DEPARTMENT (
   DEPT_ID integer not null,
   DEPT_NAME varchar(255) not null,
   DEPT_NO varchar(20) not null,
   LOCATION varchar(255),
   primary key (DEPT_ID),
   unique (DEPT_NO)
);

create table EMPLOYEE (
   EMP_ID bigint not null,
   EMP_NAME varchar(50) not null,
   EMP_NO varchar(20) not null,
   HIRE_DATE date not null,
   IMAGE longblob,
   JOB varchar(30) not null,
   SALARY float not null,
   DEPT_ID integer not null,
   MNG_ID bigint,
   primary key (EMP_ID),
   unique (EMP_NO)
);

create table SALARY_GRADE (
   GRADE integer not null,
   HIGH_SALARY float not null,
   LOW_SALARY float not null,
   primary key (GRADE)
);

create table TIMEKEEPER (
   Timekeeper_Id varchar(36) not null,
   Date_Time datetime not null,
   In_Out char(1) not null,
   EMP_ID bigint not null,
   primary key (Timekeeper_Id)
);

alter table EMPLOYEE
   add index FK75C8D6AE269A3C9 (DEPT_ID),
   add constraint FK75C8D6AE269A3C9
   foreign key (DEPT_ID)
   references DEPARTMENT (DEPT_ID);

alter table EMPLOYEE
   add index FK75C8D6AE6106A42 (EMP_ID),
   add constraint FK75C8D6AE6106A42
   foreign key (EMP_ID)
   references EMPLOYEE (EMP_ID);

alter table EMPLOYEE
   add index FK75C8D6AE13C12F64 (MNG_ID),
   add constraint FK75C8D6AE13C12F64
   foreign key (MNG_ID)
   references EMPLOYEE (EMP_ID);

alter table TIMEKEEPER
   add index FK744D9BFF6106A42 (EMP_ID),
   add constraint FK744D9BFF6106A42
   foreign key (EMP_ID)
   references EMPLOYEE (EMP_ID);
  • INSERT DATA (MYSQL)
insert into Department (DEPT_ID, DEPT_NAME, DEPT_NO, LOCATION)
values (10, 'ACCOUNTING', 'D10', 'NEW YORK');
 
insert into Department (DEPT_ID, DEPT_NAME, DEPT_NO, LOCATION)
values (20, 'RESEARCH', 'D20', 'DALLAS');
 
insert into Department (DEPT_ID, DEPT_NAME, DEPT_NO, LOCATION)
values (30, 'SALES', 'D30', 'CHICAGO');
 
insert into Department (DEPT_ID, DEPT_NAME, DEPT_NO, LOCATION)
values (40, 'OPERATIONS', 'D40', 'BOSTON');
 
-------------------------------------------------------------------------------------------------
 
 
insert into Employee (EMP_ID, EMP_NAME, EMP_NO, HIRE_DATE, JOB, SALARY, DEPT_ID, MNG_ID)
values (7839, 'KING', 'E7839', Str_To_Date('17-11-1981', '%d-%m-%Y'), 'PRESIDENT', 5000, 10, null);
 
insert into Employee (EMP_ID, EMP_NAME, EMP_NO, HIRE_DATE, JOB, SALARY, DEPT_ID, MNG_ID)
values (7566, 'JONES', 'E7566', Str_To_Date('02-04-1981', '%d-%m-%Y'), 'MANAGER', 2975, 20, 7839);
 
insert into Employee (EMP_ID, EMP_NAME, EMP_NO, HIRE_DATE, JOB, SALARY, DEPT_ID, MNG_ID)
values (7902, 'FORD', 'E7902', Str_To_Date('03-12-1981', '%d-%m-%Y'), 'ANALYST', 3000, 20, 7566);
 
insert into Employee (EMP_ID, EMP_NAME, EMP_NO, HIRE_DATE, JOB, SALARY, DEPT_ID, MNG_ID)
values (7369, 'SMITH', 'E7369', Str_To_Date('17-12-1980', '%d-%m-%Y'), 'CLERK', 800, 20, 7902);
 
insert into Employee (EMP_ID, EMP_NAME, EMP_NO, HIRE_DATE, JOB, SALARY, DEPT_ID, MNG_ID)
values (7698, 'BLAKE', 'E7698', Str_To_Date('01-05-1981', '%d-%m-%Y'), 'MANAGER', 2850, 30, 7839);
 
insert into Employee (EMP_ID, EMP_NAME, EMP_NO, HIRE_DATE, JOB, SALARY, DEPT_ID, MNG_ID)
values (7499, 'ALLEN', 'E7499', Str_To_Date('20-02-1981', '%d-%m-%Y'), 'SALESMAN', 1600, 30, 7698);
 
insert into Employee (EMP_ID, EMP_NAME, EMP_NO, HIRE_DATE, JOB, SALARY, DEPT_ID, MNG_ID)
values (7521, 'WARD', 'E7521', Str_To_Date('22-02-1981', '%d-%m-%Y'), 'SALESMAN', 1250, 30, 7698);
 
insert into Employee (EMP_ID, EMP_NAME, EMP_NO, HIRE_DATE, JOB, SALARY, DEPT_ID, MNG_ID)
values (7654, 'MARTIN', 'E7654', Str_To_Date('28-09-1981', '%d-%m-%Y'), 'SALESMAN', 1250, 30, 7698);
 
insert into Employee (EMP_ID, EMP_NAME, EMP_NO, HIRE_DATE, JOB, SALARY, DEPT_ID, MNG_ID)
values (7782, 'CLARK', 'E7782', Str_To_Date('09-06-1981', '%d-%m-%Y'), 'MANAGER', 2450, 30, 7839);
 
insert into Employee (EMP_ID, EMP_NAME, EMP_NO, HIRE_DATE, JOB, SALARY, DEPT_ID, MNG_ID)
values (7788, 'SCOTT', 'E7788', Str_To_Date('19-04-1987', '%d-%m-%Y'), 'ANALYST', 3000, 20, 7566);
 
insert into Employee (EMP_ID, EMP_NAME, EMP_NO, HIRE_DATE, JOB, SALARY, DEPT_ID, MNG_ID)
values (7844, 'TURNER', 'E7844', Str_To_Date('08-09-1981', '%d-%m-%Y'), 'SALESMAN', 1500, 30, 7698);
 
insert into Employee (EMP_ID, EMP_NAME, EMP_NO, HIRE_DATE, JOB, SALARY, DEPT_ID, MNG_ID)
values (7876, 'ADAMS', 'E7876', Str_To_Date('23-05-1987', '%d-%m-%Y'), 'CLERK', 1100, 20, 7698);
 
insert into Employee (EMP_ID, EMP_NAME, EMP_NO, HIRE_DATE, JOB, SALARY, DEPT_ID, MNG_ID)
values (7900, 'ADAMS', 'E7900', Str_To_Date('03-12-1981', '%d-%m-%Y'), 'CLERK', 950, 30, 7698);
 
insert into Employee (EMP_ID, EMP_NAME, EMP_NO, HIRE_DATE, JOB, SALARY, DEPT_ID, MNG_ID)
values (7934, 'MILLER', 'E7934', Str_To_Date('23-01-1982', '%d-%m-%Y'), 'CLERK', 1300, 10, 7698);
 
-------------------------------------------------------------------------------------------------
 
insert into Salary_Grade (GRADE, HIGH_SALARY, LOW_SALARY)
values (1, 9999, 3001);

2.3- SQL Server 创建 DB 脚本

  • SQL SERVER SCRIPT:
create table DEPARTMENT (
    DEPT_ID int not null,
    DEPT_NAME varchar(255) not null,
    DEPT_NO varchar(20) not null,
    LOCATION varchar(255),
    primary key (DEPT_ID),
    unique (DEPT_NO)
);

create table EMPLOYEE (
    EMP_ID numeric(19,0) not null,
    EMP_NAME varchar(50) not null,
    EMP_NO varchar(20) not null,
    HIRE_DATE datetime not null,
    IMAGE image,
    JOB varchar(30) not null,
    SALARY float not null,
    DEPT_ID int not null,
    MNG_ID numeric(19,0),
    primary key (EMP_ID),
    unique (EMP_NO)
);

create table SALARY_GRADE (
    GRADE int not null,
    HIGH_SALARY float not null,
    LOW_SALARY float not null,
    primary key (GRADE)
);

create table TIMEKEEPER (
    Timekeeper_Id varchar(36) not null,
    Date_Time datetime not null,
    In_Out char(1) not null,
    EMP_ID numeric(19,0) not null,
    primary key (Timekeeper_Id)
);

alter table EMPLOYEE
    add constraint FK75C8D6AE269A3C9
    foreign key (DEPT_ID)
    references DEPARTMENT;

alter table EMPLOYEE
    add constraint FK75C8D6AE6106A42
    foreign key (EMP_ID)
    references EMPLOYEE;

alter table EMPLOYEE
    add constraint FK75C8D6AE13C12F64
    foreign key (MNG_ID)
    references EMPLOYEE;

alter table TIMEKEEPER
    add constraint FK744D9BFF6106A42
    foreign key (EMP_ID)
    references EMPLOYEE;
  • INSERT DATA (SQL SERVER)
insert into Department (DEPT_ID, DEPT_NAME, DEPT_NO, LOCATION)
values (10, 'ACCOUNTING', 'D10', 'NEW YORK');
 
insert into Department (DEPT_ID, DEPT_NAME, DEPT_NO, LOCATION)
values (20, 'RESEARCH', 'D20', 'DALLAS');
 
insert into Department (DEPT_ID, DEPT_NAME, DEPT_NO, LOCATION)
values (30, 'SALES', 'D30', 'CHICAGO');
 
insert into Department (DEPT_ID, DEPT_NAME, DEPT_NO, LOCATION)
values (40, 'OPERATIONS', 'D40', 'BOSTON');

-------------------------------------------------------------------------------------------------


insert into Employee (EMP_ID, EMP_NAME, EMP_NO, HIRE_DATE, JOB, SALARY, DEPT_ID, MNG_ID)
values (7839, 'KING', 'E7839', Convert(Datetime,'17-11-1981', 105), 'PRESIDENT', 5000, 10, null);
 
insert into Employee (EMP_ID, EMP_NAME, EMP_NO, HIRE_DATE, JOB, SALARY, DEPT_ID, MNG_ID)
values (7566, 'JONES', 'E7566', Convert(Datetime,'02-04-1981', 105), 'MANAGER', 2975, 20, 7839);
 
insert into Employee (EMP_ID, EMP_NAME, EMP_NO, HIRE_DATE, JOB, SALARY, DEPT_ID, MNG_ID)
values (7902, 'FORD', 'E7902', Convert(Datetime,'03-12-1981', 105), 'ANALYST', 3000, 20, 7566);
 
insert into Employee (EMP_ID, EMP_NAME, EMP_NO, HIRE_DATE, JOB, SALARY, DEPT_ID, MNG_ID)
values (7369, 'SMITH', 'E7369', Convert(Datetime,'17-12-1980', 105), 'CLERK', 800, 20, 7902);
 
insert into Employee (EMP_ID, EMP_NAME, EMP_NO, HIRE_DATE, JOB, SALARY, DEPT_ID, MNG_ID)
values (7698, 'BLAKE', 'E7698', Convert(Datetime,'01-05-1981', 105), 'MANAGER', 2850, 30, 7839);
 
insert into Employee (EMP_ID, EMP_NAME, EMP_NO, HIRE_DATE, JOB, SALARY, DEPT_ID, MNG_ID)
values (7499, 'ALLEN', 'E7499', Convert(Datetime,'20-02-1981', 105), 'SALESMAN', 1600, 30, 7698);
 
insert into Employee (EMP_ID, EMP_NAME, EMP_NO, HIRE_DATE, JOB, SALARY, DEPT_ID, MNG_ID)
values (7521, 'WARD', 'E7521', Convert(Datetime,'22-02-1981', 105), 'SALESMAN', 1250, 30, 7698);
 
insert into Employee (EMP_ID, EMP_NAME, EMP_NO, HIRE_DATE, JOB, SALARY, DEPT_ID, MNG_ID)
values (7654, 'MARTIN', 'E7654', Convert(Datetime,'28-09-1981', 105), 'SALESMAN', 1250, 30, 7698);
 
insert into Employee (EMP_ID, EMP_NAME, EMP_NO, HIRE_DATE, JOB, SALARY, DEPT_ID, MNG_ID)
values (7782, 'CLARK', 'E7782', Convert(Datetime,'09-06-1981', 105), 'MANAGER', 2450, 30, 7839);
 
insert into Employee (EMP_ID, EMP_NAME, EMP_NO, HIRE_DATE, JOB, SALARY, DEPT_ID, MNG_ID)
values (7788, 'SCOTT', 'E7788', Convert(Datetime,'19-04-1987', 105), 'ANALYST', 3000, 20, 7566);
 
insert into Employee (EMP_ID, EMP_NAME, EMP_NO, HIRE_DATE, JOB, SALARY, DEPT_ID, MNG_ID)
values (7844, 'TURNER', 'E7844', Convert(Datetime,'08-09-1981', 105), 'SALESMAN', 1500, 30, 7698);
 
insert into Employee (EMP_ID, EMP_NAME, EMP_NO, HIRE_DATE, JOB, SALARY, DEPT_ID, MNG_ID)
values (7876, 'ADAMS', 'E7876', Convert(Datetime,'23-05-1987', 105), 'CLERK', 1100, 20, 7698);
 
insert into Employee (EMP_ID, EMP_NAME, EMP_NO, HIRE_DATE, JOB, SALARY, DEPT_ID, MNG_ID)
values (7900, 'ADAMS', 'E7900', Convert(Datetime,'03-12-1981', 105), 'CLERK', 950, 30, 7698);
 
insert into Employee (EMP_ID, EMP_NAME, EMP_NO, HIRE_DATE, JOB, SALARY, DEPT_ID, MNG_ID)
values (7934, 'MILLER', 'E7934', Convert(Datetime,'23-01-1982', 105), 'CLERK', 1300, 10, 7698);

-------------------------------------------------------------------------------------------------

insert into Salary_Grade (GRADE, HIGH_SALARY, LOW_SALARY)
values (1, 9999, 3001);
代码下载:http://pan.baidu.com/s/1skyvdKh

上一篇:Spring SimpleJdbcTemplate类命名参数实例

下一篇:Spring JavaConfig实例

关注微信小程序
程序员编程王-随时随地学编程

扫描二维码
程序员编程王

扫一扫关注最新编程教程