当前位置:网站首页>web---jdbc工具类编写
web---jdbc工具类编写
2022-07-20 05:33:00 【菜鸟小白-晓毅】
JDBCUtils工具包
package com.jdbc.utils;
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class JDBCUtils {
private static String url;
private static String user;
private static String password;
private static String driver;
static {
//创建Properties对象
Properties properties = new Properties();
//通过src路径下的文件获取
ClassLoader classLoader = JDBCUtils.class.getClassLoader();
URL res = classLoader.getResource("jdbc.properties");
String path = res.getPath();
try {
System.out.println(path);
properties.load(new FileReader(path));
//获取赋值
url = properties.getProperty("url");
user = properties.getProperty("user");
password = properties.getProperty("password");
driver=properties.getProperty("driver");
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
}
/** * 获取连接对象 * * @return */
public static Connection getConnection() throws SQLException {
//不想传递参数就配置文件
// String url = "jdbc:mysql://localhost:3306/monkey";
// String username = "root";
// String password = "123456789";
return DriverManager.getConnection(url,user,password);
}
/** * 释放资源 * * @param stat * @param conn */
public static void close(Statement stat, Connection conn) {
if (stat != null) {
try {
stat.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
jdbc.properties配置文件
url=jdbc:mysql://localhost:3306/monkey
user=root
password=123456789
driver=com.mysql.cj.jdbc.Driver
应用工具类
package com.jdbc;
import com.jdbc.utils.JDBCUtils;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JdbcDemo02 {
public static void main(String[] args) {
Connection conn = null;
Statement stat = null;
ResultSet resultSet = null;
try {
conn = JDBCUtils.getConnection();
String sql = "update lmonkey_user set USER_PASSWORD = '123456789' where USER_ID=5555";
String sql1 = "select * from lmonkey_user";
stat = conn.createStatement();
resultSet = stat.executeQuery(sql1);
while (resultSet.next()) {
String id = resultSet.getString("USER_ID");
String name = resultSet.getString("USER_NAME");
System.out.println(id + "\t" + name);
}
int i = stat.executeUpdate(sql);
if (i > 0) {
System.out.println("添加成功");
} else {
System.out.println("添加失败");
}
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
JDBCUtils.close(stat, conn);
}
}
}
边栏推荐
- Nslog output Chinese
- Tools - idea nice style font / size/
- 基于昇腾AI异构计算架构CANN的通用目标检测与识别一站式方案初体验
- 广播影视节目制作毕业论文范文
- RIoTBoard开发板系列笔记(六)—— buildroot构建系统镜像
- GD32利用CubeMX构建代码的测试
- 中文自然语言处理相关资料 | Chinese NLP Toolkits 中文NLP工具
- uni-app打包后不发请求的原因
- Tensorflow 1. The difference between conv2d padding in X and pytoch
- (7) Pytorch deep learning: full connection layer network
猜你喜欢
随机推荐
Analyze the relationship between iteration, epoch and batchsize
TensorFlow v1 入门教程
Pyqt5 uses custom tooltips to solve the problem of incomplete display of qtablewidget data
城市规划设计毕业论文范文
DC-1-实践
Detailed explanation of memory alignment rules and calculation formulas in C language
基于昇腾AI异构计算架构CANN的通用目标检测与识别一站式方案初体验
电力系统及其自动化毕业论文题目【精选】
[cann training camp] AI CPU operator development based on shengteng cann platform
MySQL master-slave replication docker cluster construction
基于STM32CubeMX的片外SPIFLASH嵌入FATFS
Play with the one-stop plan of cann target detection and recognition [introduction]
Tensorflow 1.x 和 Pytorch 中 Conv2d Padding的区别
STM32CubeMonitor的使用第二部分-历史数据存储以及网络访问
运算放大器的简要理解
bgy开发小示例
(6) Pytorch deep learning: loading datasets
pytorch实现手写数字识别 | MNIST数据集(全连接神经网络)
qlineargradient中坐标的含义
The difference between struct and class in swift