无法在静态Java方法中获取数据源
我正在尝试从CDIBean访问Oracle数据源无法在静态Java方法中获取数据源,java,sql,oracle,jsf,jsf-2,Java,Sql,Oracle,Jsf,Jsf 2,我正在尝试从CDIBean访问Oracle数据源 import java.io.Serializable; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import javax.annotation.Resource; import javax.faces.bean.ViewScoped; import
import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.annotation.Resource;
import javax.faces.bean.ViewScoped;
import javax.inject.Named;
import javax.sql.DataSource;
@Named("ParentIDNameResolveController")
@ViewScoped
public class ParentIDNameResolve implements Serializable
{
// Call the Oracle JDBC Connection driver
@Resource(name = "jdbc/Oracle")
private static DataSource ds;
// Get the ID if the parent
public static int ParentId(int chieldId) throws SQLException
{
int ParentId = 0;
if (ds == null)
{
throw new SQLException("Can't get data source");
}
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs;
try
{
conn = ds.getConnection();
ps = conn.prepareStatement("SELECT COMPONENTID, FKCOMPONENTID, COMPONENTSTATSID from COMPONENT where COMPONENTID = ?");
ps.setLong(1, chieldId);
rs = ps.executeQuery();
while (rs.next())
{
ParentId = rs.getInt("FKCOMPONENTID");
}
}
finally
{
if (ps != null)
{
ps.close();
}
if (conn != null)
{
conn.close();
}
}
return ParentId;
}
// Get Parent Name
public static String ParentName(int ParentId) throws SQLException
{
String ParentName = null;
if (ds == null)
{
throw new SQLException("Can't get data source");
}
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs;
try
{
conn = ds.getConnection();
ps = conn.prepareStatement("SELECT COMPONENTSTATSID, NAME from COMPONENTSTATS where COMPONENTSTATSID = ?");
ps.setLong(1, ParentId);
rs = ps.executeQuery();
while (rs.next())
{
ParentName = rs.getString("NAME");
}
}
finally
{
if (ps != null)
{
ps.close();
}
if (conn != null)
{
conn.close();
}
}
return ParentName;
}
}
不幸的是,当我从静态Java方法引用datasource时,我得到了以下错误:
Can't get data source
我不确定是否可以从静态Java方法访问数据源。有办法解决此问题吗?不确定您的容器是否会向静态字段或静态方法注入任何带有adnotation
@Resource
的内容。尝试重新思考您的类,并可能使其成为@ApplicationScoped
,那么每个应用程序也只有一个实例
以下是对您的课程的一些小改动:
@Named("ParentIDNameResolveController")
@javax.enterprise.context.ApplicationScoped // not from javax.faces.bean
public class ParentIDNameResolve implements Serializable
{
// Call the Oracle JDBC Connection driver
@Resource(name = "jdbc/Oracle")
private DataSource dataSource;
/*
Add getter/setter for DataSource
*/
public DataSource getDataSource() {
return this.ds;
}
public void DataSource setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
/* Change method signature to non static */
public int ParentId(int chieldId) throws SQLException
{
DataSource ds = getDataSource();
// your code here
return ParentId;
}
/* Change method signature to non static */
public String ParentName(int ParentId) throws SQLException
{
DataSource ds = getDataSource();
// your code here
return ParentName;
}
}
接下来,您可以将其作为注入对象在代码中使用,您可以确保
数据源
不会为null
,但如果会,请检查它是否在配置文件中正确定义为数据源
。您尝试过不使用静态
属性或方法吗?另外,您不能在CDIBean上使用@ViewScoped
注释,该注释只属于JSF管理的bean(至少如果您从javax.faces.bean
包中使用该注释,则不会)。可能的解决方案是什么?为什么首先要/需要将属性设置为static
?