这里所需要用到的jar包下载地址:点击下载
程序完整代码:点击这里跳转
第一步:创建JDBCUtils工具类(JDBCUtils.java)
package cn.example.utils;
import java.sql.*;
public class JDBCUtils {
// 加载驱动,并建立数据库连接
public static Connection getConnection() throws SQLException,
ClassNotFoundException {
Class.forName("oracle.jdbc.OracleDriver");
String url = "jdbc:oracle:thin:@localhost:1521:orcl";
String username = "scott";
String password = "tiger";
Connection conn = DriverManager.getConnection(url, username,
password);
return conn;
}
// 关闭数据库连接,释放资源
public static void release(Statement stmt, Connection conn) {
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
stmt = null;
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
conn = null;
}
}
public static void release(ResultSet rs, Statement stmt,
Connection conn){
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
rs = null;
}
release(stmt, conn);
}
}
第二步:创建javaBean(这里用Scott用户下的emp表作为测试)
在package cn.example.bean;
import java.util.Date;
public class Emp {
private int empno;
private String ename;
private String job;
private int mgr;
private Date hiredate;
private int sal;
private int comm;
private int deptno;
public Emp() {
}
public Emp(int empno, String ename, String job, int mgr, Date hiredate, int sal, int comm, int deptno) {
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 int getEmpno() {
return empno;
}
public void setEmpno(int empno) {
this.empno = empno;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public int getMgr() {
return mgr;
}
public void setMgr(int mgr) {
this.mgr = mgr;
}
public Date getHiredate() {
return hiredate;
}
public void setHiredate(Date hiredate) {
this.hiredate = hiredate;
}
public int getSal() {
return sal;
}
public void setSal(int sal) {
this.sal = sal;
}
public int getComm() {
return comm;
}
public void setComm(int comm) {
this.comm = comm;
}
public int getDeptno() {
return deptno;
}
public void setDeptno(int deptno) {
this.deptno = deptno;
}
@Override
public String toString() {
return "Emp{" +
"empno=" + empno +
", ename='" + ename + '\'' +
", job='" + job + '\'' +
", mgr=" + mgr +
", hiredate=" + hiredate +
", sal=" + sal +
", comm=" + comm +
", deptno=" + deptno +
'}';
}
}
第三步:创建DAO
package cn.example.dao;
import cn.example.bean.Emp;
import cn.example.utils.JDBCUtils;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
public class EmpDao {
// 查询所有的Emp对象
public static ArrayList<Emp> getAll() {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
ArrayList<Emp> list = new ArrayList<Emp>();
try {
// 获得数据的连接
conn = JDBCUtils.getConnection();
// 获得Statement对象
stmt = conn.createStatement();
// 发送SQL语句
String sql = "SELECT * FROM emp";
rs = stmt.executeQuery(sql);
// 处理结果集
while (rs.next()) {
Emp emp = new Emp();
emp.setEmpno(rs.getInt("empno"));
emp.setEname(rs.getString("ename"));
emp.setJob(rs.getString("job"));
emp.setMgr(rs.getInt("mgr"));
emp.setHiredate(rs.getDate("hiredate"));
emp.setComm(rs.getInt("comm"));
emp.setDeptno(rs.getInt("deptno"));
list.add(emp);
}
return list;
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCUtils.release(rs, stmt, conn);
}
return null;
}
}
第四步:创建测试类
package cn.example;
import cn.example.bean.Emp;
import cn.example.dao.EmpDao;
import java.util.ArrayList;
public class test {
public static void main(String[] args) {
ArrayList<Emp> empList = EmpDao.getAll();
for(int i = 0;i < empList.size();i++){
System.out.println(i + ":" + empList.get(i));
}
}
}
第五步:运行测试连接结果