Get Error Code, SQL State, Message : SQLException « Database SQL JDBC « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Database SQL JDBC » SQLException 




Get Error Code, SQL State, Message
  

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class SqlException {

  public static void main(String[] args) {

    Connection conn = null;
    Statement stmt = null;
    ResultSet rs = null;

    try {
      String driver = "oracle.jdbc.driver.OracleDriver";
      Class.forName(driver).newInstance();
      System.out.println("Connecting to database...");
      String jdbcUrl = "jdbc:oracle:thin:@localhost:1521:ORCL";
      conn = DriverManager.getConnection(jdbcUrl, "yourName""mypwd");

      stmt = conn.createStatement();

      try {
        rs = stmt.executeQuery("Select * from no_table_exisits");
      catch (SQLException seRs) {
        String exMsg = "Message from MySQL Database";
        String exSqlState = "Exception";
        SQLException mySqlEx = new SQLException(exMsg, exSqlState);
        seRs.setNextException(mySqlEx);
        throw seRs;
      }
    catch (SQLException se) {
      int count = 1;
      while (se != null) {
        System.out.println("SQLException " + count);
        System.out.println("Code: " + se.getErrorCode());
        System.out.println("SqlState: " + se.getSQLState());
        System.out.println("Error Message: " + se.getMessage());
        se = se.getNextException();
        count++;
      }
    catch (Exception e) {
      e.printStackTrace();
    }
  }
}

   
  














Related examples in the same category
1.Logging errors to a file
2.Handling a SQL Exception: how to retrieve the information in a SQLException.
3.Print the stack trace for a SQLException to STDERR.
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.