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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T21:08:45+00:00 2026-06-11T21:08:45+00:00

I am implementing a code that uses JDBC driver. Below is the code that

  • 0

I am implementing a code that uses JDBC driver.

Below is the code that I made.

public class MysqlUtils {
    public Connection conn;
    public ResultSet rs;
    public PreparedStatement stmt;

    public MysqlUtils(String address, String id, String passwd) {
        try {
            conn = DriverManager.getConnection(address, id, passwd);
            stmt = null;
            rs = null;
        } catch (SQLException e) {
            // error management
        }
    }

    public void getSomeData(long id) {
        try {
            stmt = conn.prepareStatement("SELECT * FROM some_table");
            rs = stmt.executeQuery();
            rs.next();
            System.out.println(rs.getString("some_column");
        } catch (SQLException e) {
            // error management
        }
    }
}

I have declared Connection conn, ResultSet rs, PreparedStatement stmt as member variables because somehow I thought that might help me enhance performance.

I have a couple of questions.

  1. If I call getSomeData() consecutively, will stmt and rs be assigned new objects every time?

  2. Regardless of the answer to the question above, if I run this code in a multi-threaded environment(multiple threads using MysqlUtils class), will there be a mix-up because I didn’t declare ResultSet rs in getSomeData()?

  3. Was declaring Connection conn, ResultSet rs, PreparedStatement stmt as member variables a bad choice? In other words, is my implementation of JDBC a viable one?

Thanks for the help.

  • 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-11T21:08:46+00:00Added an answer on June 11, 2026 at 9:08 pm
    1. Yes. The method will be executed, and thus stmt and rs will take new values. Of course, you might have multiple instances of your class, and thus multiple instances of those two fields.
    2. Yes. This code is completele thread-*un*safe. Public fields in general should almost always be avoided. Especially in a multi-threaded environment
    3. Yes, it’s a bad choice. The scope of a variable should be as small as possible. And these variables are used in a single method, and reassigned every time. They should be local variable.

    Also:

    • a method getSomeData() should return something, and not just printing something
    • the ResultSet and the Statement should be closed, in a finally block
    • I hope the error management doesn’t consist in swallowing the exception

    I would advise using spring-jdbc, which takes care of all the plumbing code for you, and avoids all the problems your code currently has.

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

Sidebar

Related Questions

I've the following code for implementing zoom image in Android: public class MyActivity extends
When implementing the Strategy Pattern, where does one put the code that determines which
I have the code below implementing a NON-Blocking TCP acceptor. Clients are able to
I am implementing an application that uses websockets and a console accessed via Telnet.
You are implementing an ASP.NET application that uses data-bound GridView controls in multiple pages.
We have developed a system that uses a single code base, consisting of four
I'm implementing a C# desktop application that uses SQL server database. I have this
The Scenario: I'm implementing an FTP get functionality in my application, that uses Qt
Is it possible in Java to create a static factory method/class that uses an
I am implementing an app that uses the native language settings which can be

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.