Kela's Blog

                      前面的路很坎坷,但畢竟是條路.也許走過這一段就會發(fā)現(xiàn),走過去就是夢想中的地方.因此堅持成為此刻唯一能做且必須去做的事情.
          posts - 9, comments - 27, trackbacks - 0, articles - 15

          kela的筆記 應用程序框架 ---- spring(9)

          Posted on 2006-11-09 10:32 Kela 閱讀(243) 評論(0)  編輯  收藏 所屬分類: 我的筆記(Spring)

          摘要:一個使用了DataSource注入的完整AOP例子。

          ?

          ???? IUserDAO.java

          ?

          完成一個插入和查詢方法

          package com.kela.spring.jdbc;

          ?

          public interface IUserDAO {

          ???

          ??? public void insert(User user);

          ??? public User find(Integer id);

          }

          ?

          ???? User.java

          同上一節(jié)

          ???? 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字符集解碼字節(jié)的指定數(shù)組

          ??????????? String temp = new String(temp_t);

          ??? ??????? return temp;

          ??????? } catch(UnsupportedEncodingException ex) {

          ??????????? ex.printStackTrace();

          ??????????? return "";

          ??????? }

          ??? }

          ???

          ??? /**

          ??? ?* 方法是使用指定的字符集(ISO8859-1)解碼指定的字節(jié)數(shù)組(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;

          ??? }

          }

          ?

          分享到:
          主站蜘蛛池模板: 长宁县| 西藏| 汤阴县| 大方县| 抚松县| 图片| 武城县| 施秉县| 庐江县| 兴国县| 景谷| 武胜县| 哈密市| 古浪县| 福安市| 鄂托克前旗| 资兴市| 方山县| 深圳市| 抚顺县| 青河县| 石楼县| 开鲁县| 阳原县| 长泰县| 丰县| 神池县| 舟曲县| 察哈| 柞水县| 屯昌县| 固始县| 富民县| 佳木斯市| 延吉市| 明光市| 昌都县| 开封市| 盖州市| 湖南省| 民丰县|