JDBC连接mysql数据库CRUD封装类

/**
* JDBC封装类
* 
* @author fish
*/
public class JDBCUtil {

private ResultSet rs;
private Statement stmt;
private Connection conn;
private String url = "jdbc:mysql://localhost:3306/jdbc";
private String className = "com.mysql.jdbc.Driver";
private String username = "root";
private String password = "123456";

/**
* 构造函数
*/
public JDBCUtil() {
try {
Class.forName(className);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}

/**
* 创建数据库连接
*/
public Connection getConn() {
try {
conn = DriverManager.getConnection(url, username, password);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}

/**
* 获取Statement记录
*/
public Statement getStmt() {
try {
conn = getConn();
stmt = conn.createStatement();
} catch (Exception e) {
e.printStackTrace();
}
return stmt;
}

/**
* 调法上面的方法,查询数据库,返回单个结果 
* 其他类调用过程如下: 
* JDBCUtil ju=new JDBCUtil(); ResultSet rs=ju.getrs(sql); 
* while(rs.next()){ 
* String s1 = rs.getInt(1); 
* }
*/
public ResultSet getSingleResult(String sql) {
if (sql == null)
sql = "";
try {
stmt = getStmt();
rs = stmt.executeQuery(sql);
} catch (SQLException e) {
e.printStackTrace();
}
return rs;
}

/**
* 获取Statement记录集
*/
public Statement getStmed() {
try {
conn = getConn();
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
} catch (Exception e) {
e.printStackTrace();
}
return stmt;
}

/**
* 调法上面的方法,查询数据库,返回一个结果集 
* 其他类调用过程如下: 
* JDBCUtil ju=new JDBCUtil(); 
* ResultSet rs=ju.getRs(sql); 
* while(rs.next()){ 
* String s1 = r.getInt(1); 
* String s2 = r.getInt(2); 
* }
*/
public ResultSet getResultSet(String sql) {
if (sql == null)
sql = "";
try {
stmt = getStmed();
rs = stmt.executeQuery(sql);
} catch (SQLException e) {
e.printStackTrace();
}
return rs;
}

/**
* 对数据库进行更新操作,适合SQL的insert语句和update语句 
* 返回一个int值,表示更新的记录数 若返回为0,表示更新失败
* 其他类调用过程如下: 
* JDBCUtil ju=new JDBCUtil(); int i=db.update(sql); 
* if(i==0){ return mapping.findForward("false"); } 
* else return mapping.findForward("success");
*/
public int update(String sql) {
int flag = 0;
if (sql == null)
sql = "";
try {
stmt = getStmed();
flag = stmt.executeUpdate(sql);
} catch (SQLException e) {
e.printStackTrace();
flag = 0;
}
return flag;
}

/**
* 删除一条数据 
* 其他类调用过程如下: 
* DB db=new DB(); db.delete(sql);
*/
public boolean delete(String sql) {
boolean flag = false;
if (sql == null)
sql = "";
try {
stmt = getStmed();
flag = stmt.execute(sql);
} catch (SQLException e) {
e.printStackTrace();
}
return flag;
}

/**
* 插入一条数据 
* 其他类调用过程如下: 
* JDBCUtil ju=new JDBCUtil(); 
* ju.delete(sql);
*/
public boolean insert(String sql) {
boolean flag = false;
if (sql == null)
sql = "";
try {
stmt = getStmed();
flag = stmt.execute(sql);
} catch (SQLException e) {
e.printStackTrace();
}
return flag;
}
/**
* 断开数据库连接 
* 其他类调用过程如下: JDBCUtil ju=new JDBCUtil(); ju.closed();
*/
public void closed() {
try {
if (rs != null)
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
try {
if (stmt != null)
stmt.close();
} catch (Exception e) {
e.printStackTrace();
}
try {
if (conn != null)
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}

}



ps:写于2011-01-06 09:13


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值