连接池配置好了,且在jsp中测试也通过了,代码:
<%@ page contentType= "text/html;charset=gb2312 "%>
<%@ page import= "javax.naming.Context " %>
<%@ page import= "javax.sql.DataSource " %>
<%@ page import= "javax.naming.InitialContext " %>
<%@ page import= "java.sql.* " %>
<%
DataSource ds = null;
try
{
Context initCtx = new InitialContext();
out.println(1);
Context envCtx = (Context)initCtx.lookup( "java:comp/env ");
out.println(2);
ds = (DataSource)envCtx.lookup( "jdbc/Jsp_BBS ");
out.println(3);
if (ds != null)
{
out.println( "已经获得DataSource! ");
out.println( " <br> ");
Connection conn = ds.getConnection();
Statement stmt = conn.createStatement();
out.println( "success! ");
}
}
catch(Exception e)
{
out.println(e.getMessage());
}
%>
但在javabean中使用却出错了,提示的错误:
Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
代码是:
package com.bean;
import java.sql.*;
import javax.naming.*;
import javax.sql.*;
public class dbControl
{
DataSource ds = null;
Connection conn;
String strSql;
final static String datasource = "java:comp/env/jdbc/Jsp_BBS ";
public dbControl()
{
try
{
Context initCtx = new InitialContext();
//lookup(String name):返回与指定的名字绑定的对象,获得数据库连接工厂
ds = (DataSource)initCtx.lookup( "java:comp/env/jdbc/Jsp_BBS ");
//Context initCtx = new InitialContext();
//Context envCtx = (Context)initCtx.lookup( "java:comp/env ");
//ds = (DataSource)initCtx.lookup(datasource);
conn = ds.getConnection();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
public dbControl(String strSql)
{
this();
this.strSql = strSql;
}
public static void main(String [] args)
{
dbControl db = new dbControl();
}
}
------解决方案--------------------
没人帮忙吗?无语