简体   繁体   English

循环try / catch语句

[英]Looping try/catch statement

I'm trying to take two random rowid from my database. 我正在尝试从数据库中获取两个随机rowid。 Everything works but I have a scenario when there is only one rowid. 一切正常,但是我有一个场景,只有一个rowid。 I want to make a loop on my try/catch until there is second number in my database. 我想在try / catch上进行循环,直到数据库中没有第二个数字为止。
What I'm doing wrong? 我做错了什么? Thank you 谢谢

public void Kaslaimejo() {
    String sql = "SELECT rowid FROM Zaidejai WHERE Pirmas < 4 ORDER BY random() LIMIT 2";
    Integer value1 = null, value2 = null; 
    Integer judesiukas1 = null, judesiukas2 = null;

    int a = 0;
    int k = 15; // kiek kartu? Reikia infinity padaryti

    for (a = 0; a < 3; a++) {
        try {
            Connection conn = Serveris.connect();
            Statement stmt  = conn.createStatement();
            ResultSet rs = stmt.executeQuery(sql)) {
                if (rs.next()) {
                    value1 = rs.getInt("rowid");
                    if (rs.next()) {
                        value2 = rs.getInt("rowid");
                        PreparedStatement buvo = conn.prepareStatement("UPDATE Zaidejai SET Numeriukas = ? WHERE rowid = ?");
                        buvo.setInt(1, i);
                        buvo.setInt(2, value1);
                        int buvolala = buvo.executeUpdate  ();
                        PreparedStatement buvo2 = conn.prepareStatement("UPDATE Zaidejai SET Numeriukas = ? WHERE rowid = ?");
                        buvo2.setInt(1, i);
                        buvo2.setInt(2, value2);
                        int buvolala2 = buvo2.executeUpdate  ();// 
                        i++;
                    }
                System.out.println("Pirmas zaidejas" + value1); // atspausdina 1 random zaideja is duomenu bazes
                System.out.println("Antras zaidejas" + value2); // atspausdina 2 random zaideja is duomenu bazes
            }
        } catch (SQLException e) {
            a--;
            //System.out.println(e.getMessage());
        }
    }
}

Right now my program loops two times and then gives me SQLException . 现在,我的程序循环了两次,然后给了我SQLException How I can loop my program until there is no SQLException ? 如何在没有SQLException之前循环程序?

How i can loop my program until there is no SQLException? 我如何循环我的程序,直到没有SQLException?

Change this (because, it will only allow to loop two times) 更改此项(因为它只允许循环两次)

