摘要:一个使用了DataSource注入的完整AOP例子。
●
IUserDAO.java
完成一个插入和查询方法
package com.kela.spring.jdbc;
public interface IUserDAO {
public void insert(User user);
public User find(Integer id);
}
●
User.java
同上一节
●
UserDAO.java
package com.kela.spring.jdbc;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.sql.DataSource;
import com.kela.spring.util.Util;
public class UserDAO implements IUserDAO {
private DataSource dataSource;
public DataSource getDataSource() {
return dataSource;
}
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
public void insert(User user) {
String name = user.getName();
int age = user.getAge().intValue();
String sql = "INSERT INTO user (name, age) VALUES ('" + name + "', " + age + ")";
//
为解决Mysql5.0的中文问题,转码
sql = Util.GBKToISO(sql);
Connection conn = null;
Statement stmt = null;
try {
conn = dataSource.getConnection();
stmt = conn.createStatement();
stmt.execute(sql);
} catch (SQLException e) {
e.printStackTrace();
} finally {
if(stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
public User find(Integer id) {
String sql = "SELECT * FROM user WHERE id = " + id.intValue();
Connection conn = null;
Statement stmt = null;
try {
conn = dataSource.getConnection();
stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
if(rs.next()) {
Integer i = new Integer(rs.getInt(1));
String name = Util.getStr(rs.getString(2));//
转码
Integer age = new Integer(rs.getInt(3));
User user = new User();
user.setId(i);
user.setAge(age);
user.setName(name);
return user;
}
} catch(SQLException e) {
e.printStackTrace();
} finally {
if(stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
return null;
}
}
●
Beans-config.xml
注入了DataSource的实例,这里使用了DBCP来获得连接池的功能,如果需要其他连接设置,直接在修改该配置文件即可。另外需要,DBCP所需要的.jar文件(commons-dbcp.jar, commons-pool.jar, commons-collections.jar)。
<?xml
version=
"1.0"
encoding=
"UTF-8"
?>
<!DOCTYPE
beans
PUBLIC
"-//SPRING//DTD BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd"
>
<beans>
<bean
id=
"dataSource"
class=
"org.apache.commons.dbcp.BasicDataSource"
destroy-method=
"close"
>
<property
name=
"driverClassName"
>
<value>
com.mysql.jdbc.Driver
</value>
</property>
<property
name=
"url"
>
<value>
jdbc:mysql://127.0.0.1:3306/demo
</value>
</property>
<property
name=
"username"
>
<value>
root
</value>
</property>
<property
name=
"password"
>
<value></value>
</property>
</bean>
<bean
id=
"userDAO"
class=
"com.kela.spring.jdbc.UserDAO"
>
<property
name=
"dataSource"
>
<ref
bean=
"dataSource"
/>
</property>
</bean>
</beans>
●
SpringDAODemo.java
最后是测试类
package com.kela.spring.jdbc;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class SpringDAODemo {
/**
*
测试insert()
*/
public void method_1() {
try {
ApplicationContext context = new FileSystemXmlApplicationContext(
"bin\\beans-config.xml");
User user = new User();
user.setName("kela001");
user.setAge(new Integer(27));
IUserDAO userDao = (IUserDAO) context.getBean("userDAO");
userDao.insert(user);
System.out.println("** OK **");
} catch (Exception e) {
System.out.println("[ERROR]" + e.getMessage());
}
}
public void method_2() {
try {
ApplicationContext context = new FileSystemXmlApplicationContext(
"bin\\beans-config.xml");
User user = new User();
IUserDAO userDao = (IUserDAO) context.getBean("userDAO");
user = userDao.find(new Integer(1));
System.out.println("name:" + user.getName());
System.out.println("age:" + user.getAge());
System.out.println("** OK **");
} catch (Exception e) {
System.out.println("[ERROR]" + e.getMessage());
}
}
public static void main(String[] args) {
SpringDAODemo springDAODemo = new SpringDAODemo();
springDAODemo.method_1();
springDAODemo.method_2();
}
}
●
demo.sql
create table user (
id int(11) not null auto_increment ERIMARY KEY,
name varchar(20),
age int(3));
●
Util.java
测试中用的转码工具类
package com.kela.spring.util;
import java.io.UnsupportedEncodingException;
public class Util {
public static String getStr(String str) {
try {
String temp_p = str;
byte[] temp_t = temp_p.getBytes("ISO8859-1");
//
使用ISO8859-1字符集解码字节的指定数组
String temp = new String(temp_t);
return temp;
} catch(UnsupportedEncodingException ex) {
ex.printStackTrace();
return "";
}
}
/**
*
方法是使用指定的字符集(ISO8859-1)解码指定的字节数组(GBK)
* @param str
* @return
*/
public static String GBKToISO(String str) {
if(str == null) {
str = "";
} else {
try {
str = new String(str.getBytes("GBK"), "ISO8859-1");
} catch (Exception e) {
e.printStackTrace();
}
}
return str;
}
}