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;