Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6893835
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:42:30+00:00 2026-05-27T06:42:30+00:00

So I am trying to find an example online about creating a preparedStatement that

  • 0

So I am trying to find an example online about creating a preparedStatement that has an sql query referencing multiple tables.

For e.g. The examples I’ve encountered so far are always

e.g.

s = conn.prepareStatement ("DELETE FROM Users WHERE id_user = ?");
s.setInt (1, 2);

where there is only one table involved, and the method exists in the same class of the database table. E.g. User.class , user table in database.

The query that I have requires me to set the place holder from another table/class. In this case, my method exists in the User.class, however, it requires a the binding from a Group object.

SELECT DISTINCT *
FROM usuarios 
WHERE NOT EXISTS
(SELECT * FROM usuarios_grupos 
 WHERE usuarios_grupos.id_grupo = ? 
 AND usuarios_grupos.id_usuario = usuarios.id_usuario);

Will the method be the following:

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

or will it be written differently?? because I seem to be getting a NPE with this, and from the examples I’ve seen online. The query always reference 1 table. Is what I’m doing here wrong?

okay here is my method for groups.getId_grupo(), which exists in my Group.class:

public class Grupos {

Integer id_grupo;
String descricao;

public Grupos() {

}

public Grupos(Integer id_grupo, String descricao) {
    this.id_grupo = id_grupo;
    this.descricao = descricao;
}

public Grupos(Integer id_grupo) {
    this.id_grupo = id_grupo;
}


public String getDescricao() {
    return descricao;
}

public void setDescricao(String descricao) {
    this.descricao = descricao;
}

public Integer getId_grupo() {
    return id_grupo;
}

public void setId_grupo(Integer id_grupo) {
    this.id_grupo = id_grupo;
}

}

I am calling my List list(Grupos groups) method in my ManagedBean

public class UsuariousGruposBean implements Serializable {

private Usuarious user = new Usuarious();
private Grupos grps = new Grupos();
private UsuariousGrupos userGroups = new UsuariousGrupos();

protected final UsuariousDAO userDAO = daoFactory.getUserDAO();
protected final GruposDAO grpDAO = daoFactory.getGruposDAO();
protected final UsuariousGruposDAO userGrpDAO = daoFactory.getUsuariousGruposDAO();

    private List<Usuarious> listOfUsuarios;
    private List<Grupos> listOfGrps;
    private List<UsuariousGrupos> listOfUserGroups;

public UsuariousGruposBean() {
    }

    public List<Usuarious> getListOfUsuarios() throws DAOExceptions {
        List<Usuarious> usuariosList = userDAO.list(grps);
        listOfUsuarios = usuariosList;
        return listOfUsuarios;
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T06:42:31+00:00Added an answer on May 27, 2026 at 6:42 am

    First instance in your code can throw NPE is at:

    preparedStatement = connection.prepareStatement(SQL_LIST_ALL);
    

    if your connection is null, your connection factory didnt return you one, check if you have a valid connection

    Second place :

    groups.getId_grupo()
    

    Check if your groups is null or not

    If these are not the reasons then please post your stacktrace.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to find an example that has css rollover using sprites & sliding door
Trying to find some simple SQL Server PIVOT examples. Most of the examples that
I am trying to find a good example that can help me to understand
Solution: I've misinterpreted the example from SQL Books Online. Yes, that below the section
I'm trying to divide in assembly language I can't find a good example online
I am trying to find an example of placing an element above the Table
I've been unsuccessful in trying to find an example how to get a sqlite
I'm trying to find a really good example for implementing the MetaWeblog API using
I am using UICatalog example and trying to find whether it is possible to
This is probably explained more easily with an example. I'm trying to find a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.