<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Kela's Blog

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

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

    Posted on 2006-11-09 10:32 Kela 閱讀(246) 評論(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

    同上一節

    ???? 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;

    ??? }

    }

    ?

    分享到:
    主站蜘蛛池模板: 最近免费中文在线视频| 国产成人+综合亚洲+天堂| 伊人久久大香线蕉免费视频| 久久久久国产精品免费网站| 亚洲精品无码专区2| 老妇激情毛片免费| 热久久这里是精品6免费观看| 亚洲高清无码专区视频| 在线观看亚洲精品专区| 国产99视频精品免费视频7| 亚洲AV一区二区三区四区| 日韩一区二区三区免费体验| 黄网址在线永久免费观看| 亚洲日日做天天做日日谢| 久久国产乱子伦精品免费午夜| 又黄又爽又成人免费视频| 亚洲精品无码久久久久久久| 亚洲高清中文字幕免费| 国内精品久久久久影院亚洲| 国产高清免费观看| 一本岛v免费不卡一二三区| 亚洲精品无码AV人在线播放| 免费国产叼嘿视频大全网站| 久久亚洲精品人成综合网| 中文字幕人成无码免费视频| 亚洲精品中文字幕无码A片老| 一本色道久久88亚洲综合| 亚洲人成人77777在线播放 | 巨胸喷奶水www永久免费| 人人狠狠综合久久亚洲88| 四虎最新永久免费视频| 亚洲欧美日韩中文高清www777 | 希望影院高清免费观看视频| 亚洲精品人成网线在线播放va | 久久亚洲国产精品| 特级毛片aaaa免费观看| 亚洲av无码一区二区三区乱子伦| 污视频网站在线观看免费| 亚洲国产精品成人精品无码区| 日韩在线免费视频| 国产黄在线观看免费观看不卡|