我想将托管 bean 拆分为多个文件,因为源代码重复了多次。
这是我想放入外部文件的外部 Java 方法:
public class CalcWeight implements Serializable
{
// Call the Oracle JDBC Connection driver as service
@Resource(name = "jdbc/Oracle")
// Data source
private DataSource ds;
public float TotalDCWeightCalc(int dcId) throws SQLException
{
float DCWeight = 0;
if (ds == null)
{
throw new SQLException("Can't get data source");
}
........... SQL query
return DCWeight;
}
}
我使用此代码调用外部 Java 方法:
@ViewScoped
public class DCProfileTabGeneral implements Serializable
{
// Call the Oracle JDBC Connection driver as service
@Resource(name = "jdbc/Oracle")
// Data source
private DataSource ds;
// Weight initialization
private CalcWeight weight;
// Power initialization
private CalcPower power;
// Cooling initialization
private CalcCooling cooling;
// Constructor
public DCProfileTabGeneral()
{
// Weight initialization
weight = new CalcWeight();
}
..........................
weight.TotalDCWeightCalc(id)
.....................
}
运行此代码时收到此错误消息:
java.sql.SQLException: Can't get data source
我该如何解决这个问题?