mysql-sqlconnector“递归无限”&控制台打印无限

7bsow1i6  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(442)

我正在尝试让下面的示例代码正常工作&我在其中插入了一个println,它只显示“before”,当下面的代码运行时,console打印“before”一整串,然后console打印“.nextmonthnumbers.sqlconnector(nextmonthnumbers)处的异常。java:35)“还有很多次。
我只想让下面的代码正常工作。我有很多字符串查询要处理,我正在尝试使用1个连接来完成它,这样就不会太慢了。

  1. public class NextMonthNumbers {
  2. public static Connection SQLConnector() throws SQLException {
  3. Connection con = null;
  4. PreparedStatement ps1,ps2= null;
  5. ResultSet rs,rs2=null;
  6. String query01="SELECT MAX(number)+1 FROM `Loads` WHERE month = 'Jan' AND `date_arrived` BETWEEN '2018-01-01' AND '2018-12-31'";
  7. String query02="SELECT MAX(number)+1 FROM `Loads` WHERE month = 'Feb' AND `date_arrived` BETWEEN '2018-01-01' AND '2018-12-31'";
  8. //testing for loop System.out.println("before");
  9. ps1 = SQLConnector().prepareStatement(query01);
  10. ps1.closeOnCompletion();
  11. ps2 = SQLConnector().prepareStatement(query02);
  12. rs = ps1.executeQuery();
  13. rs2 = ps2.executeQuery();
  14. try{
  15. Class.forName("com.mysql.jdbc.Driver");
  16. String username = "Example";
  17. String password = "Example";
  18. String Database = "Example";
  19. con = DriverManager.getConnection(Database, username, password);
  20. con.setAutoCommit(false);
  21. System.out.println("***Connecting to the database for Next Number***");
  22. MiddleTextbottom.setText("Connecting to the database for Table Variables");
  23. while(rs.next()){
  24. query01 = rs.getString(1);
  25. if (rs.getString(ICONIFIED) ==null) {
  26. Main.UpdatedNextjan18.setText(query01);
  27. Main.UpdatedNextjan18.setText("1");
  28. }else{
  29. Main.UpdatedNextjan18.setText(query01);
  30. }
  31. while(rs2.next()){
  32. query02 = rs2.getString(1);
  33. if (rs2.getString(ICONIFIED) ==null) {
  34. Main.UpdatedNextfeb18.setText(query02);
  35. Main.UpdatedNextfeb18.setText("1");
  36. }else{
  37. Main.UpdatedNextfeb18.setText(query02);
  38. }
  39. }
  40. }
  41. } catch (ClassNotFoundException ex) {
  42. Logger.getLogger(SQLLoads.class.getName()).log(Level.SEVERE, null, ex);
  43. System.out.println("Error: "+ex);
  44. } catch (SQLException ex) {
  45. Logger.getLogger(SQLLoads.class.getName()).log(Level.SEVERE, null, ex);
  46. System.out.println(ex);
  47. }catch ( java.util.NoSuchElementException ex) {
  48. Logger.getLogger(SQLLoads.class.getName()).log(Level.SEVERE, null, ex);
  49. System.out.println("Error: "+ex);
  50. }
  51. con.close();
  52. SQLConnector().close();
  53. return null;
  54. }
  55. }
2ul0zpep

2ul0zpep1#

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

  1. public static Connection SQLConnector() throws SQLException {
  2. Connection con = null;
  3. PreparedStatement ps,ps2 = null;
  4. ResultSet rs,rs2 = null;
  5. try{
  6. Class.forName("com.mysql.jdbc.Driver");
  7. String username = "Example";
  8. String password = "Example";
  9. String Database = "Example";
  10. con = DriverManager.getConnection(Database, username, password);
  11. con.setAutoCommit(false);
  12. System.out.println("***Connecting to the database for Next Number***");
  13. MiddleTextbottom.setText("Connecting to the database for Table Variables");
  14. String query01="SELECT MAX(number)+1 FROM `Loads` WHERE month = 'Jan' AND `date_arrived` BETWEEN '2018-01-01' AND '2018-12-31'";
  15. String query02="SELECT MAX(number)+1 FROM `Loads` WHERE month = 'Feb' AND `date_arrived` BETWEEN '2018-01-01' AND '2018-12-31'";
  16. ps = con.prepareStatement(query01);
  17. ps2 = con.prepareStatement(query02);
  18. rs = ps.executeQuery();
  19. rs2 = ps2.executeQuery();
  20. while(rs.next()){
  21. query01 = rs.getString(1);
  22. if (rs.getString(ICONIFIED) ==null) {
  23. Main.UpdatedNextjan18.setText(query01);
  24. Main.UpdatedNextjan18.setText("1");
  25. }else{
  26. Main.UpdatedNextjan18.setText(query01);
  27. }
  28. while(rs2.next()){
  29. query02 = rs2.getString(1);
  30. if (rs2.getString(ICONIFIED) ==null) {
  31. Main.UpdatedNextfeb18.setText(query02);
  32. Main.UpdatedNextfeb18.setText("1");
  33. }else{
  34. Main.UpdatedNextfeb18.setText(query02);
  35. }
  36. }
  37. }
  38. } catch (ClassNotFoundException ex) {
  39. Logger.getLogger(SQLLoads.class.getName()).log(Level.SEVERE, null, ex);
  40. System.out.println("Error: "+ex);
  41. } catch (SQLException ex) {
  42. Logger.getLogger(SQLLoads.class.getName()).log(Level.SEVERE, null, ex);
  43. System.out.println(ex);
  44. }catch ( java.util.NoSuchElementException ex) {
  45. Logger.getLogger(SQLLoads.class.getName()).log(Level.SEVERE, null, ex);
  46. System.out.println("Error: "+ex);
  47. }
  48. con.close();
  49. return null;
  50. }
展开查看全部

相关问题