summaryrefslogtreecommitdiff
path: root/src/main/java/com/mesasoft/cn/util/MariaDBUtils.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/com/mesasoft/cn/util/MariaDBUtils.java')
-rw-r--r--src/main/java/com/mesasoft/cn/util/MariaDBUtils.java80
1 files changed, 80 insertions, 0 deletions
diff --git a/src/main/java/com/mesasoft/cn/util/MariaDBUtils.java b/src/main/java/com/mesasoft/cn/util/MariaDBUtils.java
new file mode 100644
index 0000000..a86b363
--- /dev/null
+++ b/src/main/java/com/mesasoft/cn/util/MariaDBUtils.java
@@ -0,0 +1,80 @@
+package com.mesasoft.cn.util;
+
+import com.alibaba.druid.pool.DruidDataSourceFactory;
+
+import javax.sql.DataSource;
+import java.sql.Connection;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.Properties;
+
+/**
+ * Druid连接池的工具类
+ */
+public class MariaDBUtils {
+ private static DataSource ds ;
+
+ static{
+ try {
+ Properties pro = new Properties();
+// pro.load(JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties"));
+ ds = DruidDataSourceFactory.createDataSource(pro);
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+
+ /**
+ * 获取连接
+ */
+ public static Connection getConnection() throws SQLException {
+ return ds.getConnection();
+ }
+
+ /**
+ * 释放资源
+ */
+ public static void close(Statement stmt,Connection conn){
+
+ close(null,stmt,conn);
+ }
+
+
+ public static void close(ResultSet rs , Statement stmt, Connection conn){
+
+
+ if(rs != null){
+ try {
+ rs.close();
+ } catch (SQLException e) {
+ e.printStackTrace();
+ }
+ }
+
+
+ if(stmt != null){
+ try {
+ stmt.close();
+ } catch (SQLException e) {
+ e.printStackTrace();
+ }
+ }
+
+ if(conn != null){
+ try {
+ conn.close();
+ } catch (SQLException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+
+ /**
+ * 获取连接池方法
+ */
+ public static DataSource getDataSource(){
+ return ds;
+ }
+
+} \ No newline at end of file