JdbcTemplate获取数据库连接工具类, 目前只写了Oracle,MySQL,SqlServer,DB2和Sybase。
package comm.util;
import org.apache.commons.dbcp.BasicDataSource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.util.StringUtils;
public class DBUtil {
/**
* 取得数据库连接JT
* @param ip
* @param port
* @param sid
* @param user
* @param pwd
* @param sjklx
* @param ljcs
* @return
*/
public static JdbcTemplate getJdbcTemplate(String ip, String port, String sid, String user, String pwd, String sjklx, String ljcs, String service_name) {
String driverName = "";
if ("ORACLE".equals(sjklx.toUpperCase())) {//oracle
driverName = "oracle.jdbc.driver.OracleDriver";
} else if ("MYSQL".equals(sjklx.toUpperCase())) {//mysql
driverName = "com.mysql.jdbc.Driver";
} else if ("SQLSERVER".equals(sjklx.toUpperCase())) {//sql server
driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
}else if ("DB2".equals(sjklx.toUpperCase())) {//DB2
driverName = "com.ibm.db2.jcc.DB2Driver";
}else if ("SYBASE".equals(sjklx.toUpperCase())) {//Sybase
driverName = "com.sybase.jdbc3.jdbc.SybDriver";
} else {
throw new RuntimeException("数据源类型传入不正确,你传入的值为:" + sjklx);
}
BasicDataSource bds = new BasicDataSource();
bds.setDriverClassName(driverName);
String url = getJdbcUrl(sjklx, ip, sid, port,ljcs, service_name);
bds.setUrl(url);
bds.setUsername(user);
bds.setPassword(pwd);
JdbcTemplate jdbcTemplate = new JdbcTemplate();
jdbcTemplate.setDataSource(bds);
return jdbcTemplate;
}
/**
* 获取数据库连接URL
* @param sjklx
* @param ip
* @param sid
* @param port
* @param ljcs
* @param service_name
* @return
*/
public static String getJdbcUrl( String sjklx, String ip, String sid, String port, String ljcs, String service_name) {
String jdbcUrl = "";
sjklx= StringUtils.trimWhitespace(sjklx);
ip = StringUtils.trimWhitespace(ip);
sid = StringUtils.trimWhitespace(sid);
port= StringUtils.trimWhitespace(port);
service_name= StringUtils.trimWhitespace(service_name);
ljcs = StringUtils.trimWhitespace(ljcs);
if ("ORACLE".equals(sjklx.toUpperCase()) ) {//Oracle
if (StringUtils.hasText(sid)) {
jdbcUrl = "jdbc:oracle:thin:@" + ip
+ ":" + port+ ":" + sid;
} else if (StringUtils.hasText(service_name)) {
jdbcUrl = "jdbc:oracle:thin:@//" + ip+ ":" + port+ "/" + service_name;
} else {
jdbcUrl = "jdbc:oracle:thin:@" + ip
+ ":" + port+ ":" + sid;
}
} else if ("MYSQL".equals(sjklx.toUpperCase())) {//Mysql
jdbcUrl = "jdbc:mysql://" + ip;
if (StringUtils.hasText(port)) {
jdbcUrl += ":" + port;
}
jdbcUrl += "/" + sid;
if (StringUtils.hasText(ljcs) && !"null".equals(ljcs)) {
if ('?' == ljcs.charAt(0)) {
jdbcUrl += ljcs;
} else {
jdbcUrl += "?" + ljcs;
}
}
} else if ("SQLSERVER".equals(sjklx.toUpperCase())) {//Sql Server
jdbcUrl = "jdbc:sqlserver://" + ip + ":" + port + ";DatabaseName=" + sid;
if (StringUtils.hasText(ljcs) && !"null".equals(ljcs)) {
if (';' == ljcs.charAt(0)) {
jdbcUrl += ljcs;
} else {
jdbcUrl += ";" + ljcs;
}
}
} else if ("DB2".equals(sjklx.toUpperCase())) {//DB2
jdbcUrl = "jdbc:db2://" + ip + ":" + port + "/" + sid;
if (StringUtils.hasText(ljcs) && !"null".equals(ljcs)) {
if (';' == ljcs.charAt(0)) {
jdbcUrl += ljcs;
} else {
jdbcUrl += ";" + ljcs;
}
}
} else if ("SYBASE".equals(sjklx.toUpperCase())) {//Sybase
jdbcUrl = "jdbc:sybase:Tds:" + ip + ":" + port+ "/" + sid;
if (StringUtils.hasText(ljcs) && !"null".equals(ljcs)) {
if (';' == ljcs.charAt(0)) {
jdbcUrl += ljcs;
} else {
jdbcUrl += ";" + ljcs;
}
}
} else {
throw new RuntimeException("数据库类型传入不正确,你传入的值为:" + sjklx);
}
return jdbcUrl;
}
}