mirror of
https://gitee.com/dromara/MaxKey.git
synced 2026-05-16 05:20:42 +08:00
synchronizer-jdbc
This commit is contained in:
111
maxkey-common/src/main/java/org/maxkey/util/JdbcUtils.java
Normal file
111
maxkey-common/src/main/java/org/maxkey/util/JdbcUtils.java
Normal file
@@ -0,0 +1,111 @@
|
||||
/*
|
||||
* Copyright [2022] [MaxKey of copyright http://www.maxkey.top]
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
|
||||
package org.maxkey.util;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
|
||||
public class JdbcUtils {
|
||||
|
||||
public static Connection connect(String url, String user, String pwd, String driverClass) {
|
||||
Connection conn = null;
|
||||
try {
|
||||
Class.forName(driverClass);
|
||||
conn = java.sql.DriverManager.getConnection(url, user, pwd);
|
||||
return conn;
|
||||
} catch (ClassNotFoundException e) {
|
||||
System.out.println("ClassNotFoundException");
|
||||
return null;
|
||||
} catch (SQLException e) {
|
||||
System.out.println("SQLException");
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void release(Connection conn) {
|
||||
if (conn != null) {
|
||||
try {
|
||||
conn.close();
|
||||
} catch (SQLException e) {
|
||||
System.out.println("SQLException");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void release(Connection conn, Statement stmt, ResultSet rs) {
|
||||
if (rs != null)
|
||||
try {
|
||||
rs.close();
|
||||
rs = null;
|
||||
} catch (SQLException e) {
|
||||
System.out.println("SQLException");
|
||||
}
|
||||
if (stmt != null)
|
||||
try {
|
||||
stmt.close();
|
||||
stmt = null;
|
||||
} catch (SQLException e) {
|
||||
System.out.println("SQLException");
|
||||
}
|
||||
if (conn != null) {
|
||||
try {
|
||||
conn.close();
|
||||
conn = null;
|
||||
} catch (SQLException e) {
|
||||
System.out.println("SQLException");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void release(Connection conn, Statement stmt, PreparedStatement pstmt, ResultSet rs) {
|
||||
if (rs != null)
|
||||
try {
|
||||
rs.close();
|
||||
rs = null;
|
||||
} catch (SQLException e) {
|
||||
System.out.println("ResultSet Close Exception");
|
||||
}
|
||||
if (stmt != null)
|
||||
try {
|
||||
stmt.close();
|
||||
stmt = null;
|
||||
} catch (SQLException e) {
|
||||
System.out.println("Statement Close Exception");
|
||||
}
|
||||
if (pstmt != null)
|
||||
try {
|
||||
pstmt.close();
|
||||
pstmt = null;
|
||||
} catch (SQLException e) {
|
||||
System.out.println("PreparedStatement Close Exception");
|
||||
}
|
||||
if (conn != null) {
|
||||
try {
|
||||
conn.close();
|
||||
conn = null;
|
||||
} catch (SQLException e) {
|
||||
System.out.println("Connection Close Exception");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user