Using WebLogic JDBC in an Application

今天在工作中遇到一段代码,是和Weblogic有关的,利用它获取数据源

第一步:Getting a Database Connection from a DataSource Object

Obtaining a Client Connection Using a DataSource

注意事项: When using a JDBC connection in a client-side application, the exact same JDBC driver classes must be in the CLASSPATH on both the server and the client. If the driver classes do not match, you may see java.rmi.UnmarshalException exceptions.

//ImportingPackagestoAccessDataSourceObjects
importjava.sql.*;
importjava.util.*;
importjavax.naming.*;

...
Contextctx
=null;
Hashtableht
=newHashtable();
ht.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
ht.put(Context.PROVIDER_URL,
"t3://hostname:port");

Connectionconn
=null;
Statementstmt
=null;
ResultSetrs
=null;

try...{
ctx
=newInitialContext(ht);
javax.sql.DataSourceds
=(javax.sql.DataSource)ctx.lookup("myDataSource");
conn
=ds.getConnection();

//Youcannowusetheconnobjecttocreate
//Statementsandretrieveresultsets:

stmt
=conn.createStatement();
stmt.execute(
"select*fromsomeTable");
rs
=stmt.getResultSet();

...

//CloseJDBCobjectsassoonaspossible
stmt.close();
stmt
=null;

conn.close();
conn
=null;

}

catch(Exceptione)...{
//afailureoccurred
logmessage;
}

finally...{
try...{
ctx.close();
}
catch(Exceptione)...{
logmessage;}

try...{
if(rs!=null)rs.close();
}
catch(Exceptione)...{
logmessage;}

try...{
if(stmt!=null)stmt.close();
}
catch(Exceptione)...{
logmessage;}

try...{
if(conn!=null)conn.close();
}
catch(Exceptione)...{
logmessage;}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值