韩顺平老师的细说JSP视频的购物车项目中的utils包中有三个工具类,他们是在哪一集中讲到的?
展开全部
DBUtils.java
package org.dhb.utils;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DBUtil {
//定义连接数据库
private static Connection ct = null;
//定义操作数据库的类
private static PreparedStatement ps = null;
//输出数据的类
private static ResultSet rs = null;
// 静态方式取得链接
public Connection getConn() {
//数据库用户名
String username = "scott";
//数据库用户密码
String passwd = "duanhaibin";
String url = "jdbc:oracle:thin:@localhost:1521:ORCL";
//获取驱动
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
ct = DriverManager.getConnection(url,username,passwd);
} catch (Exception e) {
e.printStackTrace();
}
return ct;
}
// 释放资源 public void close() {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (ps != null) {
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (ct != null) {
try {
ct.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
SqlHelper.java
package org.dhb.utils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import javax.crypto.spec.PSource;import javax.xml.bind.ParseConversionEvent;
import com.sun.org.apache.bcel.internal.generic.NEW;
public class SqlHelper {
private Connection ct=null;
private ResultSet rs=null;
private PreparedStatement ps=null;
public ArrayList executeQuery(String sql,String paras[]){
ArrayList al = new ArrayList();
DBUtil dbu=new DBUtil();
try{
ct = dbu.getConn();
ps = ct.prepareStatement(sql);
for(int i=0;i<paras.length;i++){
ps.setString(i+1,paras[i]);
}
rs=ps.executeQuery();
ResultSetMetaData rsmd=rs.getMetaData();
int columnNum=rsmd.getColumnCount();
while(rs.next()){
Object objects[] = new Object[columnNum];
for (int i = 0; i < objects.length; i++) {
objects[i]=rs.getObject(i+1);
}
al.add(objects);
}
}catch(Exception e){
e.printStackTrace();
}finally{
dbu.close();
}
return al;
}
}
package org.dhb.utils;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DBUtil {
//定义连接数据库
private static Connection ct = null;
//定义操作数据库的类
private static PreparedStatement ps = null;
//输出数据的类
private static ResultSet rs = null;
// 静态方式取得链接
public Connection getConn() {
//数据库用户名
String username = "scott";
//数据库用户密码
String passwd = "duanhaibin";
String url = "jdbc:oracle:thin:@localhost:1521:ORCL";
//获取驱动
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
ct = DriverManager.getConnection(url,username,passwd);
} catch (Exception e) {
e.printStackTrace();
}
return ct;
}
// 释放资源 public void close() {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (ps != null) {
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (ct != null) {
try {
ct.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
SqlHelper.java
package org.dhb.utils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import javax.crypto.spec.PSource;import javax.xml.bind.ParseConversionEvent;
import com.sun.org.apache.bcel.internal.generic.NEW;
public class SqlHelper {
private Connection ct=null;
private ResultSet rs=null;
private PreparedStatement ps=null;
public ArrayList executeQuery(String sql,String paras[]){
ArrayList al = new ArrayList();
DBUtil dbu=new DBUtil();
try{
ct = dbu.getConn();
ps = ct.prepareStatement(sql);
for(int i=0;i<paras.length;i++){
ps.setString(i+1,paras[i]);
}
rs=ps.executeQuery();
ResultSetMetaData rsmd=rs.getMetaData();
int columnNum=rsmd.getColumnCount();
while(rs.next()){
Object objects[] = new Object[columnNum];
for (int i = 0; i < objects.length; i++) {
objects[i]=rs.getObject(i+1);
}
al.add(objects);
}
}catch(Exception e){
e.printStackTrace();
}finally{
dbu.close();
}
return al;
}
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
把他全下载,
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2012-09-23
展开全部
?这个 自己 就可以写的
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询