Hi friends in this example I am
going to describe Spring Basic JDBC example with Spring2.5. No more complexity
here.
Here is my project packaging stucture.
Here I have
configured org.apache.commons.dbcp.BasicDataSource
in my spring bean configuration file, this BasicDataSource belongs to apache. This is depends on driverClassName, connection url, userName
and password to create the
connection between java and Oracle DB.
Here is my applicationContext.xml spring bean configuration
file. Here you may need to change the values as per your environment.
applicationContext.xml
<?xml version="1.0"
encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="basicDataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName">
<value>oracle.jdbc.driver.OracleDriver</value>
</property>
<property name="url">
<value>jdbc:oracle:thin:@localhost:1521:XE</value>
</property>
<property name="username">
<value>lovababu</value>
</property>
<property name="password">
<value>********</value>
</property>
</bean>
</beans>
SpringJDBCTest.java
Here is may SpringJDBCTest.java class, nothing
is new here, if you are already familiar with traditional JDBC. I have already configured my basicDataSource
through spring configuration file, so spring container take care of my
datasource creation I no need to worry about datasource creation here.
Simple I am getting the connection from the basicDataSource
and creating statement and loading the STUDENT data from table.
Here is the Sql Script to create table and insert records.
CREATE TABLE STUDENT1
(
STD_NO NUMBER NOT NULL
, STD_NAME VARCHAR2(20
BYTE) NOT NULL
, AGE NUMBER NOT NULL
, CONSTRAINT STUDENT_PK1
PRIMARY KEY (STD_NO) ENABlE
);
INSERT INTO STUDENT
VALUES(1213 , 'somename' , 8);
package
com.lova.springjdbc.test;
import java.sql.Connection;
import java.sql.ResultSet;
import
java.sql.SQLException;
import java.sql.Statement;
import
javax.sql.DataSource;
import
org.springframework.context.ApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author Lovababu
*
*/
public class SpringJDBCTest
{
private static ApplicationContext context = null;
static
{
context = new
ClassPathXmlApplicationContext("applicationContext.xml");
}
public static void main(String[] args)
{
DataSource dataSource =
(DataSource) context.getBean("basicDataSource");
try
{
Connection connection =
dataSource.getConnection();
Statement statement =
connection.createStatement();
ResultSet rs =
statement.executeQuery("SELECT * FROM STUDENT");
while(rs.next())
{
System.out.println("Row "+ rs.getRow());
System.out.println(" Student NO:"+rs.getInt(1) + " Name:"+rs.getString(2)+ " Age:"+rs.getInt(3));
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
Jar Required: Spring 2.5 version I used for this example.
- spring-beans.jar
- spring-context.jar
- spring-core.jar
- commons-attributes-api.jar
- commons-attributes-compiler.jar
- commons-logging.jar log4j-1.2.14.jar
- ojdbc14.jar
- org.apache.commons.dbcp.jar
- org.apache.commons.pool.jar
Thank YOU,
please leave your comment J