DbHelper 标准类
import java.io.*; import java.sql.*; import java.util.*; import javax.servlet.jsp.jstl.sql.*; public class DBHelper { private String sql; //要传入的sql语句 public void setSql(String sql) { this.sql = sql; } private List sqlValues; //sql语句的参数 public void setSqlValues(List sqlValues) { this.sqlValues = sqlValues; } private Connection con; //连接对象 public void setCon(Connection con) { this.con = con; } public DBHelper(){ this.con=getConnection(); //给Connection的对象赋初值 } /** * 获取数据库连接 * @return */ private Connection getConnection(){ String driver_class=null; String driver_url=null; String database_user=null; String database_password=null; try { InputStream fis=this.getClass().getResourceAsStream("/db.properties"); //加载数据库配置文件到内存中 Properties p=new Properties(); p.load(fis); driver_class=p.getProperty("driver_class"); //获取数据库配置文件 driver_url=p.getProperty("driver_url"); database_user=p.getProperty("database_user"); database_password=p.getProperty("database_password"); Class.forName(driver_class); con=DriverManager.getConnection(driver_url,database_user,database_password); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return con; } /** * 关闭数据库 * @param con * @param pst * @param rst */ private void closeAll(Connection con,PreparedStatement pst,ResultSet rst){ if(rst!=null){ try { rst.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(pst!=null){ try { pst.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(con!=null){ try { con.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } /** * 查找 * @param sql * @param sqlValues * @return */ public Result executeQuery(){ Result result=null; ResultSet rst=null; PreparedStatement pst=null; try { pst=con.prepareStatement(sql); if(sqlValues!=null&&sqlValues.size()>0){ //当sql语句中存在占位符时 setSqlValues(pst,sqlValues); } rst=pst.executeQuery(); result=ResultSupport.toResult(rst); //一定要在关闭数据库之前完成转换 } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ this.closeAll(con, pst, rst); } return result; } /** * 增删改 * @return */ public int executeUpdate(){ int result=-1; PreparedStatement pst=null; try { pst=con.prepareStatement(sql); if(sqlValues!=null&&sqlValues.size()>0){ //当sql语句中存在占位符时 setSqlValues(pst,sqlValues); } result=pst.executeUpdate(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ this.closeAll(con, pst, null); } return result; } /** * 给sql语句中的占位符赋值 * @param pst * @param sqlValues */ private void setSqlValues(PreparedStatement pst,List sqlValues){ for(int i=0;i<sqlValues.size();i++){ try { pst.setObject(i+1,sqlValues.get(i)); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
__EOF__

本文作者:往心。
本文链接:https://www.cnblogs.com/lx06/p/15737352.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角【推荐】一下。您的鼓励是博主的最大动力!
本文链接:https://www.cnblogs.com/lx06/p/15737352.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角【推荐】一下。您的鼓励是博主的最大动力!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· AI与.NET技术实操系列(六):基于图像分类模型对图像进行分类