Flask接口签名sign原理与实例代码浅析
244
2023-08-03
java连接Mysql数据库的工具类
一个封装好的链接mysql数据库的工具类,可以方便的获取Connection对象关闭Statement、ResultSet、Statment对象等等
复制代码 代码如下:
package myUtil;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
* 链接mysql数据库
* @author weichk
*/
public class MysqlDbManager {
private static final String URL = "jdbc:mysql://127.0.0.1:3306/openfire";
private static final String USER = "root";
private static final String PASSWORD = "123456";
static {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
System.out.println("加载Mysql数据库驱动失败!");
}
}
/**
* 获取Connection
*
* @return
* @throws SQLException
* @throws ClassNotFoundException
*/
public static Connection getConnection() throws SQLException {
Connection conn = null;
try {
conn = DriverManager.getConnection(URL, USER, PASSWORD);
} catch (SQLException e) {
System.out.println("获取数据库连接失败!");
throw e;
}
return conn;
}
/**
lbCOFT * 关闭ResultSet
* @param rs
*/
public static void closeResultSet(ResultSet rs) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
}
/**
* 关闭Statement
* @param stmt
*/
public static void closeStatement(Statement stmt) {
if (stmt != null) {
try {
stmt.close();
}
catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
/**
* 关闭ResultSet、Statement
* @param rs
* @param stmt
*/
public static void closeStatement(ResultSet rs, Statement stmt) {
closeResultSet(rs);
closeStatement(stmt);
}
/**
* 关闭PreparedStatement
* @param pstmt
* @throws SQLException
*/
public static void fastcloseStmt(PreparedStatement pstmt) throws SQLException
{
pstmt.close();
}
/**
* 关闭ResultSet、PreparedStatement
* @param rs
* @param pstmt
* @throws SQLException
*/
public static void fastcloseStmt(ResultSet rs, PreparedStatement pstmt) throws SQLException
{
rs.close();
pstmt.close();
}
/**
* 关闭ResultSet、Statement、Connection
* @param rs
* @param stmt
* @param con
*/
public static void closeConnection(ResultSet rs, Statement stmt, Connection con) {
closeResultSet(rs);
closeStatement(stmt);
closeConnection(con);
}
/**
* 关闭Statement、Connection
* @param stmt
* @param con
*/
public static void closeConnection(Statement stmt, Connectionhttp:// con) {
closeStatement(stmt);
closeConnection(con);
}
/**
* 关闭Connection
* @param con
*/
public static void closeConnection(Connection con) {
if (con != null) {
try {
con.close();
}
catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
}
以上就是本文的全部内容了,希望对大家熟练掌握java能有所帮助。
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~