我使用带有JDBC连接的java web服务(apache轴)连接到postgres数据库以获取数据.但是在大多数情况下我突然得到一个例外,有时它工作正常.在这里,我使用了许多预备语句.我的示例代码是org.postgresql.util.PSQLException: The connection attempt failed.
Connection connection=null;
try
{
Class.forName(driver);
connection = DriverManager.getConnection(url, username, password);
ps1=connection.prepareStatement("select * from emp");
rs1=ps1.executeQuery();
while(rs1.next())
{
ps2=connection.prepareStatement("select * from dept where dept_no="+rs1.getInt("dept_no"));
rs2=ps2.executeQuery();
while(rs2.next())
{
................
................
}
}
}
catch(Exception e)
{
System.out.println("Exception occurred is"+e.getMessage());
}
finally
{
try{if(rs1!=null)rs1.close();}catch(Exception e){ System.out.println("*1closing error--->"+e);}
try{if(ps1!=null)ps1.close();}catch(Exception e){ System.out.println("**1closing error--->"+e);}
try{if(rs2!=null)rs2.close();}catch(Exception e){ System.out.println("*2closing error--->"+e);}
try{if(ps2!=null)ps2.close();}catch(Exception e){ System.out.println("**2closing error--->"+e);}
try{if(connection!=null)connection.close();}catch(Exception e){ System.out.println("***closing error--->"+e);}
}
Run Code Online (Sandbox Code Playgroud)
关于此异常的堆栈跟踪是
org.postgresql.util.PSQLException: The connection attempt failed.
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:137) …Run Code Online (Sandbox Code Playgroud)