设为首页 收藏本站
查看: 372|回复: 0

[经验分享] oracle+sql server+mysql 数据库tomcat配置连接信息

[复制链接]
YunVN网友  发表于 2016-8-15 06:46:23 |阅读模式
  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&amp;password=&amp;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();
}

}
   
   

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-257790-1-1.html 上篇帖子: 如何启用和禁用oracle&DB2数据库外键约束 下篇帖子: Oracle虚拟私有数据库(VPD)概述及简单举例
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表