首先在weblogic中配好连接池,其用法和其它的数据据库的连接方法都是一样的
解决方案 »
- Struts2学习问题,求解
- sparkweb怎样配置?
- Apache2.2+Tomcat5.5整合在windowsxp下面正常在windows2003下总是报503错误
- 一个多线程的问题
- j2ee中与中间件实现相关的技术有哪些??
- client 访问ejb时的错!help!
- 高分求一极其简单的基本问题。在线等待。。急。。
- 加了spring框架的项目,启动tomcat报No Spring WebApplicationInitializer types detected on cl
- java开发的BS项目里异常一般怎么处理啊 我们可以再catch里往log4j里写入日志 但是出了异常 展示的页面一般怎么办呢
- spring定时任务配置
- 谁能给我讲讲分页的东西!Struts里的
- 有人用java自己写过安装程序吗?
EjbContext ectx = new EjbContext();
ParamMsg parammsg = new ParamMsg();
String pagesql = "";
private netshop.page.pageParam pageparam;
//private pageParam pageparam = new pageParam(); public superDao()
{ //获取context对象
try
{
//ctx = new InitialContext();
ctx = ectx.getInitialContext();
}catch(NamingException jnex)
{
System.err.println(ErrMsg.NAMING_EXCETPION_MSG);
}catch(java.lang.Exception jlex)
{
jlex.printStackTrace();
} } //进行连接
protected DataSource getDataSource()
{
DataSource ds=null;
try{
ds = (DataSource)ctx.lookup("MSDataSource");
}catch(NamingException jnex)
{
System.err.println(ErrMsg.NAMING_EXCETPION_MSG);
}
return ds;
} //关闭所有打开的资源
protected static void closeAll(Connection connection, Statement statement, ResultSet resultSet)
{
//关闭结果集
if (resultSet != null)
{
try{
resultSet.close();
}catch(java.sql.SQLException jsqlex)
{
System.err.println("SQL Error:" + jsqlex.getMessage());
}
}
//关闭状态
if (statement != null)
{
try {
statement.close();
}catch(java.sql.SQLException jsqlex)
{
System.err.println("SQL Error:" + jsqlex.getMessage());
}
} //关闭连接
if( connection != null)
{
try{
connection.close();
}catch(java.sql.SQLException jsqlex)
{
System.err.println("SQL Exception:" + jsqlex.getMessage());
}
}
} protected static void closeAll(Connection connection, PreparedStatement statement, ResultSet resultSet)
{
//关闭结果集
if (resultSet != null) {
try {
resultSet.close();
}
catch (java.sql.SQLException jsqlex) {
System.err.println("SQL Error:" + jsqlex.getMessage());
}
}
//关闭状态
if (statement != null) {
try {
statement.close();
}
catch (java.sql.SQLException jsqlex) {
System.err.println("SQL Error:" + jsqlex.getMessage());
}
} //关闭连接
if (connection != null) {
try {
connection.close();
}
catch (java.sql.SQLException jsqlex) {
System.err.println("SQL Exception:" + jsqlex.getMessage());
}
}
} //获得连接
public Connection getConnection()
{
Connection con = null;
PreparedStatement stm = null;
ResultSet rs = null;
DataSource ds = null; String uid = null; ds = getDataSource(); try{
con = ds.getConnection();
}catch(SQLException jsqlex)
{
jsqlex.printStackTrace();
} return con; }
【services】-【jdbc】-【Connection pool】,新建连接池;
【services】-【jdbc】-【Data Source】,新建数据源;注意其中的JNDI名;
再用楼上的方法访问数据源,OK。
Context EjbContext ParamMsg DataSource NamingException
是不是有什么包我没有Import进来呀
import javax.naming.Context;
import javax.sql.DataSource;
import java.sql.*;
import java.io.*;
import javax.rmi.*;
import java.util.*; private boolean creatConnection(){
/*********************************************************************
功能:连接数据源,产生数据连接
*********************************************************************/
try{
InitialContext ctx = new InitialContext();
javax.sql.DataSource myDataSource = (javax.sql.DataSource) ctx.lookup("数据源JNDI名");
con = myDataSource.getConnection();
stm = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
return true;
}catch (Exception e) {
System.out.println("连接出错!!!"+e.getMessage());
return false;
} }