1.新增构造方法可以通过dataSource获取连接

2.通过抽象配置文件接口获取jdbc参数,为以后扩展方便
master
星期八 5 years ago
parent 0daf439758
commit fc01760722
  1. 44
      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.PropsUtil;
import org.panda.code.uitl.StringUtil; import org.panda.code.uitl.StringUtil;
import org.panda.jdbc.helper.DbConfigInterface;
import org.panda.jdbc.mode.Column; import org.panda.jdbc.mode.Column;
import org.panda.jdbc.mode.DbInfo; import org.panda.jdbc.mode.DbInfo;
import org.panda.jdbc.mode.Table; import org.panda.jdbc.mode.Table;
import javax.sql.DataSource;
import java.sql.*; import java.sql.*;
import java.util.List; import java.util.List;
import java.util.Properties; import java.util.Properties;
@ -28,18 +30,33 @@ public class JdbcUtils {
private String password = null; private String password = null;
private DataSource dataSource;
public JdbcUtils(String driver, String url, String username, String password) { public JdbcUtils(String driver, String url, String username, String password) {
this.driver = driver; this.driver = driver;
this.url = url; this.url = url;
this.username = username; this.username = username;
this.password = password; 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 { try {
Class.forName(driver); Class.forName(this.driver);
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
@Deprecated
public JdbcUtils(Properties prop) { public JdbcUtils(Properties prop) {
this.driver = PropsUtil.getString(prop, "jdbc.driver"); this.driver = PropsUtil.getString(prop, "jdbc.driver");
this.url = PropsUtil.getString(prop, "jdbc.url"); 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 * @throws SQLException
*/ */
public Connection getConnection() { public Connection getConnection() {
Connection conn = CONNECTION_HOLDER.get(); Connection conn = null;
if (conn == null) { if (dataSource!=null){
try { try {
conn = DriverManager.getConnection(url, username, password); conn = dataSource.getConnection();
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); 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; return conn;
} }
@ -140,7 +169,6 @@ public class JdbcUtils {
* @param params * @param params
*/ */
public void update(String sql, Object[] params) { public void update(String sql, Object[] params) {
System.out.println(sql);
Connection conn = null; Connection conn = null;
PreparedStatement st = null; PreparedStatement st = null;
ResultSet rs = null; ResultSet rs = null;

Loading…
Cancel
Save