享受生活

       如同草籽破粒迎来了春天,走出高中校门的我开始了漫无目的的思考,伴随着九月,我来到了大学。
开始那段时间,大家都不理解报个这个偏僻的学校干什么…
但是我还是义无反顾的孤身来了,远离了我生活三年的城市,也远离了那段如火般的心情。
记忆中很多人现在都已模糊不清,大多数的人都转化为了一串数字,无论是qq还是手机。
生活还是要继续,选择奋斗的活着,让自己少一些遗憾,只是这是一个婆娑世界,婆娑即遗憾。
虽然我一再躲避,但是你们还是不可避免的闯入我的心扉,让我也更多了几分牵挂,对这个尘世也多了几分念想,只是谁知是劫是缘?
喜欢怎么样的生活…
    总的来说我人生中最精彩的年华是我的初中,无它,第一次品味喜欢、第一次唱歌、第一次有了真正的朋友、第一次对学习充满信心、第一次上光荣榜、第一次明白自身以外的世界、第一次懂得坚持、第一次遇见地震,总之太多的第一次体验,给了我太多的牵挂,感谢手把手教我写abc的班主任,感谢和我亦师亦友的历史老师,感谢我的第一次喜欢,即便是没有结果。初中最喜欢的莫过于读书,早上书店八点开门,我则吃过早饭七点半去门口守着,只盼着能帮书店主人做点事,也好过白白的看一天书。中午不吃饭,开始父母因为这还来找过我几次,几次无果,便也只好听而任之,晚上九点关门,我九点十分踏上回家的路,手里还拿着两本没看完的书,那个时候真是什么书都看,乱七八糟的,经史子集、散文诗歌、古典哲学、经济法律、武侠玄幻,眼睛也就是在那个时候看的不行的。喜欢那种纵横肆意的感觉,只是人活在世上,大多心为形役,我也不例外。

        简洁的调调
        大学里的我趋于成熟,然却更近于幼稚。也曾徘徊于湖边,看一番风景,确是未曾想起,不知不觉中心里已经烙下了你的痕迹,只是泥上偶然留指爪,鸿飞那复计东西。
在渐行渐远的人生旅途上,总会错过一些别样的风景。
我想,关于我的青春,用一个词足以概况:何须执着。

一些小诗

黄昏的小路 –汪国真
 我们总是在黄昏
放慢了脚步
踏上了小路
小路好长好长
仿佛永远没有结尾
只有序幕

没有一条道路
我们能走得这样耐心
这样幸福
走了很远很远
小路依然如故
你我却已不是当初

下面是自己高中写的东西,偶然回想起,也是一阵涟漪。
婆娑的叶子在脚下
留下了片片黑暗
青石黄叶林荫道
鸽子在天际咕咕
硬木长椅上
太阳落山了

还有一个类似的是写给某个人的,时间差不多模糊了。
致..
你我之初见
似不经意间邂逅
小城凌乱的林荫道
只顾前行
未想撞个满怀
继而无言
怀念不如不见

                                          黍离
                                       www.clenji.com

相遇在人海

相遇在人海
人海茫茫
只是那一眸
宛若流星
前世是什么  模样
竟要今生遥遥相望
蓦然,记忆掠过过往
这年华,从不曾彷徨
是否,只剩下暗香
                                        黍离2014.11.11
                                        www.clenji.com

三天搞定java中JDBC连接数据库(第二天)

上午复习

import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
 
import org.apache.commons.beanutils.PropertyUtils;
import org.junit.Test;
 
public class TestReview {
    //通用的查询方法
    public <T> List<T> getAll(String sql, Class<T> clazz){
         List<T> list = new ArrayList<T>();
         
          //1. 获取连接
         Connection conn = null;
          //2. 获取Statement对象,用于发送SQL
         Statement stam = null;
          //3. 执行SQL,并获取结果集 ResultSet
         ResultSet rs = null;
          try {
             conn = JDBCUtil. getConnection();
             
             stam = conn.createStatement();
             
             rs = stam.executeQuery(sql);
             
              //4. 获取结果集的元数据 ResultSetMetaData
             ResultSetMetaData rsmd = rs.getMetaData();
              //4.1 获取结果集的列数
              int columnCount = rsmd.getColumnCount();
              //5. 根据元数据获取结果集中的数据,并封装进对象
              while(rs.next()){
                 T t = clazz.newInstance();
                  for (int i = 0; i < columnCount; i++) {
                       //5.1获取列的别名
                      String columnName = rsmd.getColumnLabel(i+1);
                      
                       //5.2 根据列名获取值
                      Object columnVal = rs.getObject(columnName);
                       //5.3 封装进对象
//                    Field field = clazz.getDeclaredField(columnName);
//                    field.setAccessible(true);
//                    field.set(t, columnVal);
                      
                      PropertyUtils. setProperty(t, columnName, columnVal);
                 }
                 list.add(t);
             }
         } catch (Exception e) {
             e.printStackTrace();
         } finally {
              //6. 关闭连接
             JDBCUtil. close(rs, stam, conn);
         }
          return list;
    }
    
