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

The Archive Base Latest Questions

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

So I just finished learning JDBC this weekend and have successfully transferred my code

  • 0

So I just finished learning JDBC this weekend and have successfully transferred my code from the main method into an MVC app. The purpose of this application is to hold a roster of players and display a users credentials if requested. The program works great and when I request a url like…

http://localhost:8084/gmustudent/players?id=1

I get the correct output for that player! The problem is I am performing the database connection within my PlayersDAO class and I assume that this is not the “best” way to do this. So I have two questions.

  1. Is there a way to perform the database connection within the web.xml
    file or some other file so that when the server is initially started it will
    immediately perform the connection to the database and be ready to
    query when asked?
  2. And is this actually a better alternative to having the connection in the DAO or would this have unforeseen negative drawbacks. AKA would a constant connection to my database be exactly what I do not want?

Any comments or links would be greatly appreciated. And I’ll share the code I currently have for my DAO class so that you can see what I have so far. Thank you all!

package com.jdbc.test;

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

public class PlayersDAO 
{
    public static Players viewPlayer(int id) throws SQLException
    {
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;
        Players playerObject = null;

        try
        {
            String url = "jdbc:mysql://localhost:3306/gmustudent";
            String username = "root";
            String password = "root";

            try
            {
                Class.forName("com.mysql.jdbc.Driver");
            }
            catch(ClassNotFoundException error)
            {
                System.out.println("Error: " + error.getMessage());
            }

            connection = DriverManager.getConnection(url, username, password);
            statement = connection.createStatement();
            resultSet = statement.executeQuery("SELECT * FROM players WHERE id = " + id);

            if(resultSet.next())
                playerObject = new Players(resultSet.getLong("id"), resultSet.getString("name"), resultSet.getString("position"), resultSet.getString("height"), resultSet.getString("year"), resultSet.getString("hometown"), resultSet.getString("highschool"), resultSet.getString("headshot"));   
        }
        finally
        {
            if (connection != null) try{connection.close();} catch(SQLException ignore) {}
            if (statement != null) try{statement.close();} catch(SQLException ignore) {}
            if (resultSet != null) try{resultSet.close();} catch(SQLException ignore) {}
        }

        return playerObject;
    }
}
  • 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-12T16:52:24+00:00Added an answer on June 12, 2026 at 4:52 pm

    You can create database connection in web.xml file by using resource. I hope this tutorial will help you.

    http://viralpatel.net/blogs/database-connection-pooling-tomcat-eclipse-db/

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

Sidebar

Related Questions

I am learning spring mvc and hibernate. i have just finished simple site with
I'm just getting started learning CoreData for iOS and have written some exploratory code
I have just finished learning application development on android platform, as In matter of
Just finished reading this blog post: http://www.skorks.com/2010/03/an-interview-question-that-prints-out-its-own-source-code-in-ruby/ In it, the author argues the case
I just finished reading the HTML5 Developer's Cookbook and have a question. I've read
I just finished this script it works well, but I am struggling to pass
I just finished looking at this question: https://stackoverflow.com/questions/753122/which-cloud-computing-platform-should-i-choose But, I am not certain what
I'm learning WPF and started with this MSDN tutorial. I was just following the
I just finished learning assembly language. But I couldn't understand what could I implement
Have just finished my first web based application using Ruby on Rails. It was

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.