日期:2014-05-16 浏览次数:20507 次
oracle在tomcat中的配置:
?
<Context path="/DatabaseTest" docBase="/DatabaseTest" reloadable="false" crossContext="true">
<Resource name="jdbc/oracle" auth="Container" type="javax.sql.DataSource"
maxActive="4" maxIdle="2" maxWait="10000" removeAbandoned="true" removeAbandonedTimeout="60" logAbandoned="true"
username="xxxx" password="xxxx"
driverClassName="oracle.jdbc.OracleDriver"
url="jdbc:oracle:thin:@10.10.10.33:15210:orcl"/>
</Context>
?sql server在tomcat中的配置:
<Context path="/DatabaseTest" docBase="/DatabaseTest" reloadable="false" crossContext="true">
<Resource name="jdbc/sqlserver" auth="Container" type="javax.sql.DataSource" maxActive="10" maxIdle="2" maxWait="10000" removeAbandoned="true" removeAbandonedTimeout="60" logAbandoned="true"
username="xxx" password="xxx" driverClassName="net.sourceforge.jtds.jdbc.Driver" url="jdbc:jtds:sqlserver://10.10.10.250:1433;DatabaseName=xxx"/>
</Context>
?mysql 在tomcat的配置:
<Context path="/DatabaseTest" docBase="/DatabaseTest" reloadable="false" crossContext="true">
<Resource name="jdbc/mysql" auth="Container" type="javax.sql.DataSource"
maxActive="5" maxIdle="2" maxWait="10000" removeAbandoned="true" removeAbandonedTimeout="60" logAbandoned="true"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost/lpm?user=root&password=&characterEncoding=UTF-8" />
</Context>
??==================================================================
要获取数据库连接可以在src下新建一个conf.property:
ORCL_CONNECTION=java:comp/env/jdbc/oracle
SQL_CONNECTION=java:comp/env/jdbc/sqlserver
MYSL_CONNECTION=java:comp/env/jdbc/mysql
?然后(获取连接后,要记得自己调用下close()方法关闭连接):
public class DBConn {
private static Logger log = Logger.getLogger(DBConn.class.getName());
private static InitialContext context = null;
private static DataSource sql_datas = null;
private static DataSource orcl_datas = null;
private static DataSource mysql_datas = null;
private static String sql_conn = null;
private static String orcl_conn = null;
private static String mysql_conn = null;
static
{
sql_conn = GetCfg.getValue("SQL_CONNECTION");
orcl_conn = GetCfg.getValue("ORCL_CONNECTION");
mysql_conn = GetCfg.getValue("MYSL_CONNECTION");
log.info("sql_conn:"+sql_conn);
log.info("orcl_conn:"+orcl_conn);
log.info("mysql_conn:"+mysql_conn);
try {
context = new InitialContext();
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static Connection getOrlConn()
{
Connection con = null;
try
{
if(null == context)
{
log.error("getConn NO_CONTEXT_FOUND");
return null;
}
if(null == orcl_datas )
{
orcl_datas = (DataSource)context.lookup(orcl_conn);
if(null == orcl_datas)
{
log.error("getConn lookup fail orcl_conn" + orcl_conn);
return null;
}
}
con = orcl_datas.getConnection();
}
catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
log.error("getConn exception orcl_conn="+orcl_conn);
}
return con;
}
public static Connection getSqlConn()
{
Connection con