1 java调用SQL server:
public static Connection connsql() throws SQLException
{
try
{
// 加载MYSQL JDBC驱动程序
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
} catch (Exception e)
{
// System.out.print("Error loading Oracle Driver!");
e.printStackTrace();
}
// DriverManager是管理一组 JDBC 驱动程序的基本服务。
Connection connect = DriverManager.getConnection
("jdbc:sqlserver://192.168.9.20:1433;databaseName=sentiment","sa",
"111");
return connect;
}
2 java调用oracle:
public static Connection connsql2() throws SQLException
{
try
{
// 加载MYSQL JDBC驱动程序
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (Exception e)
{
// System.out.print("Error loading Oracle Driver!");
e.printStackTrace();
}
// DriverManager是管理一组 JDBC 驱动程序的基本服务。
Connection connect = DriverManager.getConnection
(url,prop.getProperty(USERNAME),prop.getProperty(PASSWORD));
return connect;
}
3 数据库连接池:
package spider.dao;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
import javax.sql.DataSource;
import org.apache.commons.dbcp.BasicDataSource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* 数据库连接池
*
* @author caoyu
*
*/
public class ConnectPool {
private static Log log = LogFactory.getLog(ConnectPool.class);
private static String IP = "ip";
private static String PORT = "port";
private static String USERNAME = "username";
private static String PASSWORD = "password";
private static String INSTANCE = "instance";
private static String url;
private static String INITIALL_POOL_SIZE = "initialPoolSize";
private static String MAX_ACTIVE = "maxActive";
private static String MAX_IDLE = "maxIdle";
private static String MAX_WAIT = "maxWait";
private static Properties prop;
private static DataSource ds;
static {
prop = new Properties();
try {
prop.load(ConnectPool.class.getResourceAsStream("db.properties"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
url = "jdbc:oracle:thin:@" + prop.getProperty(IP) + ":"
+ prop.getProperty(PORT) + ":" + prop.getProperty(INSTANCE);
BasicDataSource bds = new BasicDataSource();
bds.addConnectionProperty("SetBigStringTryClob", "true"); //支持大字符串作为输入
bds.addConnectionProperty("accessToUnderlyingConnectionAllowed", "true");
bds.setDriverClassName("oracle.jdbc.driver.OracleDriver");
bds.setUrl(url);
bds.setUsername(prop.getProperty(USERNAME));
bds.setPassword(prop.getProperty(PASSWORD));
bds.setInitialSize(Integer.parseInt(prop
.getProperty(INITIALL_POOL_SIZE)));
bds.setMaxActive(Integer.parseInt(prop.getProperty(MAX_ACTIVE)));
bds.setMaxIdle(Integer.parseInt(prop.getProperty(MAX_IDLE)));
bds.setMaxWait(Long.parseLong(prop.getProperty(MAX_WAIT)));
ds = bds;
}
public static Connection getConnection() {
while (true) {
try {
Connection conn = ds.getConnection();
return conn;
} catch (org.apache.commons.dbcp.SQLNestedException e) {
if (true/*log.isDebugEnabled()*/) {
log.info("连接池没有多余连接,请等待");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
log.error("连接池异常",e);
}
}
}
public static Connection getConnectionWithoutBlocking() {
try {
return ds.getConnection();
} catch (org.apache.commons.dbcp.SQLNestedException e) {
if (log.isDebugEnabled()) {
log.debug("连接池没有多余连接,请等待");
}
return null;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
// public static void main(String args[]) {
// for (int i=0; i<555; i++)
// {
// ConnectPool.getConnection();
// }
// }
}
4 db.properties
ip:192.168.9.24
port:1521
username:opinion
password:000000
instance:sentiment
initialPoolSize:10
maxActive:100
maxIdle:5
maxWait:5000