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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:13:01+00:00 2026-05-28T19:13:01+00:00

I am trying to connect to a mysql database on my first Spring Project

  • 0

I am trying to connect to a mysql database on my first Spring Project and I seem to be overlooking something really simple.

I have this bean in my application-context.xml file which is commented out!

<bean id="dataSource"
      class="org.springframework.jdbc.datasource.DriverManagerDataSource"
      p:driverClassName="${jdbc.driverClassName}"
      p:url="${jdbc.url}"
      p:username="${jdbc.username}"
      p:password="${jdbc.password}" />

I know this is going to sound stupid, but in order to create a class that connects to a Mysql database using JDBC and the or the Spring JDBCTemplate, what do I do from here?

I am confused about how to populate properties in the bean above, do I do it in a superclass like this and then extend my subclasses from it.

import org.springframework.jdbc.datasource.DriverManagerDataSource;

public class JdbcDao {
     protected DriverManagerDataSource dataSource = new DriverManagerDataSource();
     JdbcDao(){
         dataSource.setDriverClassName("com.mysql.jdbc.Driver");
         dataSource.setUrl("jdbc:mysql://localhost/db_name");
         dataSource.setUsername("bcash");
         dataSource.setPassword("");        
     }    
}

I am confused so any help is truly apprecated,

Many Thanks

  • 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-28T19:13:03+00:00Added an answer on May 28, 2026 at 7:13 pm

    See JdbcTemplate Best Practices for further info.

    Nutshell: no, your DAO wouldn’t create a new datasource via new; that defeats the purpose of using Spring. Implementations should be injected, not instantiated directly; roughly:

    public class TheDaoImpl implements TheDao {
    
        private JdbcTemplate jdbcTemplate;
    
        public void setDataSource(DataSource dataSource) {
            this.jdbcTemplate = new JdbcTemplate(dataSource);
        }
    
        // JDBC-backed implementations of TheDao follow...
    }
    

    Then in your Spring config (if not using annotations), also roughly:

    <bean id="theDao" class="com.bar.plugh.TheDaoImpl" p:dataSource-ref="dataSource" />
    

    (This uses a setter; you could also use an annotation and skip the XML config.)

    You could also use JdbcTemplate as your base class and save more energy.

    I recommend going over the Data access with JDBC reference docs (if you’re using JDBC), or the corresponding section that deals directly with your ORM choice.

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

Sidebar

Related Questions

I am trying to connect to a remote MySQL database using Visual C# 2008
I am trying to connect to a remote mysql database from an iPhone. I
I'm trying to connect to a MySQL 5 database using the MySQL ODBC 5.1
I am trying to run simple perl dbi example script to connect to mysql
I'm trying to connect MySQL database and getting out datas I need it to
I'm trying to connect to MySQL database from node.js using db-mysql library. Everything works
I'm trying to connect Django to a MySQL database which is accessible through an
I am trying to connect to a remote mysql database using django. The documentation
I have setup a mysql database on a linux server on Amazon's EC2. This
I am trying to connect to MySQL database with Java and I get the

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.