作者etchen (Mier)
看板java
标题[JSP ] unreported exception java.sql.SQLException
时间Thu Apr 29 16:12:45 2010
最近尝试用Sevlet来处理网页的东西,
可是有个问题一直找不出来...
上网爬文说要处理例外性错误,可是我也有处理了...
但是还是有问题,不知道我的写法是否正确?
错误讯息:
Login.java:12: unreported exception java.sql.SQLException; must be caught or
declared to be thrown
Conn = DBConnect.getConnection();
^
Login.java:36: unreported exception java.sql.SQLException; must be caught or
declared to be thrown
if(MyRs!=null) MyRs.close();
^
Login.java:37: unreported exception java.sql.SQLException; must be caught or
declared to be thrown
if(prepStmt!=null) prepStmt.close();
^
Login.java:38: unreported exception java.sql.SQLException; must be caught or
declared to be thrown
Conn.close();
^
4 errors
我的程式码如下:
package com.posm;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.posm.LDAP;
import com.posm.DBConnect;
import java.sql.*;
public class Login extends HttpServlet {
public void service (HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException{
Connection Conn = null;
Conn = DBConnect.getConnection();
ResultSet MyRs = null;
PreparedStatement prepStmt = null;
LDAP Auth = new LDAP();
String account = request.getParameter("account");
String password = request.getParameter("password");
String result = Auth.LdapAuth(account,password)[0];
try{
if("0".equals(result)){
prepStmt = Conn.prepareStatement("Select Account From Account
Where Account = ?");
prepStmt.setString(1,account);
MyRs = prepStmt.executeQuery();
if(MyRs!=null&&MyRs.next()){
response.sendRedirect("/POSM/Main.html");
}else{
response.sendRedirect("/POSM/Index.html");
}
}else{
response.sendRedirect("/POSM/Index.html");
}
}catch(Exception e){
e.printStackTrace();
}finally{
if(MyRs!=null) MyRs.close();
if(prepStmt!=null) prepStmt.close();
Conn.close();
}
}
}
--
请勿拍打、喂食!
http://www.wretch.cc/album/etchen
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 218.211.33.46
※ 编辑: etchen 来自: 218.211.33.46 (04/29 16:15)
1F:推 LPH66:你是真的没有处理这四个地方可能丢出的例外... 04/29 16:15
2F:推 superlubu:你哪有处理了 Orz 04/29 16:38
3F:→ etchen:那要怎麽处理T________T 我看不出来 04/29 16:40
4F:→ etchen:= = 我找出来了.... 04/29 16:49