/**
* 机器上装了Oracle之后,一般jdbc驱动包会在安装目录下,我们不必要去网上找驱动包
* 以下是oracle11g的驱动包所在路劲
* D:\app\Administrator\product\11.1.0\db_1\jdbc\lib
* ojdbc5.jar ojdbc5_g.jar ojdbc6.jar ojdbc6_g.jar
*以添加外部架包的方式对这几个架包引用便可以.
*/
//数据访问类
package dao;
import java.sql.*;
public class BaseDao {
public static final String DRIVER="oracle.jdbc.driver.OracleDriver";
public static final String URL="jdbc.oracle:thin:@hull:1521:oracle";
public static final String UID="scott";
public static final String PWD="000000";
/***
* 加载驱动,打开数据库连接
* @return
*/
public Connection getConn(){
try {
Class.forName(DRIVER);
System.out.println("数据库驱动加载成功");
} catch (ClassNotFoundException e) {
System.out.println("找不到数据库驱动");
e.printStackTrace();
}
try {
Connection conn=DriverManager.getConnection("jdbc:oracle:thin:@hull:1521:oracle",UID,PWD);
System.out.println("连接数据库成功");
return conn;
} catch (SQLException e) {
System.out.println("连接数据库失败");
e.printStackTrace();
}
return null;
}
/***
* 关闭连接释放所有资源
* @param conn
* @param pstmt
* @param rs
* @throws SQLException
*/
public void closeAll(Connection conn,PreparedStatement pstmt,ResultSet rs) throws SQLException
{
if(rs!=null)rs.close();
if(pstmt!=null)pstmt.close();
if(conn!=null)conn.close();
}
public int executeSql(String preParedSql,String[] param) throws SQLException
{
Connection conn=this.getConn();
PreparedStatement pstmt=null;
pstmt=conn.prepareStatement(preParedSql);
if(param!=null)
{
for(int i=1;i<=param.length;i++)
pstmt.setString(i, param[i-1]);
}
int num=pstmt.executeUpdate();
this.closeAll(conn, pstmt, null);
return num;
}
}
//数据实体类
package bean;
public class Emp {
private int empNo;
private String eName;
private String job;
private int mar;
private String hireDate;
private double sal;
private double comm;
private int depNo;
public int getEmpNo() {
return empNo;
}
public void setEmpNo(int empNo) {
this.empNo = empNo;
}
public String getEName() {
return eName;
}
public void setEName(String name) {
eName = name;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public int getMar() {
return mar;
}
public void setMar(int mar) {
this.mar = mar;
}
public String getHireDate() {
return hireDate;
}
public void setHireDate(String hireDate) {
this.hireDate = hireDate;
}
public double getSal() {
return sal;
}
public void setSal(double sal) {
this.sal = sal;
}
public double getComm() {
return comm;
}
public void setComm(double comm) {
this.comm = comm;
}
public int getDepNo() {
return depNo;
}
public void setDepNo(int depNo) {
this.depNo = depNo;
}
}
//接口类
package imp;
import java.util.*;
import java.sql.*;
import bean.*;
import dao.*;
public class EmpImp extends BaseDao implements EmpDao {
private Connection conn=null;
private PreparedStatement pstmt=null;
private ResultSet rs=null;
/*获取所有员工信息
* (non-Javadoc)
* @see dao.EmpDao#getAll()
*/
public List getAll() {
List list=new ArrayList();
conn=this.getConn();
String sql="select * from scott.emp";
try {
pstmt=conn.prepareStatement(sql);
rs=pstmt.executeQuery();
while(rs.next())
{
Emp emp=new Emp();
emp.setEmpNo(rs.getInt("empNo"));
emp.setComm(rs.getDouble("comm"));
emp.setDepNo(rs.getInt("deptNo"));
emp.setEName(rs.getString("eName"));
emp.setHireDate(rs.getString("hireDate"));
emp.setJob(rs.getString("job"));
emp.setMar(rs.getInt("mgr"));
emp.setSal(rs.getDouble("sal"));
list.add(emp);
}
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
}