Java 帮助 - MySQL - SQLConnector "recurse infinitely"& 控制台打印无限

标签 java mysql sqlexception

我正在尝试让下面的示例代码正常工作,并且我在其中插入了一个 println ,它只显示“之前”,当下面的代码运行时,控制台打印“之前”一大堆,然后控制台打印“异常在 . NextMonthNumbers.SQLConnector(NextMonthNumbers.java:35)"也有很多次。

我只是希望下面的代码能够正常工作。我有很多字符串查询需要处理,我正在尝试使用 1 个连接来完成它,所以它并不慢。

public class NextMonthNumbers {



public static Connection SQLConnector() throws SQLException {

    Connection con = null;
    PreparedStatement ps1,ps2= null;
    ResultSet rs,rs2=null;

        String query01="SELECT MAX(number)+1 FROM `Loads` WHERE month = 'Jan' AND `date_arrived` BETWEEN '2018-01-01' AND '2018-12-31'";
        String query02="SELECT MAX(number)+1 FROM `Loads` WHERE month = 'Feb' AND `date_arrived` BETWEEN '2018-01-01' AND '2018-12-31'";

 //testing for loop    System.out.println("before");
        ps1 =  SQLConnector().prepareStatement(query01);
        ps1.closeOnCompletion();
        ps2 =  SQLConnector().prepareStatement(query02);
        rs = ps1.executeQuery();
        rs2 = ps2.executeQuery();

    try{
        Class.forName("com.mysql.jdbc.Driver");
        String username = "Example";
        String password = "Example";
        String Database = "Example";
        con = DriverManager.getConnection(Database, username, password);
        con.setAutoCommit(false);
        System.out.println("*** Connecting to the database for Next Number ***");
        MiddleTextbottom.setText("Connecting to the database for Table Variables");


        while(rs.next()){
           query01 = rs.getString(1);

            if (rs.getString(ICONIFIED) ==null) {
                Main.UpdatedNextjan18.setText(query01);
                Main.UpdatedNextjan18.setText("1");
            }else{
                Main.UpdatedNextjan18.setText(query01);
            } 


        while(rs2.next()){
           query02 = rs2.getString(1);
            if (rs2.getString(ICONIFIED) ==null) {
                Main.UpdatedNextfeb18.setText(query02);
                Main.UpdatedNextfeb18.setText("1");
            }else{
                Main.UpdatedNextfeb18.setText(query02);
            } 

        }
        }


    } catch (ClassNotFoundException ex) {
          Logger.getLogger(SQLLoads.class.getName()).log(Level.SEVERE, null, ex);
          System.out.println("Error: "+ex);
    } catch (SQLException ex) {
          Logger.getLogger(SQLLoads.class.getName()).log(Level.SEVERE, null, ex);
          System.out.println(ex);
    }catch ( java.util.NoSuchElementException ex) {
          Logger.getLogger(SQLLoads.class.getName()).log(Level.SEVERE, null, ex);
          System.out.println("Error: "+ex);

    }
    con.close();
    SQLConnector().close();
    return null;

        }

}

最佳答案

正如 Tom 所说,我的主要问题太明显了,我在其内部调用 SQLException。所以我做了一个小重写,并将一些代码移到了 try{} 下方以使其正常工作。

public static Connection SQLConnector() throws SQLException {

    Connection con = null;
    PreparedStatement ps,ps2 = null;
    ResultSet rs,rs2 = null;


    try{
        Class.forName("com.mysql.jdbc.Driver");
        String username = "Example";
        String password = "Example";
        String Database = "Example";
        con = DriverManager.getConnection(Database, username, password);
        con.setAutoCommit(false);
        System.out.println("*** Connecting to the database for Next Number ***");
        MiddleTextbottom.setText("Connecting to the database for Table Variables");

        String query01="SELECT MAX(number)+1 FROM `Loads` WHERE month = 'Jan' AND `date_arrived` BETWEEN '2018-01-01' AND '2018-12-31'";
        String query02="SELECT MAX(number)+1 FROM `Loads` WHERE month = 'Feb' AND `date_arrived` BETWEEN '2018-01-01' AND '2018-12-31'";


        ps =  con.prepareStatement(query01);
        ps2 = con.prepareStatement(query02);
        rs = ps.executeQuery();
        rs2 = ps2.executeQuery();

        while(rs.next()){
           query01 = rs.getString(1);

            if (rs.getString(ICONIFIED) ==null) {
                Main.UpdatedNextjan18.setText(query01);
                Main.UpdatedNextjan18.setText("1");
            }else{
                Main.UpdatedNextjan18.setText(query01);
            } 


        while(rs2.next()){
           query02 = rs2.getString(1);
            if (rs2.getString(ICONIFIED) ==null) {
                Main.UpdatedNextfeb18.setText(query02);
                Main.UpdatedNextfeb18.setText("1");
            }else{
                Main.UpdatedNextfeb18.setText(query02);
            } 

        }
        }


    } catch (ClassNotFoundException ex) {
          Logger.getLogger(SQLLoads.class.getName()).log(Level.SEVERE, null, ex);
          System.out.println("Error: "+ex);
    } catch (SQLException ex) {
          Logger.getLogger(SQLLoads.class.getName()).log(Level.SEVERE, null, ex);
          System.out.println(ex);
    }catch ( java.util.NoSuchElementException ex) {
          Logger.getLogger(SQLLoads.class.getName()).log(Level.SEVERE, null, ex);
          System.out.println("Error: "+ex);

    }
    con.close();
    return null;

        }

关于Java 帮助 - MySQL - SQLConnector "recurse infinitely"& 控制台打印无限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51991359/

相关文章:

java - 位图无法加载 2MB 图像

java - 如何返回泛型类型的类

mysql - 此查询中的 SQL 错误

mysql - 检查每个组是否存在不同的值

.net - 字符串或二进制数据会被截断异常吗?

java - 理想的哈希码实现

java - 单击时不显示微调器

php - PDO MySQL 创建名称为数字的表

java - java中的列名无效,但适用于数据库

java - 严重 : null java. sql.SQLException