📄 basedao.java
字号:
package eshopsys.tools.base;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Date;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.PreparedStatement;
import eshopsys.tools.database.DataBaseTool;
public abstract class BaseDao {
protected Connection con=null;
public BaseDao()
{
try {
con=DataBaseTool.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
}
//把结果集包装成实体对象数组,BaseDao的子类必须实现该方法
protected abstract BaseEntity[] pack(ResultSet resultset) throws SQLException;
//根据传入的sql语句进行查询
public ResultSet selectRSByQueryString(String sql) throws
SQLException {
Statement ps = null;
ResultSet rs = null;
try {
ps = con.createStatement();
rs = ps.executeQuery(sql);
//将结果集包装成实体对象数组
}
catch (Exception e) {
//将捕获的异常抛出,最终在视图层处理
throw new SQLException("执行数据库查找操作的过程中发生异常 : " +
e.getMessage());
}
//finally {
//关闭各项数据库资源
//DataBaseTool.close(rs, ps, null);
//}
return rs;
}
//////////////////////////////////////////////////////////////////////////
//根据传入的sql语句进行查询
public BaseEntity[] selectArrayByQueryString( String sql) throws
SQLException {
BaseEntity[] entitys = null;
Statement ps = null;
ResultSet rs = null;
try {
ps = con.createStatement();
rs = ps.executeQuery(sql);
//将结果集包装成实体对象数组
entitys = pack(rs);
}
catch (Exception e) {
//将捕获的异常抛出,最终在视图层处理
throw new SQLException("执行数据库查找操作的过程中发生异常 : " +
e.getMessage());
}
finally {
//关闭各项数据库资源
DataBaseTool.close(rs, ps, null);
}
return entitys;
}
//////////////////////////////////////////////////////////////////////////
//根据数据库中的表名和自增长ID查询记录
public BaseEntity selectById(String tableName,String properyName, int id) throws
SQLException {
BaseEntity entity = null;
PreparedStatement ps = null;
ResultSet rs = null;
StringBuffer buffer = new StringBuffer();
try {
// 构造查询sql语句
buffer.append("SELECT * FROM " + tableName);
buffer.append(" WHERE"+" "+properyName+" = " + id);
ps = con.prepareStatement(buffer.toString());
//执行查询
rs = ps.executeQuery();
BaseEntity[] entitys = (BaseEntity[]) pack(rs);
if (entitys != null) {
entity = entitys[0];
}
}
catch (Exception e) {
//将捕获的异常抛出,最终在视图层处理
throw new SQLException(
"在表 " + tableName + " 中根据自增长ID进行查找的过程中发生异常 : " + e.getMessage());
}
finally {
DataBaseTool.close(rs, ps, null);
}
return entity;
}
//根据表名和自增长ID删除一条记录
public int deleteById(String tableName, String properyName, int id) throws
SQLException {
PreparedStatement ps = null;
StringBuffer buffer = new StringBuffer();
try {
//构早删除sql语句
buffer.append("DELETE FROM " + tableName);
buffer.append(" WHERE"+" "+properyName+" = " + id);
ps = con.prepareStatement(buffer.toString());
//执行sql语句
ps.executeUpdate();
}
catch (Exception e) {
//将捕获的异常抛出,最终在视图层处理
throw new SQLException("删除数据时发生异常 : " + e.getMessage());
}
finally {
DataBaseTool.close(ps);
}
return id;
}
//根据表名删除指定表的所有记录
public void deleteAllRecords(String tableName) throws
SQLException {
PreparedStatement ps = null;
StringBuffer buffer = new StringBuffer();
try {
//构早删除sql语句
buffer.append("DELETE FROM " + tableName);
ps = con.prepareStatement(buffer.toString());
//执行sql语句
ps.executeUpdate();
}
catch (Exception e) {
//将捕获的异常抛出,最终在视图层处理
throw new SQLException("删除数据时发生异常 : " + e.getMessage());
}
finally {
DataBaseTool.close(ps);
}
}
//////////////////////////////////////////////////////////////////////////
//根据表名删除指定表的所有记录
public int getMaxPrimaryId(String tableName, String properyName) throws
SQLException {
int MaxId=0;
PreparedStatement ps = null;
StringBuffer buffer = new StringBuffer();
try {
//构早删除sql语句
buffer.append("SELECT MAX("+properyName+") as maxid FROM " + tableName);
ps = con.prepareStatement(buffer.toString());
//执行sql语句
ResultSet rs=ps.executeQuery();
if(rs.next())
{
MaxId=rs.getInt("maxid");
}
}
catch (Exception e) {
//将捕获的异常抛出,最终在视图层处理
throw new SQLException("删除数据时发生异常 : " + e.getMessage());
}
finally {
DataBaseTool.close(ps);
}
return MaxId+1;
}
//////////////////////////////////////////////////////////////////////////
public void CloseConnection()
{
DataBaseTool.close(con);
}
public void beginTransaction()throws Exception
{
if (con == null) {
throw new Exception("不能为一个空连接开启事务!");
}
else {
con.setAutoCommit(false);
return;
}
}
public void endTransaction(boolean success) throws Exception
{
if (con == null) {
throw new Exception("不能为一个空连接结束事务!");
}
if (success) {
con.commit();
}
else {
con.rollback();
}
}
//////////////////////////////////////////////////////////////////////////
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -