A Spring MySQL BasicDataSource connection example

Spring MySQL FAQ: Can you share a Java Spring MySQL example, showing how to create a Spring Framework application context file so a Java standalone application can connect to a MySQL database?

Sure, here's a Java/Spring MySQL example, specifically showing a Spring application context file that sets up a BasicDataSource (connection) to let your Java application connect to a MySQL database.

The Spring MySQL application context file

Here's the source code for the Spring MySQL application context file (which I named applicationContext.xml):

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>

  <bean id="fileEventType" class="com.devdaily.springtest1.bean.FileEventType">
    <property name="eventType" value="10"/>
    <property name="description" value="A sample description here"/>
  </bean>

  <bean id="fileEventDao" class="com.devdaily.springtest1.dao.FileEventDao">
    <property name="dataSource" ref="basicDataSource"/>
  </bean>

  <bean id="basicDataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost/my_database" />
    <property name="username" value="my_username" />
    <property name="password" value="my_password" />
    <property name="initialSize" value="3" />
    <property name="maxActive" value="10" />
  </bean>

</beans>

The Spring Java/MySQL source code

And here's the source code for a Java class (a Java standalone application) which demonstrates how to load this Spring MySQL JDBC application context file:

package com.devdaily.springtest1;

import com.devdaily.springtest1.dao.FileEventDao;
import com.devdaily.springtest1.bean.FileEventType;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringApplicationContextExample
{

  public static void main (String[] args)
  {
    new Main();
  }

  public SpringApplicationContextExample()
  {
    // open/read the application context file
    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");

    // instantiate our spring dao object from the application context
    FileEventDao fileEventDao = (FileEventDao)ctx.getBean("fileEventDao");

    // create a FileEventType object from the application context
    FileEventType fileEventType = (FileEventType)ctx.getBean("fileEventType");

    // insert the file event with the spring dao
    fileEventDao.doInsert(fileEventType);
  }

}

More information on this Spring MySQL JDBC basicDataSource example

I'm going to assume that you came here already knowing about Spring, MySQL, JDBC, and application context files, so I'm not going to explain this any more in this article. But if you are looking for an explanation of how this works, fear not, I've written a Spring application context tutorial (standalone application) that discusses how this exact code works. Please visit that page for more information.