    @Test
    public void testGet(){
//       String sql = “select id, name, email, birth from customers where id = 22”;
//       Customer cus = get(sql , Customer.class);
//       System.out.println( cus);
         
         String sql = “select order_id orderId, order_name orderName, order_date orderDate from `order` where order_id = 1”;
         Order order = get(sql, Order. class);
         System. out.println(order);
         
    }
 
    /*
     * 需要改动的位置:
     *       1.结果集处理
     *           问题:
     *               ①不能明确有几列
     *               ②不能明确有列名
     *           解决: 使用 ResultSetMetaData : 结果集的元数据
     *               ①getColumnCount() : 获取结果集的列数
     *               ②getColumnName( int index) : 根据列的索引位置,获取列名
     *                getColumnLabel( int index) : 根据列的索引位置,获取列的别名
     *
     *       2.Customer对象的位置  
     *           问题:
     *               ①不能明确创建什么对象
     *           解决:
     *               ①利用反射,在运行时(调用方法),确定创建什么对象
     *               ②利用泛型,在运行时(调用方法),确定返回什么类型的对象的类型
     */
    public <T> T get(String sql, Class<T> clazz){//(sql, Customer.class)
         T t = null;
         
         Connection conn = null;
         Statement stam = null;
         ResultSet rs = null;
          try {
             conn = JDBCUtil. getConnection();
             
             stam = conn.createStatement();
             
             rs = stam.executeQuery(sql);
             
              //获取结果集的元数据
             ResultSetMetaData rsmd = rs.getMetaData();
             
              //获取结果集的列数
              int columnCount = rsmd.getColumnCount();
             
              if(rs.next()){
 
                 t = clazz.newInstance();
                 
                  for (int i = 0; i < columnCount; i++) {
//                    //1. 获取列的名称
//                    String columnName = rsmd.getColumnName(i+1);
                      
                       //1. 获取列的别名
                      String columnName = rsmd.getColumnLabel(i+1);  //注意: 获取结果集中(相当于数据表)列的名称(别名)
                      
                       //2. 根据列名获取对应数据表中的数据
                      Object columnVal = rs.getObject(columnName);
                      
                       //3. 将数据表中得到的数据,封装进对象
                      Field field = clazz.getDeclaredField(columnName); //注意:反射根据Java中类的属性获取 Field对象
                      field.setAccessible( true);
                      field.set(t, columnVal);
                 }
                 
             }
         } catch (Exception e) {
             e.printStackTrace();
         } finally {
             JDBCUtil. close(rs, stam, conn);
         }
          return t;
    }
    
    @Test
    public void test3(){
         String sql= “select * from customers where id>0”;
         List<Customer> list=getAllCustomers(sql);
 
         Iterator<Customer> it=list.iterator();
          while(it.hasNext()){
         System. out.println(it.next());
         }
         
    }
    //通用的查询方法
    public List<Customer> getAllCustomers(String sql){
         List<Customer> list= new ArrayList<Customer>();
         
         Connection conn= null;
         Statement stam= null;
         ResultSet rs= null;
          try {
             conn = JDBCUtil. getConnection();
             stam = conn.createStatement();
             rs = stam.executeQuery(sql);
              while(rs.next()){
                  int id=rs.getInt(“id” );
                 String name=rs.getString( “name”);
                 String email=rs.getString(“email” );
                 Date birth=rs.getDate( “birth”);
                 
                 Customer cus= new Customer(id,name,email,birth);
                 list.add(cus);
             }
         } catch (Exception e) {
             e.printStackTrace();
         } finally{
             JDBCUtil. close(rs, stam, conn);
         }
          return list;
    }
    
    
    
    @Test
    public void test2(){
         String sql= “select * from customers where id=2”;
         Customer cu=getCustomers(sql);
         System. out.println(cu);
         
    }
    
    //适用于customers的查询
    
    public Customer getCustomers(String sql){
         Customer cus= null;
         Connection conn= null;
         Statement stam= null;
         ResultSet rs= null;
          try {
             conn = JDBCUtil. getConnection();
             stam = conn.createStatement();
             rs = stam.executeQuery(sql);
              if(rs.next()){
                  int id=rs.getInt(“id” );
                 String name=rs.getString( “name”);
                 String email=rs.getString(“email” );
                 Date birth=rs.getDate( “birth”);
                 
                 cus= new Customer(id,name,email,birth);
             }
         } catch (Exception e) {
             e.printStackTrace();
         } finally{
             JDBCUtil. close(rs, stam, conn);
         }
          return cus;
    }
    
