当前位置:网站首页>Encapsulating JDBC tool classes
Encapsulating JDBC tool classes
2022-06-30 04:31:00 【A fat man】
Tool class
package com.htf.util;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
/**
* JDBC Data operation tool class
* @author : htf
*
*/
public class DBUtils {
private static String url;
private static String driverClassName;
private static String userName;
private static String password;
static {
// Utility class loader load properties file
// The configuration file must be placed in src Under the path
// Read the file
InputStream is =DBUtils.class.getClassLoader().getResourceAsStream("jdbc_dev.properties");
Properties prop =new Properties();
try {
prop.load(is);
url=prop.getProperty("url");
userName=prop.getProperty("userName");
password=prop.getProperty("password");
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Get database connection
* @return
*/
public static Connection getConnection() {
Connection conn =null;
try {
//
conn=DriverManager.getConnection(url, userName, password);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
/**
* close JDBC resources , Order of attention
* @param conn
* @param stmt
* @param rs
*/
public static void close(Connection conn,Statement stmt,ResultSet rs) {
try {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
The configuration file
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai
userName=root
password=123456
边栏推荐
- 找到接口在表单里加参数
- Myrpc version 0
- Named pipes for interprocess communication
- 网络层详解
- JS file block to Base64 text
- MySQL updates JSON string in array form
- Introduction to system programming
- Memorize unfamiliar words at SSM stage and update them from time to time
- Unity échappe à l'entrée de caractères lors de l'entrée de chaînes dans l'éditeur
- FortiGate configures multiple server IPS as link monitor monitoring objects on the same interface
猜你喜欢

An error occurs when sqlyog imports the database. Please help solve it!

基于servlet+jsp+mysql实现的工资管理系统【源码+数据库】

I spent three years in a big factory outsourcing, which subverted my understanding!

Myrpc version 1

Blocking queue example

A solution to the problem of "couldn't open file /mnt/repodata/repomd.xml"

File system and directory operations

Day 11 script and game AI

基于ROS的SLAM建图、自动导航、避障(冰达机器人)

el-upload上传文件(手动上传,自动上传,上传进度)
随机推荐
Differences between beanfactory and factorybean
7-3 single source shortest circuit for strange play upgrade
My idea configuration
Code cloud fatal: authentication failed for
Interprocess communication
基于ROS的SLAM建图、自动导航、避障(冰达机器人)
base64.c
Unity when entering a string in the editor, escape the input of characters
An error occurs when sqlyog imports the database. Please help solve it!
IO stream, character read / write, copy
Myrpc version 6
Blue Bridge Cup: magic cube rotation [Vocational group]
FortiGate firewall and Aruze cloud tunnel interruption
基于海康EhomeDemo工具排查公网部署出现的视频播放异常问题
The same node code will cause duplicate data
Default value of JS parameter
Tea mall system based on SSM framework [project source code + database script + report]
Error encountered in SQL statement, solve
BeanFactory创建流程
Es2019 key summary