Jdbc Error Handling
Contents |
- Sample Code JDBC - Driver Types JDBC - Connections JDBC - Statements JDBC - Result Sets JDBC - Data Types JDBC - Transactions JDBC - Exceptions JDBC - Batch Processing JDBC - Stored Procedure JDBC - Streaming Data
Database Exception Handling In Java
JDBC Examples JDBC - Create Database JDBC - Select Database JDBC - Drop Database JDBC sqlexception error codes - Create Tables JDBC - Drop Tables JDBC - Insert Records JDBC - Select Records JDBC - Update Records JDBC - Delete
Java Sqlexception Error Codes
Records JDBC - WHERE Clause JDBC - Like Clause JDBC - Sorting Data JDBC Useful Resources JDBC - Questions and Answers JDBC - Quick Guide JDBC - Useful Resources JDBC - Discussion Useful - Java Tutorials Selected sqlexception in java Reading Developer's Best Practices Questions and Answers Effective Resume Writing HR Interview Questions Computer Glossary Who is Who JDBC - Exceptions Handling Advertisements Previous Page Next Page Exception handling allows you to handle exceptional conditions such as program-defined errors in a controlled fashion. When an exception condition occurs, an exception is thrown. The term thrown means that current program execution stops, and the control is redirected to the nearest applicable catch clause. If no jdbc error codes applicable catch clause exists, then the program's execution ends. JDBC Exception handling is very similar to the Java Exception handling but for JDBC, the most common exception you'll deal with is java.sql.SQLException. SQLException Methods An SQLException can occur both in the driver and the database. When such an exception occurs, an object of type SQLException will be passed to the catch clause. The passed SQLException object has the following methods available for retrieving additional information about the exception − Method Description getErrorCode( ) Gets the error number associated with the exception. getMessage( ) Gets the JDBC driver's error message for an error, handled by the driver or gets the Oracle error number and message for a database error. getSQLState( ) Gets the XOPEN SQLstate string. For a JDBC driver error, no useful information is returned from this method. For a database error, the five-digit XOPEN SQLstate code is returned. This method can return null. getNextException( ) Gets the next Exception object in the exception chain. printStackTrace( ) Prints the current exception, or throwable, and it's backtrace to a standard error stream. printStackTrace(PrintStream s) Prints this throwable and its backtrace to the print stream you specify. printStackTrace(PrintWriter w) Prints this throwable and it's backtrace to the print writer you specify. By utilizing the information available from the Exception object, you can catch an e
segment of code might throw, we have to have a good understanding of what it does. So let's quickly review what happens in a standard JDBC operation. The
Throw Sql Exception Java
basic scheme of any JDBC operation is pretty straightforward: Get a connection. Create unreachable catch block for sqlexception. this exception is never thrown from the try statement body a statement. Execute the statement and get a ResultSet. Process the ResultSet. Close everything: the ResultSet, the Statement, and the
Java Sqlexception Error Codes Oracle
Connection. The code will look something like this: public void handleJDBC() { DataSource dataSource = getDataSource(); Connection connection = dataSource.getConnection(); String query = "SELECT * FROM book"; PreparedStatement preparedStatement = connection.prepareStatement(query); ResultSet https://www.tutorialspoint.com/jdbc/jdbc-exceptions.htm resultSet = preparedStatement.executeQuery(); //process resultSet resultSet.close(); preparedStatement.close(); connection.close(); } This code doesn't compile, and I bet you know why - we haven't included any exception handling, and pretty much every line here can throw an exception. In Java you have to either declare or handle exceptions. But handling of exceptions in JDBC is surprisingly difficult. Today we'll show you how to handle exceptions related to the http://onewebsql.com/blog/how-to-handle-exceptions-in-jdbc Connection object. The other exceptions can be handled in a similar way. We will abbreviate this code String query = "SELECT * FROM book"; PreparedStatement preparedStatement = connection.prepareStatement(query); ResultSet resultSet = preparedStatement.executeQuery(); //process resultSet resultSet.close(); preparedStatement.close(); as operation(connection) An inexperienced programmer might try something like this: Bad Solution 1: Just add the "throws" declaration public void handleJDBC() throws SQLException { operation(connection) } Or this: Bad Solution 1A: Catch everything public void handleJDBC() { try { DataSource dataSource = getDataSource(); Connection connection = dataSource.getConnection(); operation(connection) connection.close(); } catch (SQLException e) { //do something here } } The inability of many novice programmers to write something sensible in the catch block (the do something here) is a topic for another time. So this code compiles, but it doesn't work. We could have resource leaks because, if an exception is thrown during the operation part of the code, the close statements will never be executed. The connection, and probably also the statement and resultSet, will stay open. Rule 1: Remember to close your resources. Bad Solution 2: Add "finally" clause Here, the connection is closed in the finally clause. public void handleJDBC() throws SQLException { Connection connection = null
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the http://stackoverflow.com/questions/1988570/how-to-catch-a-specific-exception-in-jdbc company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up How to catch a specific exception in JDBC? up vote 8 down vote favorite 3 How to catch a specific error codes exceptions in JDBC? Examples: primary key exception or foreign key exception. java jdbc share|improve this question edited Jul 22 '15 at 20:37 Willi Mentzel 2,33891840 asked Jan 1 '10 at 13:17 GuruKulki 10.8k27100162 add a comment| 5 Answers 5 active oldest votes up vote 7 down vote accepted SQLException contains some database-specific info related to the exception. From the doc: Each SQLException provides several kinds of information: 1) a string describing the error. This sqlexception error codes is used as the Java Exception message, available via the method getMesage. 2) a "SQLstate" string, which follows either the XOPEN SQLstate conventions or the SQL 99 conventions. The values of the SQLState string are described in the appropriate spec. The DatabaseMetaData method getSQLStateType can be used to discover whether the driver returns the XOPEN type or the SQL 99 type. 3) an integer error code that is specific to each vendor. Normally this will be the actual error code returned by the underlying database. 4) a chain to a next Exception. This can be used to provide additional error information. share|improve this answer answered Jan 1 '10 at 13:23 Brian Agnew 188k21234334 add a comment| up vote 21 down vote The best and DB-independent way to handle SQLException more specifically is to determine the SQL state code which can be obtained by SQLException#getSQLState(). The SQLState is a 5-char code, of which the first two are common among all DB's and the last three might differ depending on the DB and/or the specific condition. Here's an extract from the spec: 02: no data 07: dynamic SQL error 08: connection exception 0A: feature not supported 21: cardinality violation 22: data exception 23: integrity constraint violation 24: invalid cursor state 25: invalid transaction state 26: invalid SQL statement name