0

CDI Bean から 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 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 メソッドからデータソースを参照すると、次のエラーが発生します。

Can't get data source

静的 Java メソッドからデータソースにアクセスできるかどうかはわかりません。この問題を解決する方法はありますか?

4

1 に答える 1

2

@Resourceコンテナが静的フィールドまたは静的メソッドに注釈付きで何かを注入するかどうかはわかりません。クラスを再考し、おそらくそれを作成してみてください。@ApplicationScopedそうすると、アプリケーションごとにインスタンスが1つだけになります。

クラスの変更点は次のとおりです。

@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;
  }
}


次に、それをインジェクトされたオブジェクトとしてもコードで使用できます。そうなることは確かですが、そうDataSourceなる場合は、構成ファイルのようnullに適切に定義されているかどうかを確認してください。DataSource

于 2013-01-26T06:41:29.310 に答える