📄 dbutil.java
字号:
package com.test.ps.util;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.SQLException;
public class DBUtil {
static String drivername = "com.mysql.jdbc.Driver";
static String url = "jdbc:mysql://localhost:3306/phoneshop";
static String username = "root";
static String password = "1234";
static Connection conn = null;
static PreparedStatement pstmt = null;
static Statement stmt = null;
static ResultSet rs = null;
/*
* 加载数据库驱动并连接
*/
public static Connection getConnection() {
try {
Class.forName(drivername);
conn = DriverManager.getConnection(url, username, password);
return conn;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static PreparedStatement prepare(Connection conn, String sql) {
PreparedStatement pstmt = null;
try {
if (conn != null) {
pstmt = conn.prepareStatement(sql);
}
} catch (SQLException e) {
e.printStackTrace();
}
return pstmt;
}
public static Statement getStatement(Connection conn) {
Statement stmt = null;
try {
if (conn != null) {
stmt = conn.createStatement();
}
} catch (SQLException e) {
e.printStackTrace();
}
return stmt;
}
public static ResultSet getResultSet(Statement stmt, String sql) {
ResultSet rs = null;
try {
if (stmt != null) {
rs = stmt.executeQuery(sql);
}
} catch (SQLException e) {
e.printStackTrace();
}
return rs;
}
/*
* 处理插入或查询语句
*/
public static void executeUpdate(Statement stmt, String sql) {
try {
if (stmt != null) {
stmt.executeUpdate(sql);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static int executeUpdate(String sql) {
int result = 0;
try {
conn = getConnection();
stmt = conn.createStatement();
result = stmt.executeUpdate(sql);
} catch (SQLException ex) {
result = 0;
}
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}
public static ResultSet getResult(String SQL) {
Connection con = getConnection();
try {
Statement stmt = con.createStatement();
rs = stmt.executeQuery(SQL);
//stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
return rs;
}
/*
* 搜索功能 -查询数据方法
*/
public static ResultSet executeQuery(String sql)
{
Connection conn= DBUtil.getConnection();
try {
Statement stmt =conn.createStatement();
rs = stmt.executeQuery(sql);
//rs.last();
} catch (Exception e) {
e.printStackTrace();
}
return rs;
}
/*
* 关闭数据库连接
*/
public static void close() {
try {
if (rs != null)
rs.close();
} catch (Exception e) {
e.printStackTrace(System.err);
}
try {
if (stmt != null)
stmt.close();
} catch (Exception e) {
e.printStackTrace(System.err);
}
try {
if (conn != null)
conn.close();
} catch (Exception e) {
e.printStackTrace(System.err);
}
}
/*
* 关闭结果集
*/
public static void closeConnection(Connection conn) {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (pstmt != null) {
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
/*
* 关闭结果集
*/
public static void CloseResultSet(ResultSet rs) {
try {
if (rs != null)
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void close(Connection conn) {
try {
if (conn != null) {
conn.close();
conn = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void close(Statement stmt) {
try {
if (stmt != null) {
stmt.close();
stmt = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
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 + -