当前位置:网站首页>简单的数据库连接示例
简单的数据库连接示例
2022-06-12 03:10:00 【会飞的小恐龙】
# 简单数据库工具
public class SimpleDBDemo {
public static void main(String[] args) {
try {
// 加载数据库驱动 com.mysql.cj.jdbc.Driver
String strDriver = "com.mysql.cj.jdbc.Driver";
// 获取mysql连接地址
String strURL = "jdbc:mysql://localhost:3306/mytest?&useSSL=false&serverTimezone=UTC";
// 用户名称
String username = "root";
// 用户密码
String password = "123456";
Class.forName(strDriver);
System.out.println("SQLServerDriver success");
// 获取一个数据的连接,连接MySql数据库
Connection con = DriverManager.getConnection(strURL, username, password);
System.out.println("Connection success");
// 创建statement类对象,用来执行SQL语句!
Statement stm = con.createStatement();
// 要执行的SQL语句
String strSql = "select * from student";
// ResultSet类,用来存放获取的结果集!
ResultSet rs = stm.executeQuery(strSql);
while(rs.next()){
String s1 = rs.getString(1);
String s2 = rs.getString(2);
double d3 = rs.getDouble(3);
System.out.println(s1+"\t"+s2+"\t\t"+d3);
}
rs.close();
stm.close();
con.close();
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
}
}
通用数据库工具
public class JDBCUtil {
private static final String driver;
private static final String url;
private static final String userName;
private static final String password;
static {
InputStream is = JDBCUtil.class.getClassLoader().getResourceAsStream("db.properties");
Properties properties = new Properties();
try {
properties.load(is);
} catch (IOException e) {
e.printStackTrace();
}
driver = properties.getProperty("driver");
url = properties.getProperty("url");
userName = properties.getProperty("username");
password = properties.getProperty("password");
}
// 获取数据库连接
public static Connection getConnection() {
Connection con = null;
try {
Class.forName(driver);
con = DriverManager.getConnection(url, userName, password);
} catch (SQLException | ClassNotFoundException e) {
e.printStackTrace();
}
return con;
}
// 数据库查询,返回结果集
public static ResultSet query(Connection con, PreparedStatement st, ResultSet rs, String sql
, Object[] params) throws SQLException {
st = con.prepareStatement(sql, ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY);
if (params != null) {
for (int i = 0; i < params.length; i++) {
st.setObject(i + 1, params[i]);
}
}
rs = st.executeQuery();
return rs;
}
// 数据库增删改
public static int update(Connection con, String sql
, Object[] params, ResultSet rs, PreparedStatement st) throws SQLException {
st = con.prepareStatement(sql);
for (int i = 0; i < params.length; i++) {
st.setObject(i + 1, params[i]);
}
return st.executeUpdate();
}
// 关闭数据库连接
public static void release(Connection con, Statement st, ResultSet rs) {
boolean flag = true;
if (rs != null) {
try {
rs.close();
rs = null;
} catch (SQLException e) {
e.printStackTrace();
flag = false;
}
}
if (st != null) {
try {
st.close();
st = null;
} catch (SQLException e) {
e.printStackTrace();
flag = false;
}
}
if (con != null) {
try {
con.close();
con = null;
} catch (SQLException e) {
e.printStackTrace();
flag = false;
}
}
}
}
db.properties
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/XXX?&useSSL=false&serverTimezone=UTC
username=root
password=123456
边栏推荐
- [string] judge whether S2 is the rotation string of S1
- Leetcode 6[finding rules] Z-transform the leetcode path of heroding
- WPS table learning notes - highlight duplicate values
- central limit theorem
- [Business Research Report] the salary growth rate report of each industry in 2022 includes regional growth rate - download link is attached
- 分数大小的比较
- AcrelCloud-6000安全用电云平台在某商业广场的应用
- 安科瑞抗晃电产品在河北某化工项目的应用
- Demand and business model innovation - demand 11 - overview of demand analysis
- I2C protocol overview
猜你喜欢
![Leetcode 6[finding rules] Z-transform the leetcode path of heroding](/img/c0/aca2eb185ce4b423df9ee171ee91e1.jpg)
Leetcode 6[finding rules] Z-transform the leetcode path of heroding

分数大小的比较

架构入门讲解 - 谁动了我的蛋糕

About 100 to realize the query table? Really? Let's experience the charm of amiya.

errno: -4091, syscall: ‘listen‘, address: ‘::‘, port: 8000

Comparaison de la taille des fractions

微信小程序項目實例——體質計算器

Application of residual pressure monitoring system in high-rise civil buildings

Requirements and business model innovation - Requirements 12 - process oriented modeling

The road of global evolution of vivo global mall -- multilingual solution
随机推荐
For the first time, why not choose "pure medium platform" for byte beating data platform
跨域有哪些解决方法?
Selection (046) - what is the output of the following code?
Selection (043) - which of the following values are false?
cupp字典生成工具(同类工具还有crunch)
Unity3d ugui translucent or linear gradient pictures display abnormally (blurred) problem solving (color space mismatch)
Restful interface design specification [for reference only]
Intel case
Getting started with RPC
errno: -4091, syscall: ‘listen‘, address: ‘::‘, port: 8000
$LastExitCode=0, but $?= False in PowerShell. Redirecting stderr to stdout gives NativeCommandError
The unique path of leetcode topic resolution II
errno: -4091, syscall: ‘listen‘, address: ‘::‘, port: 8000
Kubernetes affinity learning notes
What are the solutions across domains?
Function templatesfunction templates
Redis gets the set of keys prefixed with XXX
[point cloud compression] variable image compression with a scale hyperprior
[Business Research Report] the salary growth rate report of each industry in 2022 includes regional growth rate - download link is attached
[digital signal processing] correlation function (power signal | cross correlation function of power signal | autocorrelation function of power signal)