for (a = 0; a < 2; a++) {

to

while(true)

Put everything inside while(true) , if exception occurred, then it will come out from the while loop. 将所有内容放入while(true) ,如果发生异常,则它将从while循环中出来。 Something similar : 相似的东西 :

try
{
  while(true)
  {
  ...
  ...
  }
  ...
}
catch(SQLException e)
{
    // do somthing
}

The logic becomes easier if you add the values to a list 如果将值添加到列表中,逻辑将变得更容易

var values = new ArrayList<Integer>();
while (values.Count < 2) {
    try (Connection conn = Serveris.connect();
         Statement stmt  = conn.createStatement();
         ResultSet rs = stmt.executeQuery(sql))
    {
        while (values.Count < 2 && rs.next()) {
            Integer v = rs.getInt("rowid");
            values.Add(v);
        }
    } catch (SQLException e) {
    }
}
//TODO: process the values here

The advantage is, that you can retrieve one value at the first database query and the second at a later one or both in the same round and you don't have to keep track of which one of two variables to use. 优点是,您可以在同一回合中在第一个数据库查询中检索一个值,而在以后的一个或两个数据库中检索第二个值,而不必跟踪要使用的两个变量中的哪个。

(Bear with me with the syntax details, I'm not a Java programmer.) (请耐心等待我的语法详细信息,我不是Java程序员。)

OK, I've tried to write what I think you're trying to do. 好的,我已经尝试写出我认为您正在尝试做的事情。 You wait for ever until someone puts at least two entries in the database. 您一直等到有人将至少两个条目放入数据库中。 You extract two values, process them, then wait some more. 您提取两个值,对其进行处理,然后再等待一些。

Some points to watch out: 1. Object comparisons need to be made with .equals() not with == 2. You might want to provide some way to break out of the infinite loop I've written ( while(true) ). 需要注意的几点:1.对象比较需要使用.equals()而不是== 2。您可能想提供一些方法来摆脱我编写的无限循环( while(true) )。 3. Careful with null values. 3.小心使用null值。 They might produce NullPointerException . 他们可能会产生NullPointerException 4. Try to break up your code into methods. 4.尝试将代码分解为方法。 Each large block of code could go into each own method. 每个大的代码块都可以放入每个自己的方法中。

public void Kaslaimejo(){
    String sql = "SELECT rowid FROM Zaidejai WHERE Pirmas < 4 ORDER BY random() LIMIT 2";
    Integer judesiukas1 = null, judesiukas2 = null;


    while(true) {

        List<Integer> values = new ArrayList<>();

        while (values.size() < 2) {
            try (Connection conn = Serveris.connect();
                 Statement stmt = conn.createStatement();
                 ResultSet rs = stmt.executeQuery(sql)) {
                if( rs.next() ){
                    Integer value = rs.getInt("rowid");
                    values.add(value);
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        try( Connection conn = Serveris.connect()) {
            PreparedStatement buvo = conn.prepareStatement("UPDATE Zaidejai SET Numeriukas = ? WHERE rowid = ?");
            buvo.setInt(1, i);
            buvo.setInt(2, values.get(0));
            int buvolala = buvo.executeUpdate  ();
            PreparedStatement buvo2 = conn.prepareStatement("UPDATE Zaidejai SET Numeriukas = ? WHERE rowid = ?");
            buvo2.setInt(1, i);
            buvo2.setInt(2, values.get(1));
            int buvolala2 = buvo2.executeUpdate  ();//
            i++;
        }catch (SQLException e) {
            e.printStackTrace();
        }

        Connection conn = Serveris.connect();

        try {
            PreparedStatement pstmt = conn.prepareStatement("SELECT Pirmas FROM Zaidejai WHERE rowid = ?");
            PreparedStatement pstmt2 = conn.prepareStatement("SELECT Pirmas FROM Zaidejai WHERE rowid = ?");
            pstmt.setInt(1, values.get(0));
            pstmt2.setInt(1, values.get(1));
            ResultSet myrsv = pstmt.executeQuery();
            ResultSet myrsv2 = pstmt2.executeQuery();


            {

                if (myrsv.next()) {
                    judesiukas1 = myrsv.getInt("Pirmas");
                    if (myrsv2.next()) {
                        judesiukas2 = myrsv2.getInt("Pirmas");
                    }

                }
                //System.out.println("Pirmo zaidejo veiksmas" + myrsv.getInt("Pirmas"));
                //System.out.println("Antro zaidejo veiksmas" + myrsv2.getInt("Pirmas"));

            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();

        }

        if (judesiukas1.equals(judesiukas2)) // careful here. NullPointerException may happen.
        {
            try {
                PreparedStatement laim = conn.prepareStatement("UPDATE Zaidejai SET Rezultatas = ? WHERE rowid = ?"); // ble ble update reikia naudoti , o ne insert into. Insert kai sukuriame nauja kazka tik
                PreparedStatement laim2 = conn.prepareStatement("UPDATE Zaidejai SET Rezultatas = ? WHERE rowid = ?");
                laim.setString(1, "Lygiosios");
                laim.setInt(2, values.get(0));
                laim2.setString(1, "Lygiosios");
                laim2.setInt(2, values.get(1));
                int irasyk = laim.executeUpdate  (); // kodel executeupdate, o ne executequery????
                int irasyk2 = laim2.executeUpdate  (); // kodel executeupdate, o ne executequery????
                {


                }
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.print("Lygiosios");
        } else {

            //  (1) - Rock
            //  (2)  Scissors
            //  (3) - Paper
            switch (values.get(0)){
                case 1:
                    if (judesiukas2 == 2)
                        System.out.print("Zaidejas 1 wins!");

                    else
                        System.out.print("Zaidejas 2 wins!");
                    break;
                case 2:
                    if (judesiukas2 == 3)
                        System.out.print("Zaidejas 1 wins!");
                    else
                        System.out.print("Zaidejas 2 wins!");
                    break;
                case 3:
                    if (judesiukas2 == 1)
                        System.out.print("Zaidejas 1 wins!");
                    else
                        System.out.print("Zaidejas 2 wins!");
                    break;
            }
        }

        try {
            conn.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM