JDBC连接数据库实例
发表于:2025-11-07 作者:千家信息网编辑
千家信息网最后更新 2025年11月07日,package javacommon.base;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql
千家信息网最后更新 2025年11月07日JDBC连接数据库实例
package javacommon.base;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.ResultSetMetaData;import java.sql.SQLException;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;/** * 数据库操作类 * @author * */public class JDBCTemplate { private Connection conn = null; private Connection getConnection() { if(conn == null) { conn = DBManager.getConn(); } return conn; } public JDBCTemplate(Connection conn) { this.conn = conn; } public JDBCTemplate() { conn = getConnection(); } public Connection getConn() { return conn; } public void beginTranscation() throws SQLException { conn.setAutoCommit(false); } public void commit() throws SQLException { conn.commit(); } @SuppressWarnings("unchecked") public Listpackage javacommon.base;import java.sql.Connection;import java.sql.DatabaseMetaData;import java.sql.SQLException;import java.util.Properties;import javax.sql.DataSource;import org.apache.commons.dbcp.BasicDataSource;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;/** * 数据库连接池管理类 * @author * */public class DBManager { private static final Log LOG = LogFactory.getLog(DBManager.class); private static DataSource dataSource; static { Properties properties = PropertiesHandler.readPropertiesFile("/opt/hr/hr-info-sync.properites"); try { BasicDataSource basicDataSource = new BasicDataSource(); if (null != properties .getProperty("dataSource.accessToUnderlyingConnectionAllowed")) { basicDataSource .setAccessToUnderlyingConnectionAllowed(Boolean.valueOf(properties .getProperty("dataSource.accessToUnderlyingConnectionAllowed"))); } // if (null != // properties.getProperty("dataSource.connectionInitSqls")) // {basicDataSource.setConnectionInitSqls(properties.getProperty("dataSource.connectionInitSqls"));} if (null != properties .getProperty("dataSource.connectionProperties")) { basicDataSource.setConnectionProperties(properties .getProperty("dataSource.connectionProperties")); } if (null != properties.getProperty("dataSource.defaultAutoCommit")) { basicDataSource.setDefaultAutoCommit(Boolean.valueOf(properties .getProperty("dataSource.defaultAutoCommit"))); } if (null != properties.getProperty("dataSource.defaultCatalog")) { basicDataSource.setDefaultCatalog(properties .getProperty("dataSource.defaultCatalog")); } if (null != properties.getProperty("dataSource.defaultReadOnly")) { basicDataSource.setDefaultReadOnly(Boolean.valueOf(properties .getProperty("dataSource.defaultReadOnly"))); } if (null != properties .getProperty("dataSource.defaultTransactionIsolation")) { basicDataSource .setDefaultTransactionIsolation(Integer.valueOf(properties .getProperty("dataSource.defaultTransactionIsolation"))); } // if (null != // properties.getProperty("dataSource.driverClassLoader")) // {basicDataSource.setDriverClassLoader(properties.getProperty("dataSource.driverClassLoader"));} if (null != properties.getProperty("dataSource.driverClassName")) { basicDataSource.setDriverClassName(properties .getProperty("dataSource.driverClassName")); } if (null != properties.getProperty("dataSource.initialSize")) { basicDataSource.setInitialSize(Integer.valueOf(properties .getProperty("dataSource.initialSize"))); } if (null != properties.getProperty("dataSource.logAbandoned")) { basicDataSource.setLogAbandoned(Boolean.valueOf(properties .getProperty("dataSource.logAbandoned"))); } if (null != properties.getProperty("dataSource.loginTimeout")) { basicDataSource.setLoginTimeout(Integer.valueOf(properties .getProperty("dataSource.loginTimeout"))); } // if (null != properties.getProperty("dataSource.logWriter")) // {basicDataSource.setLogWriter(properties.getProperty("dataSource.logWriter"));} if (null != properties.getProperty("dataSource.maxActive")) { basicDataSource.setMaxActive(Integer.valueOf(properties .getProperty("dataSource.maxActive"))); } if (null != properties.getProperty("dataSource.maxIdle")) { basicDataSource.setMaxIdle(Integer.valueOf(properties .getProperty("dataSource.maxIdle"))); } if (null != properties .getProperty("dataSource.maxOpenPreparedStatements")) { basicDataSource .setMaxOpenPreparedStatements(Integer.valueOf(properties .getProperty("dataSource.maxOpenPreparedStatements"))); } if (null != properties.getProperty("dataSource.maxWait")) { basicDataSource.setMaxWait(Long.valueOf(properties .getProperty("dataSource.maxWait"))); } if (null != properties .getProperty("dataSource.minEvictableIdleTimeMillis")) { basicDataSource .setMinEvictableIdleTimeMillis(Long.valueOf(properties .getProperty("dataSource.minEvictableIdleTimeMillis"))); } if (null != properties.getProperty("dataSource.minIdle")) { basicDataSource.setMinIdle(Integer.valueOf(properties .getProperty("dataSource.minIdle"))); } if (null != properties .getProperty("dataSource.numTestsPerEvictionRun")) { basicDataSource .setNumTestsPerEvictionRun(Integer.valueOf(properties .getProperty("dataSource.numTestsPerEvictionRun"))); } if (null != properties.getProperty("dataSource.password")) { basicDataSource.setPassword(properties .getProperty("dataSource.password")); } if (null != properties .getProperty("dataSource.poolPreparedStatements")) { basicDataSource .setPoolPreparedStatements(Boolean.valueOf(properties .getProperty("dataSource.poolPreparedStatements"))); } if (null != properties.getProperty("dataSource.removeAbandoned")) { basicDataSource.setRemoveAbandoned(Boolean.valueOf(properties .getProperty("dataSource.removeAbandoned"))); } if (null != properties .getProperty("dataSource.removeAbandonedTimeout")) { basicDataSource .setRemoveAbandonedTimeout(Integer.valueOf(properties .getProperty("dataSource.removeAbandonedTimeout"))); } if (null != properties.getProperty("dataSource.testOnBorrow")) { basicDataSource.setTestOnBorrow(Boolean.valueOf(properties .getProperty("dataSource.testOnBorrow"))); } if (null != properties.getProperty("dataSource.testOnReturn")) { basicDataSource.setTestOnReturn(Boolean.valueOf(properties .getProperty("dataSource.testOnReturn"))); } if (null != properties.getProperty("dataSource.testWhileIdle")) { basicDataSource.setTestWhileIdle(Boolean.valueOf(properties .getProperty("dataSource.testWhileIdle"))); } if (null != properties .getProperty("dataSource.timeBetweenEvictionRunsMillis")) { basicDataSource .setTimeBetweenEvictionRunsMillis(Long.valueOf(properties .getProperty("dataSource.timeBetweenEvictionRunsMillis"))); } if (null != properties.getProperty("dataSource.url")) { basicDataSource .setUrl(properties.getProperty("dataSource.url")); } if (null != properties.getProperty("dataSource.username")) { basicDataSource.setUsername(properties .getProperty("dataSource.username")); } if (null != properties.getProperty("dataSource.validationQuery")) { basicDataSource.setValidationQuery(properties .getProperty("dataSource.validationQuery")); } if (null != properties .getProperty("dataSource.validationQueryTimeout")) { basicDataSource .setValidationQueryTimeout(Integer.valueOf(properties .getProperty("dataSource.validationQueryTimeout"))); } dataSource = basicDataSource; Connection conn = getConn(); DatabaseMetaData mdm = conn.getMetaData(); LOG.info("Connected to " + mdm.getDatabaseProductName() + " " + mdm.getDatabaseProductVersion()); if (conn != null) { conn.close(); } } catch (Exception e) { LOG.error("初始化连接池失败:" + e); } } /** * 获取链接,用完后记得关闭 * @see {@link DBManager#closeConn(Connection)} * @return */ public static final Connection getConn() { Connection conn = null; try { conn = dataSource.getConnection(); } catch (SQLException e) { LOG.error("获取数据库连接失败:" + e); } return conn; } /** * 关闭连接 * * @param conn * 需要关闭的连接 */ public static void closeConn(Connection conn) { try { if (conn != null && !conn.isClosed()) { conn.setAutoCommit(true); conn.close(); } } catch (SQLException e) { LOG.error("关闭数据库连接失败:" + e); } }}package javacommon.base;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.util.Properties;/** * Properties处理 * @author * */public class PropertiesHandler { /** * 读取资源文件 * @param filename 文件名 * @return Properties */ public static Properties readPropertiesFile(String filename) { Properties properties = new Properties(); try { InputStream inputStream = new FileInputStream(filename); properties.load(inputStream); inputStream.close(); //关闭流 } catch (IOException e) { e.printStackTrace(); } return properties; }}
数据
数据库
文件
后记
文件名
资源
链接
处理
管理
实例
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
软件开发增值税免税政策依据
网络安全法日志保留多久
新冠肺炎期间的网络安全事件
武汉旅游团软件开发
软件开发团队小组名称
天津途致臻网络技术
软件开发用华硕
以国家角度讲网络安全
网络技术公司简介
观看网络安全教育后的新的视频
果蔬分拣系统软件开发
舜宇软件开发工资
打击网络安全犯罪的通知
计算机网络技术的毕业设计题目
新都区哪里招聘网络安全工程师
borland数据库驱动
智能互联网络技术工资
软件开发过程中的几个阶段
更改数据库默认存储位置
sql数据库复制表结构
贵州国家大数据试验区网络安全
风影软件开发工作室
服务器448线程
互联网科技对社会的影响
万致服务器
智能化网络技术服务操作
麒麟系统服务器版本查看序列号
服务器性能查看
数据集成对异构数据库
2008数据库文件收缩