免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 993 | 回复: 0
打印 上一主题 下一主题

spring演示 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-01-09 08:24 |只看该作者 |倒序浏览
此为spring演示,其实spring的好处不只这一点,如果运用得当,可胜百万代码哦
  1. package com.org.spring.demo;
  2. import javax.sql.DataSource;
  3. import java.sql.Connection;
  4. import java.sql.ResultSet;
  5. import java.sql.Statement;
  6. public class DataBean
  7. {
  8.     private DataSource dataSource;
  9.     private Connection con;
  10.     private ResultSet rs;
  11.     private Statement stmt;
  12.    
  13.     public void setDataSource(DataSource dataSource)
  14.     {
  15.         this.dataSource = dataSource;
  16.     }
  17.    
  18.     public void testDataSource()
  19.     {
  20.         try
  21.         {
  22.             con = dataSource.getConnection();
  23.             if(con != null)
  24.             {
  25.                 System.out.println("成功获取数据源");
  26.                 stmt = con.createStatement();
  27.                 rs = stmt.executeQuery("SELECT * FROM friends");
  28.                 while(rs.next())
  29.                 {
  30.            
  31.       
  32. System.out.println("编号->"+rs.getInt("id"));
  33.            
  34.       
  35. System.out.println("姓名->"+rs.getString("name"));
  36.            
  37.       
  38. System.out.println("性别->"+rs.getString("sex"));
  39.            
  40.       
  41. System.out.println("年龄->"+rs.getInt("age"));
  42.            
  43.       
  44. System.out.println("电话->"+rs.getString("phone"));
  45.            
  46.       
  47. System.out.println("地址->"+rs.getString("address"));
  48.            
  49.       
  50. System.out.println("备注->"+rs.getString("memo"));
  51.                 }
  52.                 stmt.close();
  53.                 rs.close();
  54.                 //偷懒不往finally中写了
  55.             }
  56.             else
  57.             {
  58.                 System.out.println("未能获取数据源");
  59.             }
  60.         }
  61.         catch(Exception err)
  62.         {
  63.             err.printStackTrace();
  64.         }
  65.         finally
  66.         {
  67.             try
  68.             {
  69.                
  70.                 if(con != null)
  71.                 {
  72.                     con.close();
  73.                     System.out.println("释放连接");
  74.                 }
  75.             }
  76.             catch(Exception err)
  77.             {
  78.                 err.printStackTrace();
  79.             }
  80.         }
  81.     }
  82. }
复制代码
  1.    
  2.         
  3.             org.gjt.mm.mysql.Driver
  4.         
  5.         
  6.             jdbc:mysql://IP地址/数据库名
  7.         
  8.         
  9.             用户名
  10.         
  11.         
  12.             密码
  13.         
  14.    
  15.    
  16.         
  17.             
  18.         
  19.    
复制代码
  1. package com.org.spring.demo;
  2. import org.springframework.beans.factory.BeanFactory;
  3. public class DataClient
  4. {
  5.     private void showDataSource()
  6.     {
  7.         try
  8.         {
  9.             Resource resource = new ClassPathResource("datasource.xml");
  10.             BeanFactory factory = new XmlBeanFactory(resource);
  11.             DataBean data = (DataBean)factory.getBean("dataBean");
  12.             data.testDataSource();
  13.         }
  14.         catch(Exception err)
  15.         {
  16.             err.printStackTrace();
  17.         }
  18.     }
  19.    
  20.     public static void main(String args[])
  21.     {
  22.         new DataClient().showDataSource();
  23.     }
  24. }
复制代码
               
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/460/showart_66298.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP