19
Dec
09

database connectivity using spring framework


This page describes the process of setting up database connectivity using a spring framework based application. We are going to use the DriverManagerDataSource provided by the spring framework to get things started.

As stated by its javadoc:

Useful for test or standalone environments outside of a J2EE container, either as a DataSource bean in a corresponding ApplicationContext or in conjunction with a simple JNDI environment.

In other words this is a quick an dirty way of doing it. This is not production ready. In order to make it production ready you need to use database connection pools provided by your Web application container.

Driver Manager Names and Connection URL’s

In order to connect to databases you need one or more of the following items:

  • Driver Manager Class Name
  • URL
  • username
  • password

In some databases all this information is part of the URL in that case you only need a URL. It depends on the database vendor implementation.

Continue Reading about connection pools

Refer to the following page in order to setup a connection pool for quicker database access.

Database Connection Strings

The following is a list of popular databases and their connection strings.

mySQL Connection String

spring configuration

	<bean name="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
		<property name="url" value="jdbc:mysql://hostname:3306/dbname"/>
		<property name="username" value="username"/>
		<property name="password" value="xxxxx"/>
	</bean>

	<bean name="jdbcTemplate" class="org.springframework.jdbc.core.simple.SimpleJdbcTemplate">
		<constructor-arg><ref bean="dataSource"/></constructor-arg>
	</bean>

Maven dependency

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.9</version>
</dependency> 

Sybase

Driver: com.sybase.jdbc.SybDriver
URL: jdbc:sybase:Tds:hostname:port/dbname

Oracle

Driver: oracle.jdbc.driver.OracleDriver
URL: jdbc:oracle:thin:myusername/mypassword@//localhost:1521/DBNAME
URL: jdbc:oracle:thin:@localhost:1521:MyDatabase

DB2

com.ibm.db2.jcc.DB2Driver
jdbc:db2://hostname:port/TEST

Microsoft SQL Server

Driver: com.microsoft.jdbc.sqlserver.SQLServerDriver
URL: jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=northwind

Informix

URL: jdbc:informix-sqli://hostname:portnumber/MyDatabase:INFORMIXSERVER=MyServerName
Driver: com.informix.jdbc.IfxDriver

postgresql

URL: jdbc:postgresql:template1
Driver: org.postgresql.Driver

Derby

URL (Network): jdbc:derby://hostname:1527/sample;user=username;password=password
URL (FileSystem): jdbc:derby://hostname:1527/c:/directory/dbname;user=username;password=password
Driver: org.apache.derby.jdbc.ClientDriver

HSQLDB

URL (InMemory): jdbc:hsqldb:mem:aname
URL (File system): jdbc:hsqldb:file:aname
Driver: org.hsqldb.jdbcDriver

Advertisements

3 Responses to “database connectivity using spring framework”


  1. July 23, 2013 at 3:22 am

    Thanx for such a nice tutorial. Please post tutorial for how to create pojo class for spring.


Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 74 other followers

December 2009
S M T W T F S
« Nov   Jan »
 12345
6789101112
13141516171819
20212223242526
2728293031  

Blog Stats

  • 801,397 hits

%d bloggers like this: