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 8665011
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:22:47+00:00 2026-06-12T17:22:47+00:00

I am making a program that retrieves the inputted data/values of fields from the

  • 0

I am making a program that retrieves the inputted data/values of fields from the database I created. But when I’m running it the output is always null. I wonder what’s wrong? Here’s my code:

import java.sql.*;

public class GuestsInfo
{
private String  firstName, lastName;
private int  age;
private Connection con;
private PreparedStatement statement;

public GuestsInfo()
{
    try {
         Class.forName("com.mysql.jdbc.Driver");
         con = DriverManager.getConnection(
                        "jdbc:mysql://localhost:3306/3moronsdb","root","");
    } catch (Exception e) {
         e.printStackTrace();
    }
}
public GuestsInfo(String firstName, String lastName, int age)
{
    this();
    try
    {   
        statement = con.prepareStatement("Insert into guest(firstName,lastName,age)values(?,?,?)");

        statement.setString(1, firstName);
        statement.setString(2, lastName);
        statement.setInt(3, age);

        statement.executeUpdate();
        con.close();
    }
    catch(Exception e)
    {
        e.printStackTrace();
        return;
    }   
}


public void setFirstName(String firstName)
{

    try{
        statement= con.prepareStatement("SELECT * FROM guest WHERE firstName = ?");
        statement.setString(1, firstName);
        ResultSet rs = statement.executeQuery();
        while(rs.next()){
            firstName = rs.getString(1);
        }
    }catch(Exception e){
        System.out.print(e);
    }
}

public String getFirstName(){
    return firstName;
}

public void setLastName(String lastName)
{

    try{
        statement= con.prepareStatement("SELECT * FROM guest WHERE lastName = ?");
        statement.setString(2, lastName);
        ResultSet rs = statement.executeQuery();
        while(rs.next()){
            lastName = rs.getString(2);
        }
    }catch(Exception e){
        System.out.print(e);
    }
}

public String getLastName(){
    return lastName;
}

public void setAge(int age){
    try{
        statement = con.prepareStatement("SELECT * FROM guest WHERE age=?");
        statement.setInt(3, age);
        ResultSet rs = statement.executeQuery();
        while(rs.next()){
            age = rs.getInt(3);
        }
    }catch(Exception e){
        System.out.print(e);
    }
}

public int getAge(){
    return age;
}

In my main class I have this codes:

 public class TestMain {
public static void main(String [] args){

    GuestsInfo gi = new GuestsInfo();
    gi.setFirstName("Ericka");
    System.out.println(gi.getFirstName());

}
 }

I wonder why it always says null when I’m running it.

  • 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-06-12T17:22:48+00:00Added an answer on June 12, 2026 at 5:22 pm

    You are not actually “setting” anything in your setters.

    public void setFirstName(String firstName)
    {
    
        try{
        // Here you create a READing statement.
            statement= con.prepareStatement("SELECT * FROM guest WHERE firstName = ?");
            statement.setString(1, firstName);
            ResultSet rs = statement.executeQuery();
            while(rs.next()){
        // Here you set the >>method param<< to a non-existing String
                firstName = rs.getString(1);
            }
        }catch(Exception e){
           System.out.print(e);
        }
    }
    // After the method is done you have done nothing because you didn't even set your class member variable "firstName", which you would have set using "this.firstName".
    

    On top of that: My understanding of getters and setter is such that you would do the query in your getter.

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

Sidebar

Related Questions

I am making a program that graphs a line based on data inputted by
I have a C# program that retrieves multiple rows from a DB but currently
I'm making a program that retrieves decently large amounts of data through a python
I am making a program that get's it's data from a site that needs
I'm making a program that gets information from a database and puts in a
I am trying to learn python and am making a program that will output
I'm making an android program that retrieves content of a webpage using HttpURLConnection. I'm
I'm making a program that will display a few images from a directory beside
I'm making a program that will output lines to a text file. I don't
I'm making a program that connects to a website and downloads XML from it.

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.