public class DbHelper { public static QueryRunner getQueryRunner() { DataSource ds = null; try { Context context = new InitialContext(); ds = (DataSource) context.lookup("java:/comp/env/jdbc/oracleds"); //System.out.println("oracle鏈接池成功"); } catch (Exception e) { System.out.println("oracle鏈接池失敗"); } QueryRunner qr = new QueryRunner(ds); return qr; } } 查詢就直接使用QueryRunner裏的query();方法就能夠了,其它的增刪改就直接用QueryRunner裏的update();方法就好了。
以下是查詢的示例:
String sql = "select empno,ename,job,mgr,hiredate,sal,comm,deptno from emp"; QueryRunner qr = DbHelper.getQueryRunner(); List list = (List) qr.query(sql, new BeanListHandler(Emp.class));
以下是刪除的示例:
String id = request.getParameter("id"); String sql = "delete from comment where id=" + id; QueryRunner qr = DbHelper.getQueryRunner(); qr.update(sql);
以下是修改的示例:
String id = request.getParameter("id"); String username = request.getParameter("name"); String content = request.getParameter("content"); String sql = "update comment set username=?,content=? where id=?"; String params[] = { username, content, id }; QueryRunner qr = DbHelper.getQueryRunner(); qr.update(sql, params);