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

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

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

    Kela's Blog

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

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

    ?

    ???? IUserDAO.java

    ?

    完成一個(gè)插入和查詢方法

    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的中文問題,轉(zhuǎn)碼

    ??????? 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));// 轉(zhuǎn)碼

    ??????????????? 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的實(shí)例,這里使用了DBCP來獲得連接池的功能,如果需要其他連接設(shè)置,直接在修改該配置文件即可。另外需要,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

    ?

    測試中用的轉(zhuǎn)碼工具類

    ?

    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;

    ??? }

    }

    ?

    分享到:
    主站蜘蛛池模板: 最近最好的中文字幕2019免费| 免费不卡在线观看AV| 中国好声音第二季免费播放| 成人免费区一区二区三区| 37pao成人国产永久免费视频| 免费人成在线视频| 亚洲成a人片在线观看国产| 国产亚洲综合久久系列| 91亚洲va在线天线va天堂va国产| 亚洲无人区码一二三码区别图片 | 久久精品国产亚洲AV嫖农村妇女| 亚洲av无码电影网| 成人免费在线观看网站| 午夜亚洲国产成人不卡在线| 国产精品亚洲а∨无码播放| 亚洲jjzzjjzz在线观看| 一级做a爰黑人又硬又粗免费看51社区国产精品视 | 亚洲AV永久无码精品水牛影视| 亚洲男人天堂影院| 久久久久久亚洲精品无码| 久久国产精品免费一区二区三区| 久久久久久精品免费免费自慰| 国产禁女女网站免费看| 久久亚洲国产精品一区二区| 精品亚洲AV无码一区二区| 一级特黄aaa大片免费看| 7723日本高清完整版免费| av无码东京热亚洲男人的天堂| 亚洲人成电影在线天堂| 亚洲av乱码一区二区三区按摩 | 精品国产免费一区二区三区香蕉| 久久精品无码一区二区三区免费 | 亚洲人成无码久久电影网站| 亚洲成在人线中文字幕| 一级做a爰全过程免费视频毛片| 亚洲一级毛片免费看| 久久久久亚洲爆乳少妇无 | 国产亚洲精品仙踪林在线播放| 亚洲精品免费视频| 亚洲 自拍 另类小说综合图区 | 亚洲av永久无码|