[JSP ] unreported exception java.sql.SQLExc …

看板java作者 (Mier)時間15年前 (2010/04/29 16:12), 編輯推噓2(202)
留言4則, 3人參與, 最新討論串1/1
最近嘗試用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)

04/29 16:15, , 1F
你是真的沒有處理這四個地方可能丟出的例外...
04/29 16:15, 1F

04/29 16:38, , 2F
你哪有處理了 Orz
04/29 16:38, 2F

04/29 16:40, , 3F
那要怎麼處理T________T 我看不出來
04/29 16:40, 3F

04/29 16:49, , 4F
= = 我找出來了....
04/29 16:49, 4F
文章代碼(AID): #1BsJ-02w (java)