| package com.xcgdt.template; |
| |
| import java.sql.Connection; |
| import java.sql.DriverManager; |
| import java.sql.PreparedStatement; |
| import java.sql.ResultSet; |
| import java.sql.SQLException; |
| import java.sql.Statement; |
| |
| import com.mysql.jdbc.Driver; |
| |
| public class BaseDao { |
| private static String drive_class = "com.mysql.jdbc.Driver"; |
| private static String url = "jdbc:mysql://localhost:3306/train?useUnicode=true&characterEncoding=utf-8"; |
| private static String user = "root"; |
| private static String password = "shell_girl"; |
| |
| |
| static { |
| try { |
| Class.forName(drive_class); |
| } catch (ClassNotFoundException e) { |
| e.printStackTrace(); |
| } |
| |
| } |
| |
| |
| |
| |
| |
| |
| |
| public Connection getConnection() { |
| try { |
| Connection conn = DriverManager.getConnection(url, user, password); |
| return conn; |
| } catch (SQLException e) { |
| e.printStackTrace(); |
| } |
| return null; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public ResultSet executeQuery(String sql, Object... pramas) { |
| Connection conn = this.getConnection(); |
| PreparedStatement stmt = null; |
| try { |
| stmt = conn.prepareStatement(sql); |
| if (pramas != null && pramas.length != 0) { |
| for (int i = 0; i < pramas.length; i++) { |
| stmt.setObject(i + 1, pramas[i]); |
| } |
| } |
| ResultSet rs = stmt.executeQuery(); |
| return rs; |
| } catch (SQLException e) { |
| e.printStackTrace(); |
| } |
| return null; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public int executeUpdate(String sql, Object... pramas) { |
| Connection conn = this.getConnection(); |
| PreparedStatement pstmt = null; |
| try { |
| int result = 0; |
| pstmt = conn.prepareStatement(sql); |
| if (pramas != null && pramas.length != 0) { |
| for (int i = 0; i < pramas.length; i++) { |
| pstmt.setObject(i + 1, pramas[i]); |
| } |
| } |
| result = pstmt.executeUpdate(); |
| return result; |
| } catch (SQLException e) { |
| e.printStackTrace(); |
| } finally { |
| this.closeAll(pstmt, null, conn); |
| } |
| return 0; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public void closeAll(Statement stmt, ResultSet rs, Connection conn) { |
| try { |
| if (stmt != null) { |
| stmt.close(); |
| stmt = null; |
| } |
| if (rs != null) { |
| rs.close(); |
| rs = null; |
| } |
| if (conn != null && conn.isClosed() == false) { |
| conn.close(); |
| conn = null; |
| } |
| } catch (SQLException e) { |
| e.printStackTrace(); |
| } |
| } |
| } |
| |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~