Magazine
 
Spring Framework
 

You will get the message ‘Query OK, 0 rows affected’.

Now we have created a table in mysql database with two fields: Name and City.

Setting up the environment variables: As the entire Spring Framework is included in spring.jar. We use it to run our examples.

1 Copy spring.jar from spring1.2.9\dist
folder to the working folder
(springdemo) , mysql-connectorjava-
3.1.6-bin.jar
, also copy commons-logging.jar from apachetomcat- 6.0.10 to the working directory.

2 Set path for jdk1.4.2 and above versions only

3. Now set the classpath as shown:

D:\>springdemo >set classpath=D:\springdemo; D:\springdemo\spring.jar; D:\springdemo\commons-logging.jar; D:\springdemo\ mysql-connector-java-
3.1.6-bin.jar;

4. For a typical Spring Application we need the following files:

i. An interface that defines the functions.
ii. An Implementation that contains properties, its setter and getter methods, functions etc.,
iii. A XML file called Spring configuration file.
iv. Client program that uses the function.


Create the following files

1. datacon.java
2. dataconimpl.java
3. datacon.xml
4. helloclient.java
  1. D:\springdemo\ datacon.java

import javax.sql.*;
public interface datacon{
public DataSource dbcon();
}

2. D:\springdemo\ dataconimpl.java

import org.springframework.jdbc.core.*;
import
org.springframework.jdbc.datasource.*;
import org.springframework.jdbc.object.*;
import
org.springframework.jdbc.support.*;
import javax.sql.*;
public class dataconimpl implements
datacon{
private DataSource dataSource;
public void setDataSource(DataSource ds)
{
dataSource = ds;
}
public DataSource dbcon() {
return dataSource;
}}

3. D:\springdemo\datacon.xml

<? xml version=”1.0" encoding=”UTF-
8"?><!DOCTYPE beans PUBLIC “-//
SPRING//DTD BEAN//EN””http://
www.springframework.org/dtd/springbeans.
dtd”><beans><bean
id=”dataSource”
class=”org.springframework.jdbc.datasource.
DriverManagerDataSource”>
<property name=”driverClassName”>
<value>com.mysql.jdbc.Driver</value>
</property>
<property name=”url”>
<value>
jdbc:mysql://localhost:3333/test
</value>
</property>
<property name=”username”>
<value>root</value>
</property>

Sept 2007 | Java Jazz Up | 56
previous
index
next
 
Pages: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,

30
, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53 , 54, 55, 56, 57,

58
, 59, 60, 61, 62, 63 , 64, 65 , 66 , 67 , 68 , 69 , 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,   Download PDF