您的位置:首页 > 数据库 > MySQL

JDBC连接mysql数据库CRUD封装类

2013-10-27 13:09 351 查看
/**
* 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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: