当前位置: 技术问答>java相关
向ejb高手求教--关于jdbc Api在ejb中连接sql server 7.0的问题
来源: 互联网 发布时间:2015-01-04
本文导语: 我用freetds_jdbc.jar jdbc api 在ejb中连接sql server 7.0 但是出这样的问题---请看: 这是我的程序代码(部分) import java.util.*; import javax.ejb.*; import java.sql.*; import javax.sql.*; import javax.naming.*; import javax.transaction....
我用freetds_jdbc.jar jdbc api 在ejb中连接sql server 7.0
但是出这样的问题---请看:
这是我的程序代码(部分)
import java.util.*;
import javax.ejb.*;
import java.sql.*;
import javax.sql.*;
import javax.naming.*;
import javax.transaction.*;
public class accountEjb implements SessionBean
{
private String usrID;
private String usrPwd;
private SessionContext context;
private Connection con;
private String dbName = "java:comp/env/jdbc/Account";
private double machineBalance;
/////callback
public void ejbCreate(String id) throws CreateException
{
System.out.println("begine create the bean");
usrID = id;
try
{
makeConnection();
System.out.println("makeConnection ok");
machineBalance = selectMachine();
System.out.println(machineBalance);
System.out.println("end create the bean");
}
catch (Exception ex)
{
System.out.println("the Error is:");
throw new CreateException(ex.getMessage());
}
}
..................
.....................................省略
///access database
private void makeConnection() throws NamingException, SQLException
{
System.out.println("access database-makeConnection");
InitialContext ic = new InitialContext();
DataSource ds = (DataSource) ic.lookup(dbName);
System.out.println("look ok");
con = ds.getConnection();
}
private double selectMachine() throws SQLException
{
System.out.println("access database-selectMachine");
String selectStatement =
"SELECT balance " +
"FROM ejbtest.dbtest.account " +
"WHERE name = 'George'";
PreparedStatement prepStmt =
con.prepareStatement(selectStatement);
System.out.println("prepareStatement ok");
ResultSet rs = prepStmt.executeQuery();
if (rs.next())
{
double result = rs.getDouble(1);
prepStmt.close();
return result;
}
else
{
prepStmt.close();
throw new EJBException("Row for id " + usrID + " not found.");
}
}
......................................
.............省略
} // over
当执行到 con = ds.getConnection(); 就会抛出这个错误
javax.ejb.CreateException: access denied (java.lang.RuntimePermission accessDecl
aredMembers)
有谁用过?或是知道答案请指教?
或是提点建议---在下感激不尽
但是出这样的问题---请看:
这是我的程序代码(部分)
import java.util.*;
import javax.ejb.*;
import java.sql.*;
import javax.sql.*;
import javax.naming.*;
import javax.transaction.*;
public class accountEjb implements SessionBean
{
private String usrID;
private String usrPwd;
private SessionContext context;
private Connection con;
private String dbName = "java:comp/env/jdbc/Account";
private double machineBalance;
/////callback
public void ejbCreate(String id) throws CreateException
{
System.out.println("begine create the bean");
usrID = id;
try
{
makeConnection();
System.out.println("makeConnection ok");
machineBalance = selectMachine();
System.out.println(machineBalance);
System.out.println("end create the bean");
}
catch (Exception ex)
{
System.out.println("the Error is:");
throw new CreateException(ex.getMessage());
}
}
..................
.....................................省略
///access database
private void makeConnection() throws NamingException, SQLException
{
System.out.println("access database-makeConnection");
InitialContext ic = new InitialContext();
DataSource ds = (DataSource) ic.lookup(dbName);
System.out.println("look ok");
con = ds.getConnection();
}
private double selectMachine() throws SQLException
{
System.out.println("access database-selectMachine");
String selectStatement =
"SELECT balance " +
"FROM ejbtest.dbtest.account " +
"WHERE name = 'George'";
PreparedStatement prepStmt =
con.prepareStatement(selectStatement);
System.out.println("prepareStatement ok");
ResultSet rs = prepStmt.executeQuery();
if (rs.next())
{
double result = rs.getDouble(1);
prepStmt.close();
return result;
}
else
{
prepStmt.close();
throw new EJBException("Row for id " + usrID + " not found.");
}
}
......................................
.............省略
} // over
当执行到 con = ds.getConnection(); 就会抛出这个错误
javax.ejb.CreateException: access denied (java.lang.RuntimePermission accessDecl
aredMembers)
有谁用过?或是知道答案请指教?
或是提点建议---在下感激不尽
|
因该不是pool名字的错误,因为如果是名字的错误的话,
那么在DataSource ds = (DataSource) ic.lookup(dbName);
这一步就应该报告javax.naming.NameNotFoundException: ejb contacts; remaining name ’java:comp/env/jdbc/Account‘
那么在DataSource ds = (DataSource) ic.lookup(dbName);
这一步就应该报告javax.naming.NameNotFoundException: ejb contacts; remaining name ’java:comp/env/jdbc/Account‘
|
看看xml部署文件的pool名字有没有错误?
还有服务器有没有建立Account连接池
还有服务器有没有建立Account连接池