当前位置:网站首页>简单的数据库连接示例
简单的数据库连接示例
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;
}
}
}
}
边栏推荐
- Demand and business model innovation - demand 10- observation and document review
- Redis gets the set of keys prefixed with XXX
- 微信小程序项目实例——我有一支画笔(画画)
- The four pain points of enterprise digitalization are solved by low code platform
- Intel Galileo Gen2 development
- 如何防止商场电气火灾的发生?
- 跨域有哪些解决方法?
- 微信小程序項目實例——體質計算器
- laravel 8 选用 jwt 进行接口验证
- [string] judge whether S2 is the rotation string of S1
猜你喜欢

Demand and business model innovation - demand 6- stakeholder analysis and hard sampling

Requirements and business model innovation - Requirements 7- user requirements acquisition based on use case / scenario model

$LastExitCode=0, but $?= False in PowerShell. Redirecting stderr to stdout gives NativeCommandError

Comparison of scores

Application of ard3m motor protector in coal industry

Application of acrelcloud-6000 secure power cloud platform in a commercial plaza

[DFS "want" or "don't"] seek subsets; Seeking combination

C language array

Application of ankery anti shake electric products in a chemical project in Hebei

How to prevent electrical fire in shopping malls?
随机推荐
[Business Research Report] 2021 global mobile game player white paper - download link attached
alertmanager告警配置
errno: -4078, code: ‘ECONNREFUSED‘, syscall: ‘connect‘, address: ‘127.0.0.1‘, port: 3306;postman报错
推荐6款办公软件,好用还免费,效率翻倍
errno: -4091, syscall: ‘listen‘, address: ‘::‘, port: 8000
2020-12-06
Go 语法 变量
Application of residual pressure monitoring system in high-rise civil buildings
TCP three handshakes and four waves
Sparse tensor based point cloud attribute compression
Unity3D中DrawCall、Batches、SetPassCall
ssh公钥登录失败报错:sign_and_send_pubkey: no mutual signature supported
AcrelCloud-6000安全用电云平台在某商业广场的应用
xml
Unscrambling 2021 of service grid: bid farewell to the "great leap forward" of architecture, and a hundred schools of thought contend for the technological ecology
Selection (045) - what is the output of the following code?
Special materials | household appliances, white electricity, kitchen electricity
What is the commonly heard sub table of MySQL? Why does MySQL need tables?
JSON and XML pros and cons
Min25 sieve