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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T06:47:22+00:00 2026-05-18T06:47:22+00:00

When I call getName from MyServ class, I get null, but when I call

  • 0

When I call getName from MyServ class, I get null, but when I call them locally from DBClass they return strings. anyone know what I’m doing wrong?

package DB;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class DBClass{

    private Statement stmt;
    private Connection conn;
    private ResultSet result;
    public String name, surname;

    public DBClass(){
    }

    public Connection dbConnect(final String db_connect_string,
        final String db_userid,
        final String db_password){

        try{
            DriverManager.registerDriver(new oracle.jdbc.OracleDriver());

            conn =
                DriverManager.getConnection(db_connect_string, db_userid,
                    db_password);

            stmt = conn.createStatement();
            result = stmt.executeQuery("Select * from .....etc");

            if(result.next()){
                name = result.getString(1).toString();
                surname = result.getString(2).toString();
            }
            return conn;

        } catch(final Exception e){
            e.printStackTrace();

            return null;
        }
    }

    public String getName(){
        return name;
    }

    public String getSurname(){
        return surname;
    }

}




package DB;

import java.io.IOException;
import java.sql.Connection;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class MyServ
 */
public class MyServ extends HttpServlet{

    private static final long serialVersionUID = 1L;
    private final DBClass dbclass;
    private final String name, surname;

    private final Connection conn;

    public MyServ(){

        dbclass = new DBClass();
        final DBClass db = new DBClass();

        conn =
            db.dbConnect("jdbc:oracle:thin:@elanweb:1510:xxxxx", "xxxxx",
                "xxxxxx");

        name = dbclass.getName();
        surname = dbclass.getSurname();
    }

    @Override
    protected void doGet(final HttpServletRequest request,
        final HttpServletResponse response) throws ServletException,
        IOException{

    }

    @Override
    protected void doPost(final HttpServletRequest request,
        final HttpServletResponse response) throws ServletException,
        IOException{

        System.out.println("MyServer -- " + name + " " + surname);
        response.sendRedirect("http://localhost:8080/DWP/");

    }
}
  • 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-18T06:47:23+00:00Added an answer on May 18, 2026 at 6:47 am

    Surely you mean:

    name = db.getName();
    

    instead of:

    name = dbclass.getName();
    

    You seem to be referencing the wrong variable within the constructor, the one without an open connection (dbclass).

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

Sidebar

Related Questions

I cant understand why in the class constructor I can call this function but
Hey, I am trying to return a user defined class from a web method.
Is it possible to get/set thread name from joinPoint? @Around(call(void methodA())) public Object conditionB(ProceedingJoinPoint
Okay, I'm creating a program from a single class, let's call it John class.
I call from a spreadsheet module a function that does some processing in another
Users call a web service API in my PHP code but don't properly encode
I call this function from with my code-behind: DeleteFile(Server.MapPath(/) + sitemap_index.xml) Public Shared Function
I call a javascript function from a textbox by using OnKeyPress=clickSearchButton() Here is my
I call the following function with a mouseover event but it's not working. My
I call this a flash meeting, but maybe there is another more appropriate name.

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.