How to deal with the error message for connecting database JavaBean

zhaozj2021-02-17  50

When performing a Java database development, we usually package the operation of the database in JavaBean. In this way, we don't have to place these statements in the try-catch statement when data is processed in the page. But how do I return detailed error message?

In actual operation, I use this way - define two private variables int countint and String strDBerror record error coding and error messages. The demo code is as follows:

Package DB;

/ ** * Title: JavaBean of Database * Copyright: Copyright (c) 2002 * Company: Weide * @Author weidegong * @version 1.0 * / import java.sql. *; Import java.util. *;

Public class dataconn {

Private connection conn = null; private result set drprst = null; private int countint = 0; // 0, default; -1, indicating a database operation error; -2, java.lang.exception; other Encoding ..... private string strdberror = ""; // Error message Boolean AutoCommit

Public DataConn () {Try {// jdbc-odbc class.Forname ("Sun.jdbc.odbc.jdbcodbcodbdriver"); conn = drivermanager.getConnection ("JDBC: ODBC: SQLServer", "SA", "706ADM607);

Drpstmt = conn.createstatement ();} catch (sqlexception sqle) {countint = -1; strdberror = SQLE.GetMessage ();} catch (java.lang.exception e) {countint = -2; strdberror = E.GetMessage }}

/ / -------------------------------------------------------------------------------------------- -------------------

Public int getErrorCode () {

Return country;

}

/ / -------------------------------------------------------------------------------------------- -------------------

/ / -------------------------------------------------------------------------------------------- -------------------

Public string geterrorinfo () {

Return strdberror;

}

/ / -------------------------------------------------------------------------------------------- -------------------

/ / -------------------------------------------------------------------------------------------- ------------------- Public ResultSet ExeQuery (string querystring) {countint = 0; // initializes 0 strdberror = ""; // Initialization error message try {drprst = drpStmt.executeQuery (queryString); countInt = 1;} catch (SQLException sqly) {countInt = -1; strDBError = "! Error occur while useing dataConn.exeQuery (queryString)

The queryString is

" queryString

The error information from dbms:

" sqly.getMessage ();} catch (java.lang.exception y) {countint = -2; strdberror ="

java.lang.exception: " Y.getMessage ();} Return DrPRST;} // -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- --------------------------------------- -------------------------------------------------- public int exeUpdate (String updateString) {countInt = 0; // initializes to 0 strDBError = ""; try {countInt = drpStmt.executeUpdate (updateString);} catch (SQLException sqlz) {countInt = -1; // -1 Indicating an error strDberror = "Error Occur While Using Dataconn.exeupdat e ()! The SQL IS:

" UpdateString "

error information from the database is:

sqlz.getMessage ();} return countint;} // ------ -------------------------------------------------- ----------------

/ / -------------------------------------------------------------------------------------------- ----- / / Get the number of records returned after the specified SQL query statement is executed / / avoiding the "Order By" statement in STRSQL, if there is any, otherwise it will be wrong. // Remove the "Order By" clause There is no impact on the number of acquisitions. public int getRowCount (String queryString) {countInt = 0; strDBError = ""; ResultSet rs; int nRowCount = 0; try {rs = drpStmt.executeQuery ( "select count (*) from (" queryString ") as viewTempQueryString" ); While (rs.next ()) nrowcount = rs.getint (1);} catch (sqlexception errGetrowcount) {countint = -1; // Error strdberror = "Error Occount (" queryString " )! Please contact the administrator.

Error information returned from the database: " ErrGetrowCount.getMessage ();} catch (java.lang.exception errother) {countint = -2; strdberror ="

Other Error: " Errother.getMessage ();} return nrowcount;} // --------------------------------- --------------------------} // end of all

Defined the above JavaBean, the use of the JSP page is shown below:

Test.jsp Source Code -----------------

<% String strsql = "select * from table"; ResultSet RS = cond.exequery (strsql); if (Condb.GeterrorCode () <0) {Out.println ("error:" condb.GeterrorInfo ()); Out.close ();}%>

转载请注明原文地址:https://www.9cbs.com/read-28652.html

New Post(0)