📄 db.java
字号:
package com.beifengkd.drp.util;
import java.sql.*;
public class DB {
//连接数据库
public static Connection getConn() {
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/drp?user=root&password=root");
} catch(ClassNotFoundException e) {
e.printStackTrace();
} catch(SQLException e) {
e.printStackTrace();
}
return conn;
}
//Statement语句对象
public static Statement createStatement(Connection conn) {
Statement stmt = null;
try {
stmt = conn.createStatement();
} catch(SQLException e) {
e.printStackTrace();
}
return stmt;
}
//创建PreparedStatement语句对象
public static PreparedStatement createPreparedStatement(Connection conn, String sql) {
PreparedStatement pstmt = null;
try {
pstmt = conn.prepareStatement(sql);
} catch(SQLException e) {
e.printStackTrace();
}
return pstmt;
}
//重写PreparedStatement语句对象autoGeneratedKeys
public static PreparedStatement createPreparedStatement(Connection conn, String sql, int autoGeneratedKeys) {
PreparedStatement pstmt = null;
try {
pstmt = conn.prepareStatement(sql, autoGeneratedKeys);
} catch(SQLException e) {
e.printStackTrace();
}
return pstmt;
}
//创建结果集
public static ResultSet executeQuery(Statement stmt, String sql) {
ResultSet rs = null;
try {
rs = stmt.executeQuery(sql);
} catch(SQLException e) {
e.printStackTrace();
}
return rs;
}
//关闭连接
public static void close(Connection conn) {
try {
if(conn != null) {
conn.close();
conn = null;
}
} catch(SQLException e) {
e.printStackTrace();
}
}
//关闭Statement
public static void close(Statement stmt) {
try {
if(stmt != null) {
stmt.close();
stmt = null;
}
} catch(SQLException e) {
e.printStackTrace();
}
}
//关闭ResultSet
public static void close(ResultSet rs) {
try {
if(rs != null) {
rs.close();
rs = null;
}
} catch(SQLException e) {
e.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -