1

在此链接的帮助下,我编写了一段catch特定的代码。它像是:exception

catch (SQLException sqle) {
    // TODO Auto-generated catch block
String sqlMessage = sqle.getMessage();
String sqlState = sqle.getSQLState();
int vendorCode = sqle.getErrorCode();
System.out.println("Exception occurred:");
System.out.println("Message: " + sqlMessage);
System.out.println("SQL state: " + sqlState);
System.out.println("Vendor code: " + vendorCode); 
}

但我得到的输出为:

java.sql.SQLException: ORA-00001: unique constraint (SYSTEM.PK_USERID) violated

at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:125)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:305)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:272)
at oracle.jdbc.driver.T4C8Oall.receive(T4C8Oall.java:623)
at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:181)
at oracle.jdbc.driver.T4CPreparedStatement.execute_for_rows(T4CPreparedStatement.java:543)
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1028)
at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:2888)
at oracle.jdbc.driver.OraclePreparedStatement.executeUpdate(OraclePreparedStatement.java:2960)
at accessdb.Dao.insertnewuser(Dao.java:32)
at Registration.doGet(Registration.java:47)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:619)

如果错误是:

java.sql.SQLException: ORA-00001: 违反唯一约束 (SYSTEM.PK_USERID)

我得到的输出为

Exception occurred:
Message: Listener refused the connection with the following error:
ORA-12505, TNS:listener does not currently know of SID given in connect descriptor
The Connection descriptor used by the client was:
localhost:1521:XE
SQL state: null
Vendor code: 0

如果错误是:

侦听器拒绝连接并出现以下错误:ORA-12505,TNS:listener 目前不知道连接描述符中给出的 SID 客户端使用的连接描述符是:localhost:1521:XE。

为什么同一段代码会出现输出差异?

编辑: 好的,这里是try进一步澄清的块。

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // Authentication and Logging in The Registered User
    Getset g=new Getset();
    Dao dao=new Dao();
    String userid="";
    String fname="";
//  PrintWriter pw=response.getWriter();
    String loginemail=request.getParameter("loginemail");
    String loginpassword=request.getParameter("loginpassword");
    if(loginemail.equals("")||loginemail.equals(" ")||loginpassword.equals("")||loginpassword.equals(" "))
    {
        response.sendRedirect("WelcomePage.jsp");
    }
    else{
    g.setloginemail(loginemail);
    g.setloginpassword(loginpassword);
    try {
        ResultSet rs=dao.loginauthentication(g);
        while(rs.next())   //Fetching all emails and passwords from user table
        {
            String regemail=rs.getString("regemail");
            String regpassword=rs.getString("regpassword");
            System.out.println(""+regemail);
            if(loginemail.equals(regemail) && (loginpassword.equals(regpassword))==true)
            {   
                System.out.println("55555");
                ResultSet rs1=dao.getnameid(g);
                while(rs1.next())   //GET USERID and name FROM NEWUSER TO USE AS PRIMARY KEY
                {
                     userid=rs1.getString("USERID");
                     fname=rs1.getString("FNAME");
                //  System.out.println(""+userid);

                }

                HttpSession session = request.getSession(true);
                  session.setAttribute("USERID", userid);
                  session.setAttribute("FNAME", fname);
                response.sendRedirect("UserHome.jsp");
                return;
            }
        }  
        if(rs.next()==false){
            System.out.println("caught");
            response.sendRedirect("WelcomePage.jsp");
            return; 
        }

    }
4

3 回答 3

5

您没有发布足够多的代码来提供明确的答案,但这就是您所谓的“有根据的猜测”:您的子句的 the tryof 您的catch子句没有包含这行代码:

accessdb.Dao.insertnewuser(Dao.java:32)

使用您的新代码,以下假设更加合理:您的insertnewuser方法有自己的try-catch块,如下所示:

try {
  ...prepare an insert statement...
  stmt.executeUpdate(...);
} catch (SQLException e) { e.printStackTrace(); }

这会吞下您的异常,并使您的方法正常完成,尽管有异常。解决方案:从该方法中删除整个try-catch并使该方法声明throws SQLException

于 2012-08-01T18:36:09.510 回答
0

这里有两个问题。请注意,这个答案只是一个猜测,因为您没有发布足够的代码。

一方面,当尝试连接到数据库服务器时SQLException抛出an 时,您将获得所需的输出。可能,这意味着您将块放在管理与数据库的连接的代码块中。catch

另一方面,当您尝试这样做时,您会得到一个不需要的输出,insertnewuser并且ORA-00001DB 会引发错误,这反过来会导致SQLException. 这意味着与数据库的连接已成功,但插入操作尚未成功。

你有两个 try-catch 结构吗?

于 2012-08-01T18:35:53.397 回答
0

让我也加入“让我们猜猜代码”的戏:你正在使用一些持久性抽象而不是调用flush()// ormFlush()- what have you> 所以不能保证立即插入 -> 所以无论何时/何地都会抛出 SQLException?

于 2012-08-01T18:41:18.670 回答