文档介绍://连接数据库代码
.tools;
import ;
import ;
import ;
public class DBUtil {
public static Connection getConnection() throws ClassNotFoundException, SQLException{
// 1. 导入Jar包
// 2. 加载驱动
("");
// 3. 获得数据库连接
Connection conn = ("jdbc:oracle:thin:***@localhost:1521:orcl", "scott", "tiger");
return conn;
}
}
//EMP表的增删改查功能实现
.dao;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
.;
.;
public class EmpDAO {
public static void main(String[] args) throws ClassNotFoundException,
SQLException {
EmpDAO dao = new EmpDAO();
();
}
public ArrayList<Emp> queryAll() throws ClassNotFoundException,
SQLException {
Connection conn = ();
// 4. 创建Statement
Statement s = ();
// 5. 执行SQL语句
String sql = "select * from emp";
// 6. 处理结果集
ResultSet rs = (sql);
ArrayList<Emp> list = new ArrayList<Emp>();
while (()) {
Emp e = new Emp();
(("empno"));
(("ename"));
(("job"));
(("mgr"));
(("sal"));
(("comm"));
Date date = ("hiredate");
(() + 1900 + "年" + (() + 1) + "月" + () + "日");
(("deptno"));
(e);
}
// 7. 关闭
();
();
();
return list;
}
public Emp findById(int id) throws ClassNotFoundException, SQLException {
Connection conn = ();
// SQL语句生成
String sql = "select * from emp where empno=?";
PreparedStatement ps = (sql);
(