• Iterator中的next()


    DBExchangeMoney类:
    1
    package com.ch.test15; 2 3 import java.sql.DriverManager; 4 import java.sql.ResultSet; 5 import java.sql.SQLException; 6 import java.sql.Statement; 7 import java.sql.Timestamp; 8 import java.util.ArrayList; 9 import java.util.Date; 10 import java.util.Iterator; 11 12 import com.ch.test02.Person; 13 14 /** 15 * 交换A,B,C的金额 16 * @author Administrator 17 * 18 */ 19 public class DBExchangeMoney { 20 public static void main(String[] args) throws ClassNotFoundException, SQLException { 21 //定义数据库驱动器 22 String DRIVER = "com.mysql.jdbc.Driver"; 23 //加载数据库驱动器 24 Class.forName(DRIVER); 25 //定义数据库的链接 26 String URL = "jdbc:mysql://192.168.1.20:3306/cuihao?useUnicode=true&characterEncoding=UTF-8"; 27 String Users = "root"; 28 String Passw = "root"; 29 java.sql.Connection con = null; 30 try { 31 con = DriverManager.getConnection(URL, Users, Passw); 32 con.setAutoCommit(false); 33 } catch (SQLException e) { 34 e.printStackTrace(); 35 } 36 //PreparedStatement prepStmt = null; 37 Statement st = null; 38 ResultSet rs = null; 39 int tem = 0; 40 ArrayList<MoneyDefine> list = new ArrayList<MoneyDefine>(); 41 try { 42 //执行sql查询 43 String selectStatement = "select * from Moneys"; 44 st = con.createStatement(); 45 rs = st.executeQuery(selectStatement); 46 while (rs.next()) { 47 String id = rs.getString("id"); 48 String name = rs.getString("pname"); 49 int moneys = rs.getInt("prices"); 50 String dates = rs.getString("dates"); 51 MoneyDefine md = new MoneyDefine(id,name,moneys,dates); 52 list.add(md); 53 } 54 for(Iterator<MoneyDefine> iterator = list.iterator();iterator.hasNext();){ 55 //错误的,next()在list内,跳到下一条的下一个位置 56 //System.out.println(iterator.next().getId() +iterator.next().getName()
    // +iterator.next().getMoneys() +iterator.next().getDates());
    57 //用对象p接收 58 MoneyDefine p = iterator.next(); 59 System.out.println("ID:" +p.getId() +" 姓名:" +p.getName() 60 +" 金额:" +p.getMoneys() +" 时间:" +p.getDates()); 61 // System.out.println(p.getMoneys()); 62 // System.out.println(p.getDates()); 63 } 64 65 System.out.println("--------------------------"); 66 //two给one 1000; one给two 1000; 67 //更新 68 String exchangeMoney = "UPDATE Moneys SET prices=prices+1000 WHERE id=1"; 69 String exchangeMoney1 = "UPDATE Moneys SET prices=prices-1000 WHERE id=2"; 70 st = con.createStatement(); 71 tem = st.executeUpdate(exchangeMoney); 72 tem = st.executeUpdate(exchangeMoney1); 73 System.out.println("执行后:" +tem); 74 //查询更新后的内容 75 String selectStatement1 = "select * from Moneys"; 76 st = (Statement) con.createStatement(); 77 rs = st.executeQuery(selectStatement1); 78 while (rs.next()) { 79 String id = rs.getString("id"); 80 String name = rs.getString("pname"); 81 String moneys = rs.getString("prices"); 82 Date dates = rs.getTimestamp("dates"); 83 System.out.print("ID:" +id); 84 System.out.print(" 姓名:" +name); 85 System.out.print(" 金额:" +moneys); 86 System.out.print(" 时间:" +dates); 87 System.out.println(); 88 } 89 System.out.println("--------------------"); 90 } catch (SQLException e) { 91 con.rollback(); 92 e.printStackTrace(); 93 } 94 //关闭 95 con.commit(); 96 rs.close(); 97 st.close(); 98 con.close(); 99 } 100 }
    MoneyDefine类:
    1
    package com.ch.test15; 2 3 import java.sql.Timestamp; 4 import java.util.Date; 5 6 public class MoneyDefine { 7 8 public String id; 9 public String name; 10 public int moneys; 11 public String dates; 12 13 public MoneyDefine(String id, String name, int moneys, String dates){ 14 this.id = id; 15 this.name = name; 16 this.moneys = moneys; 17 this.dates = dates; 18 } 19 public String getId() { 20 return id; 21 } 22 public void setId(String id) { 23 this.id = id; 24 } 25 public String getName() { 26 return name; 27 } 28 public void setName(String name) { 29 this.name = name; 30 } 31 public float getMoneys() { 32 return moneys; 33 } 34 public void setMoneys(int moneys) { 35 this.moneys = moneys; 36 } 37 public String getDates() { 38 return dates; 39 } 40 public void setDates(String dates) { 41 this.dates = dates; 42 } 43 44 45 }
    DBExchangeMoney类中,将从数据库取出的元素放入ArrayList中,然后再从list里取出。
    这时遇到问题,当用迭代取元素时
    System.out.println(iterator.next().getId() +iterator.next().getName() +iterator.next().getMoneys() +iterator.next().getDates());

    执行出行错误。

    修改为:

    1 MoneyDefine p = iterator.next();
    2 System.out.println("ID:" +p.getId() +" 姓名:" +p.getName()+" 金额:" +p.getMoneys() +" 时间:" +p.getDates());
    View Code

    正确输出。

    如果每次都调用iterator.next(),则第一次调用iterate.next(),指向List的第一行的getId(),即id=1,第二次调用iterate.next(),就会指向第二行的getName(),即name=two。第三次调用,就会报出异常。所以无法正常输出!

    id name prices dates
    1 one 0 2015-04-15 11:10:34
    2 two 1000 2015-04-15 11:10:40

    当iterator.next()应该赋值给一个对象p,然后调用对象p来取值,这样p每次会从每行的首个元素开始迭代。

    JDK API 1.6,关于Iterator中next()的说明:

    next()

    返回迭代的下一个元素。

    返回:

    迭代的下一个元素。

    抛出:

    NoSuchElementException - 没有元素可以迭代。

  • 相关阅读:
    计算机网络中的多路复用技术
    ActiveMQ之一--ActiveMQ入门
    ehcache介绍
    I/O模型之二:Linux IO模式及 select、poll、epoll详解
    【甘道夫】HBase(0.96以上版本号)过滤器Filter具体解释及实例代码
    Android Studio安装及主题字体配置
    HDU 2136 Largest prime factor 參考代码
    update更新两个字段
    Hadoop对小文件的解决方式
    赵雅智_ContentProvider
  • 原文地址:https://www.cnblogs.com/cuihao/p/4428054.html
Copyright © 2020-2023  润新知