我正在尝试让下面的示例代码正常工作&我在其中插入了一个println,它只显示“before”,当下面的代码运行时,console打印“before”一整串,然后console打印“.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;
}
}
1条答案
按热度按时间7d7tgy0s1#
我的主要问题太明显了,正如tom所说的,我在内部调用sqlexception。所以我做了一个小的重写,并将一些代码移到我的代码下面,以使其正常工作。