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 = null;
try
{
if(null == context)
{
log.error("getConn NO_CONTEXT_FOUND");
return null;
}
if(null == sql_datas )
{
sql_datas = (DataSource)context.lookup(sql_conn);
if(null == sql_datas)
{
log.error("getConn lookup fail sql_conn" + sql_conn);
return null;
}
}
con = sql_datas.getConnection();
}
catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
log.error("getConn exception sql_conn="+sql_conn);
}
return con;
}
public static Connection getMysqlConn()
{
Connection con = null;
try
{
if(null == context)
{
log.error("getConn NO_CONTEXT_FOUND");
return null;
}
if(null == mysql_datas )
{
mysql_datas = (DataSource)context.lookup(mysql_conn);
if(null == mysql_datas)
{
log.error("getConn lookup fail mysql_conn" + mysql_conn);
return null;
}
}
con = mysql_datas.getConnection();
}
catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
log.error("getConn exception mysql_conn="+mysql_conn);
}
return con;
}
}
以下是读取配置文件的功能类:
public class GetCfg {
private static Properties pro = null;
private static GetCfg getcfg = new GetCfg();
private GetCfg()
{
pro = new Properties();
try {
pro.load(this.getClass().getClassLoader().getResourceAsStream("conf.properties"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static String getValue(String key)
{
String v = pro.getProperty(key.trim()).trim();
return (null == v)? "" : v.trim();
}
public static String getDefaultValue(String key ,String defaultStr)
{
return pro.getProperty(key.trim(), defaultStr).trim();
}
}
另外一种直接写在程序里简单的数据库连接方式:
public static void main(String[] args) {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost/lpm", "root", "");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from persion");
while(rs.next()){
System.out.println(rs.getString("name"));
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}