    @Test
    public void test1(){
         String sql= “update customers set name=’李逵’ where id=27” ;
          int x=update(sql);
         System. out.println(“受影响行数:” +x);
    }
    
    //通用的增删改方法
    public int update(String sql){
          //1.获取连接
         Connection conn= null;
          //2.获取statement对象用于发送SQL语句
         Statement stam= null;
          int row=0;
          try {
             conn = JDBCUtil. getConnection();
             stam = conn.createStatement();
              //3.执行sql
             row=stam.executeUpdate(sql);
         } catch (SQLException e) {
              // TODO Auto-generated catch block
             e.printStackTrace();
         } catch (Exception e) {
              // TODO Auto-generated catch block
             e.printStackTrace();
         } finally{
              //4.关闭连接
             JDBCUtil. close(null,stam, conn);
         }
          return row;
    }
 
————-
import java.sql.Date;
 
public class Customer {
    private int id ;
    private String name;
    private String email;
    private Date birth;
 
    public Customer() {
    }
 
    public Customer( int id, String name, String email, Date birth) {
          this.id = id;
          this.name = name;
          this.email = email;
          this.birth = birth;
    }
 
    public int getId() {
          return id ;
    }
 
    public void setId(int id) {
          this.id = id;
    }
 
    public String getName() {
          return name ;
    }
 
    public void setName(String name) {
          this.name = name;
    }
 
    public String getEmail() {
          return email ;
    }
 
    public void setEmail(String email) {
          this.email = email;
    }
 
    public Date getBirth() {
          return birth ;
    }
 
    public void setBirth(Date birth) {
          this.birth = birth;
    }
 
    @Override
    public String toString() {
          return “Customer [id=” + id + “, name=” + name + “, email=” + email
                 + “, birth=” + birth + “]”;
    }
 
————–
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
 
 
 
public class JDBCUtil {
 
    public static Connection getConnection() throws Exception{
         Properties pros= new Properties();
         pros.load( new FileInputStream(“jdbc.properties” ));
         String driverClass=pros.getProperty(“driverClass” );
         String url=pros.getProperty( “url”);
         String user=pros.getProperty( “user”);
         String password=pros.getProperty(“password” );
         
         
          //1.加载驱动
         Class. forName(driverClass);
         
          //2.获取连接
         Connection conn=DriverManager.getConnection(url, user, password);
          return conn;     
    }
    
    public static void close(ResultSet r,Statement s,Connection c){
          try {
              if(s!=null){
                 s.close();
             }
         } catch (SQLException e) {
             e.printStackTrace();
         }
          try {
              if(c!=null){
                 c.close();
             }
         } catch (SQLException e) {
             e.printStackTrace();
         }
          try {
              if(r!=null){
                 r.close();
             }
         } catch (SQLException e) {
             e.printStackTrace();
         }
    }
 
}
 
 
——————-
上午学习preparedstatement
——————-
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
 
import org.apache.commons.beanutils.PropertyUtils;
import org.junit.Test;
 
public class TestPreparedStatement {
    @Test
    public void testGet(){
         String sql = “select order_name orderName, order_date orderDate from `order` where order_id = ?”;
         Order cus = get(sql, Order. class, 2);
         System. out.println(cus);
    }
    
    //2. 使用 PreparedStatement 完成通用的查询方法
    public <T> T get(String sql, Class<T> clazz, Object … args){
         T t = null;
         
         Connection conn = null;
         PreparedStatement ps = null;
         ResultSet rs = null;
          try {
             conn = JDBCUtil. getConnection();
             
             ps = conn.prepareStatement(sql);
             
              for (int i = 0; i < args.length; i++) {
                 ps.setObject(i+1, args[i]);
             }
             
             rs = ps.executeQuery();
             
             ResultSetMetaData rsmd = rs.getMetaData();
             
              int columnCount = rsmd.getColumnCount();
             
              while(rs.next()){
                 t = clazz.newInstance();
                 
                  for (int i = 0; i < columnCount; i++) {
                      String columnName = rsmd.getColumnLabel(i+1);
                      Object columnVal = rs.getObject(columnName);
                      
                      PropertyUtils. setProperty(t, columnName, columnVal);
                 }
             }
         } catch (Exception e) {
             e.printStackTrace();
         } finally {
             JDBCUtil. close(rs, ps, conn);
         }
         
          return t;
    }
    
    
    @Test
    public void testUpdate(){
//       String sql = “update customers set name = ? where id = ?”;
         String sql = “delete from customers where id = ?”;
         update(sql, 30);
    }
 
