<%
String url = "jdbc:odbc:Temdata";
String username = "huyaozhong";
String password = "123456";
Connection con = null;
try{
out.println("Beforw first connection<br>");
con = DriverManager.getConnection(url,username,"");
out.println("After first connection<br>");
}catch(SQLException e){
out.println("First connection throws exception "+e.getMessage()+e.getCause()+e.getErrorCode()+"<br>");
}
if(con == null){
out.println("First connection was failed<br>");
}else{
out.println("First connection successs<br>");
}
try{
out.println("Before load Driver class<br>");
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
out.println("After load Driver class<br>");
}catch(ClassNotFoundException e){
out.println("Exception occure when load Driver Class"+e.getMessage()+e.getCause()+e.getException()+"<br>");
}
try{
out.println("Before second conenction<br>");
con = DriverManager.getConnection(url,username,"");
out.println("After second connection<br>");
}catch(SQLException e){
out.println("Exception occure when first connecte to database "+e.getMessage()+e.getCause()+e.getErrorCode()+"<br>");
}
if(con == null){
out.println("seceond connection failed<br>");
}else{
out.println("seconde connection seccess");
}
try{
if(con != null)
con.close();
}catch(SQLException e){
out.println("Exception occure when the connection closed"+e.getSQLState()+e.getCause()+"<br>");
}
%>
运行结果:
Beforw first connection
First connection throws exception [Microsoft][ODBC SQL Server Driver][SQL Server]用户 'huyaozhong' 登录失败。null18456
First connection was failed
Before load Driver class
After load Driver class
Before second conenction
Exception occure when first connecte to database [Microsoft][ODBC SQL Server Driver][SQL Server]用户 'huyaozhong' 登录失败。null18456
seceond connection failed