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

  • SEARCH
  • Home
  • 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 6913887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:16:32+00:00 2026-05-27T09:16:32+00:00

I am working with a JDBC, How do I actually pass my Connection code

  • 0

I am working with a JDBC, How do I actually pass my Connection code to other objects?? so that it wouldn’t be so much of a hassle to keep on coding it and do I need other objects to create if I need to close a database connection?? Here’s my code

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

import javax.sql.DataSource;

public class DisplayUsers {

    ResultSet resultSet = null;
    Statement statement = null;
    Scanner input = new Scanner(System.in);
    DataSource ds = null;



    public void showAll() {

        System.out.println("Search User: ");
        String user = input.nextLine();

        String query = "Select * from user";
        try {
            resultSet = statement.executeQuery(query);
            ResultSetMetaData metadata = resultSet.getMetaData();
            int columns = metadata.getColumnCount();
            while(resultSet.next()){
                for(int i = 1 ; i<=columns;i++){
                    System.out.printf("%-8s\t",resultSet.getObject(i));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Here’s my Connector code

import java.sql.*;

public class Jdbc {

        public void dbConn(){
            final String url = "jdbc:mysql://localhost:3306/payroll";
            Connection conn = null;
            try{
                 Class.forName("com.mysql.jdbc.Driver");
                 conn = DriverManager.getConnection(url,"root","123192");  
            }catch(Exception e){
                e.printStackTrace();
            }
        }
}
  • 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-27T09:16:33+00:00Added an answer on May 27, 2026 at 9:16 am

    The best way is to use connection pool. If you are coding Java EE application, you can make the pool in configuration (In glassfish you can use admin console). Server will bind this pool (via DataSource) to a JNDI name you specified (say “jndi/mydb”).

    So getting a connection in code is very simple:

    InitialContext ctx = new InitialContext();
    //Application Server will automatically bind new InitialContext() call to its 
    //own context (where your DataSource is located).
    DataSource ds = ctx.lookup("jndi/mydb") 
    

    You can pass this DataSource everywhere and getConnection as simple as ds.getConnection(), never closing it (because it’s pooled and reused).

    P.S. Use of DriverManager as a way of getting connection is appropriate in small console application with no requirements to performance and scalability.

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

Sidebar

Related Questions

In the JDBC code, I have the following that is working with SQL Server:
I am working on a Java app that uses Spring IoC and JDBC Template
Actually I am working on a Java application that requires an ODBC. I created
In the good old JDBC days I wrote a lot of SQL code that
I ve installed MySQL (last update). I need to code, that ll create &
I'm working on OSGI bundle which uses JDBC connection in order to update rows
I have a persistence.xml file using JDBC, working fine, but the code updates the
I'm working with legacy code that uses some convoluted logic to bind IN and
I have been working on a java application that has a connection to a
I've seen and worked with a lot of older, JDBC-based DAO code that usually

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.