From fc0176072246e8d75e223763274e005b222ad12a Mon Sep 17 00:00:00 2001 From: qi_liang Date: Wed, 14 Aug 2019 21:36:21 +0800 Subject: [PATCH] =?UTF-8?q?1.=E6=96=B0=E5=A2=9E=E6=9E=84=E9=80=A0=E6=96=B9?= =?UTF-8?q?=E6=B3=95=E5=8F=AF=E4=BB=A5=E9=80=9A=E8=BF=87dataSource?= =?UTF-8?q?=E8=8E=B7=E5=8F=96=E8=BF=9E=E6=8E=A5=202.=E9=80=9A=E8=BF=87?= =?UTF-8?q?=E6=8A=BD=E8=B1=A1=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E8=8E=B7=E5=8F=96jdbc=E5=8F=82=E6=95=B0=EF=BC=8C?= =?UTF-8?q?=E4=B8=BA=E4=BB=A5=E5=90=8E=E6=89=A9=E5=B1=95=E6=96=B9=E4=BE=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/org/panda/jdbc/JdbcUtils.java | 44 +++++++++++++++---- 1 file changed, 36 insertions(+), 8 deletions(-) diff --git a/panda-jdbc/src/main/java/org/panda/jdbc/JdbcUtils.java b/panda-jdbc/src/main/java/org/panda/jdbc/JdbcUtils.java index fc5660a..b7957d3 100644 --- a/panda-jdbc/src/main/java/org/panda/jdbc/JdbcUtils.java +++ b/panda-jdbc/src/main/java/org/panda/jdbc/JdbcUtils.java @@ -2,10 +2,12 @@ package org.panda.jdbc; import org.panda.code.uitl.PropsUtil; import org.panda.code.uitl.StringUtil; +import org.panda.jdbc.helper.DbConfigInterface; import org.panda.jdbc.mode.Column; import org.panda.jdbc.mode.DbInfo; import org.panda.jdbc.mode.Table; +import javax.sql.DataSource; import java.sql.*; import java.util.List; import java.util.Properties; @@ -28,18 +30,33 @@ public class JdbcUtils { private String password = null; + private DataSource dataSource; + public JdbcUtils(String driver, String url, String username, String password) { this.driver = driver; this.url = url; this.username = username; this.password = password; + loadDriver(); + } + + public JdbcUtils(DbConfigInterface dbConfigInterface){ + this.driver = dbConfigInterface.getJdbcDriver(); + this.url = dbConfigInterface.getJdbcUrl(); + this.username =dbConfigInterface.getJdbcUser(); + this.password =dbConfigInterface.getJdbcPassword(); + loadDriver(); + } + + private void loadDriver(){ try { - Class.forName(driver); + Class.forName(this.driver); } catch (ClassNotFoundException e) { e.printStackTrace(); } } + @Deprecated public JdbcUtils(Properties prop) { this.driver = PropsUtil.getString(prop, "jdbc.driver"); this.url = PropsUtil.getString(prop, "jdbc.url"); @@ -52,6 +69,10 @@ public class JdbcUtils { } } + public JdbcUtils(DataSource dataSource){ + this.dataSource = dataSource; + } + /** * 连接数据库 * @@ -59,17 +80,25 @@ public class JdbcUtils { * @throws SQLException */ public Connection getConnection() { - Connection conn = CONNECTION_HOLDER.get(); - if (conn == null) { + Connection conn = null; + if (dataSource!=null){ try { - conn = DriverManager.getConnection(url, username, password); + conn = dataSource.getConnection(); } catch (SQLException e) { e.printStackTrace(); - } finally { - CONNECTION_HOLDER.set(conn); + } + }else { + conn = CONNECTION_HOLDER.get(); + if (conn == null) { + try { + conn = DriverManager.getConnection(url, username, password); + } catch (SQLException e) { + e.printStackTrace(); + } finally { + CONNECTION_HOLDER.set(conn); + } } } - return conn; } @@ -140,7 +169,6 @@ public class JdbcUtils { * @param params */ public void update(String sql, Object[] params) { - System.out.println(sql); Connection conn = null; PreparedStatement st = null; ResultSet rs = null;