    // 1. 使用 PreparedStatement 完成通用的增删改方法
    public int update(String sql, Object … args) {//(?, ?, ?) — 1, 2, 3
 
          int row = 0;
         Connection conn = null;
         PreparedStatement ps = null;
          try {
             conn = JDBCUtil. getConnection();
 
             ps = conn.prepareStatement(sql);
 
              // 填充占位符
              for (int i = 0; i < args.length; i++) {
                 ps.setObject(i+1, args[i]);
             }
             
             row = ps.executeUpdate();
 
             System. out.println(“已修改 “ + row + ” 行”);
         } catch (Exception e) {
             e.printStackTrace();
         } finally {
             JDBCUtil. close(null, ps, conn);
         }
         
          return row;
    }
 
}
——————-
第二天下午学习
——————-
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.Statement;
 
import org.junit.Test;
 
public class TestPreparedStatement2 {
    //批量处理
          @Test
          public void testPreparedStatment1(){ //25936 – 447 – 361 – 371
              long start = System.currentTimeMillis();
             
             Connection conn = null;
             PreparedStatement ps = null;
              try {
                 conn = JDBCUtil. getConnection();
                 
                 String sql = “insert into emp3 values(?, ?)”;
                 
                 ps = conn.prepareStatement(sql);
                 
                  for (int i = 0; i < 100002; i++) {
                      ps.setInt(1, i+1);
                      ps.setString(2, “emp” + i);
                      
                      ps.addBatch();
                      
                       if((i+1) % 100000 == 0){
                          ps.executeBatch();
                          
                          ps.clearBatch();
                      }
                      
//                    ps.executeUpdate();
                 }
                 
                 ps.executeBatch();
             } catch (Exception e) {
                 e.printStackTrace();
             } finally {
                 JDBCUtil. close(null, ps, conn);
             }
             
              long end = System.currentTimeMillis();
 
             System. out.println(“耗费时间为:” + (end – start));
             
         }
         
          @Test
          public void testStatement1(){ //48629
              long start = System.currentTimeMillis();
             
             Connection conn = null;
             Statement stam = null;
              try {
                 conn = JDBCUtil. getConnection();
                 
                 stam = conn.createStatement();
                 
                  for (int i = 0; i < 100000; i++) {
                      String sql = “insert into emp3 values(“+ (i + 1) +“, ’emp”+ i + “‘)”;
                      
                      stam.addBatch(sql);
                      
                       if((i+1) % 250 == 0){
                          stam.executeBatch();
                          
                          stam.clearBatch();
                      }
                      
//                    stam.executeUpdate( sql);
                 }
             } catch (Exception e) {
                 e.printStackTrace();
             } finally {
                 JDBCUtil. close(null, stam, conn);
             }
             
              long end = System.currentTimeMillis();
             
             System. out.println(“耗费时间为:” + (end – start));
         }
         
         
          @Test
          public void testPreparedStatment(){ //25936
              long start = System.currentTimeMillis();
             
             Connection conn = null;
             PreparedStatement ps = null;
              try {
                 conn = JDBCUtil. getConnection();
                 
                 String sql = “insert into emp3 values(?, ?)”;
                 
                 ps = conn.prepareStatement(sql);
                 
                  for (int i = 0; i < 100000; i++) {
                      ps.setInt(1, i+1);
                      ps.setString(2, “emp” + i);
                      
                      ps.executeUpdate();
                 }
             } catch (Exception e) {
                 e.printStackTrace();
             } finally {
                 JDBCUtil. close(null, ps, conn);
             }
             
              long end = System.currentTimeMillis();
 
             System. out.println(“耗费时间为:” + (end – start));
             
         }
         
         
          @Test
          public void testStatement(){ //45902
              long start = System.currentTimeMillis();
             
             Connection conn = null;
             Statement stam = null;
              try {
                 conn = JDBCUtil. getConnection();
                 
                 stam = conn.createStatement();
                 
                  for (int i = 0; i < 100000; i++) {
                      String sql = “insert into emp3 values(“+ (i + 1) +“, ’emp”+ i + “‘)”;
                      stam.executeUpdate(sql);
                 }
             } catch (Exception e) {
                 e.printStackTrace();
             } finally {
                 JDBCUtil. close(null, stam, conn);
             }
             
              long end = System.currentTimeMillis();
             
             System. out.println(“耗费时间为:” + (end – start));
         }
 
    }