设置属于另一个类的Object的prepareStatement方法的ID [英] setting id of preparedStatement method that belongs to Object from another class

查看:90
本文介绍了设置属于另一个类的Object的prepareStatement方法的ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此与先前的方法有关.这是我的UserDAO和DaoFactory. UserDao拥有daoFactory的实例.

So with relation to the previous method. This is my UserDAO, and DaoFactory. The UserDao holds an instance of the daoFactory.

这是我的UserDAO:

This is my UserDAO:

public class UsuariousDAO {

    private static final String SQL_LIST_ALL =
            "SELECT DISTINCT * "
            + "FROM usuarios WHERE NOT EXISTS (SELECT * FROM usuarios_grupos WHERE usuarios_grupos.id_grupo = ? AND usuarios_grupos.id_usuario = usuarios.id_usuario)";


    private static final String SQL_INSERT =
            "INSERT INTO usuarios (nome, setor, senha, email, bloquear, admin) VALUES (?, ?, ?, ?, ?, ?)";

    private static final String SQL_UPDATE =
            "UPDATE usuario SET nome = ?, setor = ?, senha = ?, email = ?, bloquear = ?, admin = ? WHERE id_usuario = ?";

    private static final String SQL_DELETE =
            "DELETE FROM usuario WHERE id_usuario = ?";

    private DAOFactory daoFactory;

     UsuariousDAO(DAOFactory daoFactory) {
        this.daoFactory = daoFactory;
    }

     public Usuarious find(Integer id) throws DAOExceptions {
        return find(SQL_LIST_BY_ID_GRUPO, id);
    }

我的USERDAO中具有以下方法:

I have the following methods in my USERDAO:

private Usuarious find(String sql, Object... values) throws DAOExceptions {

                                CODE
}

 public List<Usuarious> list() throws DAOExceptions {

                               CODE
}

 public List<Usuarious> list(Grupos groups) throws DAOExceptions {

                                   CODE
    }


 public void create(Usuarious user) throws IllegalArgumentException, DAOExceptions {

                              CODE
}


public void update(Usuarious user) throws DAOExceptions {
                                  CODE
}



public void save(Usuarious user) throws DAOExceptions {
        if (user.getId_usuario() == null) {
            create(user);
        } else {
            update(user);
        }
    }


public void delete(Usuarious user) throws DAOExceptions {

                    CODE
}

   private static Usuarious mapUser(ResultSet rs) throws SQLException {
        Usuarious user = new Usuarious(rs.getInt("id_usuario"), rs.getString("nome"), rs.getString("setor"),
                rs.getString("senha"), rs.getString("email"), rs.getString("bloquear"), rs.getString("admin"));
        return user;
    }

} //end of class

我的DaoFactory类如下:

MY DaoFactory class is the following:

     public abstract class DAOFactory  {

        private static final String JNDI_ROOT = "java:comp/env/";

         public static DAOFactory getInstance(String name) throws DAOConfigurationException {
            if (name == null) {
                throw new DAOConfigurationException("Database name is null.");
            }

            String url = "jdbc:mysql://200.230.71.12:3306/social";
            String driverClassName = "com.mysql.jdbc.Driver";
            String password = "1234cinco";
            String username = "cepein";
            DAOFactory instance;

   if (driverClassName != null) {
            try {
                Class.forName(driverClassName);
            } catch (ClassNotFoundException e) {
                throw new DAOConfigurationException(
                    "Driver class '" + driverClassName + "' is missing in classpath.", e);
            }
            instance = new DriverManagerDAOFactory(url, username, password);
        }

        // Else assume URL as DataSource URL and lookup it in the JNDI.
        else {
            DataSource dataSource;
            try {
                dataSource = (DataSource) new InitialContext().lookup(JNDI_ROOT + url);
            } catch (NamingException e) {
                throw new DAOConfigurationException(
                    "DataSource '" + url + "' is missing in JNDI.", e);
            }
            if (username != null) {
                instance = new DataSourceWithLoginDAOFactory(dataSource, username, password);
            } else {
                instance = new DataSourceDAOFactory(dataSource);
            }
        }

        return instance;
    }




  abstract Connection getConnection() throws SQLException;

        // DAO getters --------------------------------------------------------------------------------

        /**
         * Returns the User,Grupos, UserGrupos DAO associated with the current DAOFactory.
         * @return The User,Grupos, UserGrupos DAO associated with the current DAOFactory.
         */


          public UsuariousDAO getUserDAO() {
        return new UsuariousDAO(this);
    }

    public GruposDAO getGruposDAO() {
        return new GruposDAO(this);
    }

    public UsuariousGruposDAO getUsuariousGruposDAO() {
        return new UsuariousGruposDAO(this);
    }

}


class DriverManagerDAOFactory extends DAOFactory {
    private String url;
    private String username;
    private String password;

    DriverManagerDAOFactory(String url, String username, String password) {
        this.url = url;
        this.username = username;
        this.password = password;
    }

    Connection getConnection() throws SQLException {
        return DriverManager.getConnection(url, username, password);
    }
}

/**
 * The DataSource based DAOFactory.
 */
class DataSourceDAOFactory extends DAOFactory {
    private DataSource dataSource;

    DataSourceDAOFactory(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    Connection getConnection() throws SQLException {
        return dataSource.getConnection();
    }
}

/**
 * The DataSource-with-Login based DAOFactory.
 */
class DataSourceWithLoginDAOFactory extends DAOFactory {
    private DataSource dataSource;
    private String username;
    private String password;

    DataSourceWithLoginDAOFactory(DataSource dataSource, String username, String password) {
        this.dataSource = dataSource;
        this.username = username;
        this.password = password;
    }

    Connection getConnection() throws SQLException {
        return dataSource.getConnection(username, password);
    }
}

这是我的清单(Grupos grps):

Here is my list(Grupos grps):

 public List<Usuarious> list(Grupos groups) throws DAOExceptions {
    Connection connection = null;
    PreparedStatement preparedStatement = null;
    ResultSet resultSet = null;
    List<Usuarious> users = new ArrayList<Usuarious>();

    try {
        connection = daoFactory.getConnection();
        preparedStatement = connection.prepareStatement(SQL_LIST_ALL);
        preparedStatement.setInt(1, groups.getId_grupo());
        resultSet = preparedStatement.executeQuery();
        while (resultSet.next()) {
            users.add(mapUser(resultSet));
        }
    } catch (SQLException e) {
        throw new DAOExceptions(e);
    } finally {
        close(connection, preparedStatement, resultSet);
    }

    return users;
}

我在这里在用户托管的Bean中调用该方法:

I call the method in my User managed bean here:

public List<Usuarious> getListOfUsuarios() throws DAOExceptions {
        List<Usuarious> usuariosList = userDAO.list(grps);
        listOfUsuarios = usuariosList;
        return listOfUsuarios;
    }

在我看来,以下内容:

<p:dataTable var="users" value="#{usuariousGruposBean.listOfUsuarios}"
                                 selection="#{users}" selectionMode="single">
                        <p:column headerText="" style="height:0" rendered ="false">
                            <h:outputText value="#{users.id_usuario}"/>
                        </p:column> 

推荐答案

调用此方法时,变量groups为null,因此问题出在调用者而不是这里.

The variable groups is null when you call this method, so the problem is in the caller and not here.

NullPointerException表示preparedStatementgroups为空(但我们现在preparedStatement不是).

NullPointerException on preparedStatement.setInt(1, groups.getId_grupo()); means that either preparedStatement or groups are null (but we now preparedStatement is not).

这篇关于设置属于另一个类的Object的prepareStatement方法的ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