我正在尝试使下面的示例代码正常工作,并且我在其中贴了一个println,上面只写着“ before”,当下面的代码运行时,控制台在“ before”之前打印,然后控制台在“”处打印“ exception”。 NextMonthNumbers.SQLConnector(NextMonthNumbers.java:35)“也是很多次。
我只希望下面的代码正常工作。我有很多要处理的String查询,我尝试使用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;
}
}
答案 0 :(得分:0)
正如汤姆(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;
}