Java 语法警告显示:try语句体中从不抛出异常
警告: 在try语句体中从不抛出异常Java 语法警告显示:try语句体中从不抛出异常,java,mysql,exception-handling,Java,Mysql,Exception Handling,警告: 在try语句体中从不抛出异常 出现在捕获(ConnectException){行。您的try块中没有一个代码: public class SetupConnection{ public static Statement setCon(){ Connection con=null; Statement st=null; try{ Class.forName("com.mysql.jdbc.Driver").newI
出现在
捕获(ConnectException){
行。您的try块中没有一个代码:
public class SetupConnection{
public static Statement setCon(){
Connection con=null;
Statement st=null;
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
con=DriverManager.getConnection(
"jdbc:mysql://localhost:3306/zomatocrm","root","");
st = con.createStatement();
}
catch(ConnectException err){
JOptionPane.showMessageDialog(null, "Connection refused!!!");
System.out.println(err.getMessage());
}
catch(Exception e){
System.out.println(e.getMessage());
}
return st;
}
…可能会抛出一个ConnectException
。因此编译器警告您,此catch
子句:
Class.forName("com.mysql.jdbc.Driver").newInstance();
con=DriverManager.getConnection(
"jdbc:mysql://localhost:3306/zomatocrm","root","");
st = con.createStatement();
是多余的。如果删除它,警告将消失。try块中没有任何代码:
public class SetupConnection{
public static Statement setCon(){
Connection con=null;
Statement st=null;
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
con=DriverManager.getConnection(
"jdbc:mysql://localhost:3306/zomatocrm","root","");
st = con.createStatement();
}
catch(ConnectException err){
JOptionPane.showMessageDialog(null, "Connection refused!!!");
System.out.println(err.getMessage());
}
catch(Exception e){
System.out.println(e.getMessage());
}
return st;
}
…可能会抛出一个ConnectException
。因此编译器警告您,此catch
子句:
Class.forName("com.mysql.jdbc.Driver").newInstance();
con=DriverManager.getConnection(
"jdbc:mysql://localhost:3306/zomatocrm","root","");
st = con.createStatement();
是多余的。如果删除它,警告将消失。
getConnection
和createStatement
throwSQLException
,它将被捕获在第二个catch块中。ConnectException
永远不会被抛出…getConnection
和createStatement
throwSQLException
>,它被捕获在您的第二个catch块中。ConnectException
从未被抛出…注意,如果到数据库的连接由于网络问题而失败,您仍然只会得到一个SQLException
,因此捕获会更好,如果您想确定是否存在网络问题,可以检查另外(作为一般点,与此错误无关),您的方法创建一个连接(
con
object),但从未返回对它的引用,因此如何关闭连接?不关闭连接可能会导致连接泄漏,这将导致您出现问题。注意,如果到数据库的连接由于网络问题而失败,您仍然只会得到一个SQLException
,因此如果您愿意,捕获该连接会更好确定是否存在网络问题,您可以检查SQLState
(根据):
另外(作为一般点,与此错误无关),您的方法创建一个连接(
con
object),但从未返回对它的引用,因此您如何关闭连接?不关闭连接可能会导致连接泄漏,这将给您带来问题。您真正的问题已经得到其他人的回答。我只想指出,这
try {
...
} catch (SQLException sqle) {
if ("08S01".equals(sqle.getSQLState())) {
JOptionPane.showMessageDialog(null, "Connection refused (or networking problem)!!!");
System.out.println(err.getMessage());
}
}
…是非常糟糕的代码
您正在做的是将stacktrace转储到stderr
,然后继续,就好像没有发生什么不好的事情一样。问题是:
- 您刚刚捕获的
可能是由各种原因引起的,其中一些不应被忽略,并且异常
- stderr流可能未连接任何内容
- (可能更糟)原始stacktrace可能会显示给不懂Java的用户,他们可能不理解它,并且可能会忽略它或发疯
- 你真正的问题已经被其他人回答了。我只想指出这
try {
...
} catch (SQLException sqle) {
if ("08S01".equals(sqle.getSQLState())) {
JOptionPane.showMessageDialog(null, "Connection refused (or networking problem)!!!");
System.out.println(err.getMessage());
}
}
…是非常糟糕的代码
您正在做的是将stacktrace转储到stderr
,然后继续,就好像没有发生什么不好的事情一样。问题是:
- 您刚刚捕获的
可能是由各种原因引起的,其中一些不应被忽略,并且异常
- stderr流可能未连接任何内容
- (可能更糟)原始stacktrace可能会显示给不懂Java的用户,他们可能不理解它,并且可能会忽略它或发疯
con.close()
?因为如上所述的方法,您正在setCon
方法中创建一个con
对象,但它没有返回。返回con
对象可能更好,这意味着使用setCon()的人
method然后负责关闭连接。是的,只有…我在代码n中返回了con latr,然后在我使用它的时候关闭了它…很好…我猜!!!…你在哪里使用了con.close()
?因为如上所述的方法,您正在setCon
方法内创建一个con
对象,但它没有返回。最好返回con
对象,这意味着使用setCon()的人
method然后负责关闭连接。是的,只有…我在代码n中返回了con latr,然后在我使用它的时候关闭了它…很好…我猜!!!。。。