1、想通过Java对Oracle数据库进行操作,那么就要首先加载驱动,将Oracle下面的JDBC的jar包导入到工作区来,并加入到lib里面,然后build path,这个时候就会自动创建一个reference libraries ,里面自动加载了导入的jar包
2、然后还要build path添加一个Java的类库用于测试的时候直接调用 ,通过build path ,找到add libraries ,添加Junit4,
3、加载好所有的文件之后,就是创建相应的包,一般我们需要一个连接数据库的util的工具包,这里命名的话一般是type.company.util进行命名,(这里的点号在Java中表示分级,也就是文件夹之间的包含关系,相当于建立一个type文件夹,然后type文件夹里面有一个company文件夹)这里我们建立一个包为com.zt.util, 然后 在这个工具包下面我们创建一个JDBCUtil.java的class文件
4、然后连接好数据库之后,我们还需要创建一个数据库的操作对象包DAO(database access object),这里我们是对员工表进行操作,所以我们就定义一个的包,com.zt.dao,然后在这个包里面创建一个emp表的操作对象 EmpDAO.java 的class文件。在这个包里面我们定义一系列对数据库进行操作的函数或者方法
5、然后我们还需要一个实体类的包com.zt.entity,用于为数据表添加相应的属性
然后还需要一个用于测试的包 com.zt.test
6、最后连接数据库的时候,我们还需要一个properties的文件
用于保存操作数据库的驱动,以及数据库的地址,还有数据库的用户名和密码
这个文件一般我们就放在src文件夹下面,后面添加的时候比较好添加
driver=oracle.jdbc.driver.OracleDriver----》这个是我们添加的jar包里面的Oracledriver的路径,依次复制下来就行
url=jdbc:oracle:thin@localhost:1521:orcl----》这是数据库的地址,意思是jdbc下面的Oracle数据库连接到本地的orcl数据库
user=scott
pwd=tiger
所有的准备工作好了之后,下面就是敲代码了
首先我们先完成数据库的连接,也就是com.zt.util下面的JDBCUtil.java 文件
对于数据库的连接,我们分为三部分,
1、第一步就是加载驱动,相当于我们在桌面上点开数据库图标
2、然后第二步就是获取连接,相当于我们需要选择数据库,输入用户名和密码,,然后这里就是我们url user pwd
3、第三步就是关闭连接,关闭的时候要注意先开的后关,这里我们需要先关闭结果集rs 然后st/ps预处理,最后关闭连接con
//由于驱动我们只需要加载一次就行了
//所以这里我们定义一个静态块来执行加载驱动
static{
//采用大写的类的forName()属性来获取到驱动的名字
try {
Class.forName("oracle.jdbc.driver.OracleDriver");--->这里需要catch异常
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
下面我们对这个程序进行一定的扩展
将这个驱动的名字作为一个变量提取出来,让获取驱动这个方法具有一定的灵活性和自动型
//定义一个资源包类型的变量用于获取资源包的内容
//这个变量为私有的静态的类型
private static ResourceBundle rb = ResourceBundle.getBundle("jdbc");
//资源包就在src里面,所以直接写资源包的名字,不要后缀名
//然后通过资源包来获取里面的驱动字段的内容
//由于driver是字符串类型的所以用rb.gerstring属性来获取
//注意这里是一个字段名要打引号,不是变量
//加载驱动需要在一个静态块里面执行
//所以要先定义一个static块
static{
try {
Class.forName(rb.getString("driver"));
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
那么驱动的加载部分我们就算完美的完成了
接下来进行获取连接部分,也就是我们打开了软件之后需要选择数据库,然后输入用户名和密码
//获取连接
//获取连接采用驱动管理来获取连接
//同样的获取连接我们也定义一个函数来执行,不过这里不再是一个静态块
//而是要给公共函数,返回一个connection的类型参数
public Connection getConnection(){
//为了先不报错,先定义一个connection的变量con用于返回
Connection con = null;
//这里的url user password 都需要通过资源包来获取相应的字段名的内容
try {
//注意这里的url user pwd 都是资源包jdbc.properties里面的键值对的键的字段名,是字符串格式需要引号
//而不是变量,相当于这里是一个map,通过键(这里的键是字符串的形式)来获取对应的值
DriverManager.getConnection(rb.getString("url"), rb.getString("user"), rb.getString("pwd"));
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return con;
}
一定注意我们这里的加载驱动和获取连接以及释放连接都是三个对立的函数方法
//关闭连接
//关闭连接的顺序是结果集rs 预处理ps/st 连接 con
//由于他们都有初值null
//再关闭之前都需要先判断是否为空,为空的话就不用关闭操作了
//这里的关闭没有返回值
//但是需要传入三个需要关闭的参数进来
public void closeALL(ResultSet rs,PreparedStatement ps,Connection con){
try {
if(rs!=null){
rs.close();
}
if(ps!=null){
ps.close();
}
if(con!=null){
con.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
那么到这里我们连接数据库这一个类就写好了,下面就是准备数据库的实体类
这里我们由于要涉及到两张表emp和dept所以我们创建两个实体类
package com.zt.entity;
import java.util.Date;
public class Emp {
// int empno, String ename, String job, Emp mgr, Date hiredate,
// double sal, double comm, Dept deptno
// 先定义实体类Emp的属性 实体的属性一般定义为私有,不让随便修改
// 然后通过set get 方法进行赋值和获取
private int empno;
private String ename;
private String job;
// 这里的mgr为外键,属性为Emp类型
private Emp mgr;
// 日期为date类型
private Date hiredate;
private double sal;
private double comm;
// 这里的部门编号也是外键
// 属性为Dept
private Dept deptno;
//有参构造
public Emp(int empno, String ename, String job, Emp mgr, Date hiredate,
double sal, double comm, Dept deptno) {
super();
this.empno = empno;
this.ename = ename;
this.job = job;
this.mgr = mgr;
this.hiredate = hiredate;
this.sal = sal;
this.comm = comm;
this.deptno = deptno;
}
//无参构造
public Emp() {
super();
// TODO Auto-generated constructor stub
}
//重写hash code方法
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
long temp;
temp = Double.doubleToLongBits(comm);
result = prime * result + (int) (temp ^ (temp >>> 32));
result = prime * result + ((deptno == null) ? 0 : deptno.hashCode());
result = prime * result + empno;
result = prime * result + ((ename == null) ? 0 : ename.hashCode());
result = prime * result
+ ((hiredate == null) ? 0 : hiredate.hashCode());
result = prime * result + ((job == null) ? 0 : job.hashCode());
result = prime * result + ((mgr == null) ? 0 : mgr.hashCode());
temp = Double.doubleToLongBits(sal);
result = prime * result + (int) (temp ^ (temp >>> 32));
return result;
}
//重写equal方法
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Emp other = (Emp) obj;
if (Double.doubleToLongBits(comm) != Double
.doubleToLongBits(other.comm))
return false;
if (deptno == null) {
if (other.deptno != null)
return false;
} else if (!deptno.equals(other.deptno))
return false;
if (empno != other.empno)
return false;
if (ename == null) {
if (other.ename != null)
return false;
} else if (!ename.equals(other.ename))
return false;
if (hiredate == null) {
if (other.hiredate != null)
return false;
} else if (!hiredate.equals(other.hiredate))
return false;
if (job == null) {
if (other.job != null)
return false;
} else if (!job.equals(other.job))
return false;
if (mgr == null) {
if (other.mgr != null)
return false;
} else if (!mgr.equals(other.mgr))
return false;
if (Double.doubleToLongBits(sal) != Double.doubleToLongBits(other.sal))
return false;
return true;
}
//重写tostring方法
@Override
public String toString() {
return "Emp [empno=" + empno + ", ename=" + ename + ", job=" + job
+ ", mgr=" + mgr + ", hiredate=" + hiredate + ", sal=" + sal
+ ", comm=" + comm + ", deptno=" + deptno + "]";
}
}
package com.zt.entity;
public class Dept {
// int deptNo, String deptName, String deptLoc
private int deptNo;
private String deptName;
private String deptLoc;
public Dept() {
super();
// TODO Auto-generated constructor stub
}
public int getDeptNo() {
return deptNo;
}
public void setDeptNo(int deptNo) {
this.deptNo = deptNo;
}
public String getDeptName() {
return deptName;
}
public void setDeptName(String deptName) {
this.deptName = deptName;
}
public String getDeptLoc() {
return deptLoc;
}
public void setDeptLoc(String deptLoc) {
this.deptLoc = deptLoc;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((deptLoc == null) ? 0 : deptLoc.hashCode());
result = prime * result
+ ((deptName == null) ? 0 : deptName.hashCode());
result = prime * result + deptNo;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Dept other = (Dept) obj;
if (deptLoc == null) {
if (other.deptLoc != null)
return false;
} else if (!deptLoc.equals(other.deptLoc))
return false;
if (deptName == null) {
if (other.deptName != null)
return false;
} else if (!deptName.equals(other.deptName))
return false;
if (deptNo != other.deptNo)
return false;
return true;
}
@Override
public String toString() {
return "Dept [deptNo=" + deptNo + ", deptName=" + deptName
+ ", deptLoc=" + deptLoc + "]";
}
public Dept(int deptNo, String deptName, String deptLoc) {
super();
this.deptNo = deptNo;
this.deptName = deptName;
this.deptLoc = deptLoc;
}
}
创建好实体类之后就是创建我们DAO数据库操作对象的方式了
注意由于这里emp的外键是dept,所以我们需要先创建Dept的类型,也就是需要先创建外键
而DAO里面的操作主要就是增删改查的方法,所以我们只需要再DeptDAO.Java里面为每一种方法定义一个函数,然后再Test.Java里面调用相应的函数就可以i了