Example usage for java.sql SQLException getErrorCode

List of usage examples for java.sql SQLException getErrorCode

Introduction

In this page you can find the example usage for java.sql SQLException getErrorCode.

Prototype

public int getErrorCode() 

Source Link

Document

Retrieves the vendor-specific exception code for this SQLException object.

Usage

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
    Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL", "yourName", "mypwd");
    Statement stmt = conn.createStatement();

    String streamingDataSql = "CREATE TABLE XML_Data (id INTEGER, Data LONG)";
    try {//from  w w  w .  jav  a2 s  .c o m
        stmt.executeUpdate("DROP TABLE XML_Data");
    } catch (SQLException se) {
        if (se.getErrorCode() == 942)
            System.out.println("Error dropping XML_Data table:" + se.getMessage());
    }
    stmt.executeUpdate(streamingDataSql);

    File f = new File("employee.xml");
    long fileLength = f.length();
    FileInputStream fis = new FileInputStream(f);
    PreparedStatement pstmt = conn.prepareStatement("INSERT INTO XML_Data VALUES (?,?)");
    pstmt.setInt(1, 100);
    pstmt.setAsciiStream(2, fis, (int) fileLength);
    pstmt.execute();
    fis.close();
    ResultSet rset = stmt.executeQuery("SELECT Data FROM XML_Data WHERE id=100");
    if (rset.next()) {
        InputStream xmlInputStream = rset.getAsciiStream(1);
        int c;
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        while ((c = xmlInputStream.read()) != -1)
            bos.write(c);
        System.out.println(bos.toString());
    }
    conn.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String url = "jdbc:odbc:databaseName";
    String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
    String user = "guest";
    String password = "guest";

    String theStatement = "SELECT lastname, firstname FROM autors";

    try {/* www  .j  a v a2 s  . c  o  m*/
        Class.forName(driver);
        Connection connection = DriverManager.getConnection(url, user, password);
        Statement queryAuthors = connection.createStatement();
        ResultSet theResults = queryAuthors.executeQuery(theStatement);

        queryAuthors.close();
    } catch (ClassNotFoundException cnfe) {
        System.err.println(cnfe);
    } catch (SQLException sqle) {
        String sqlMessage = sqle.getMessage();
        String sqlState = sqle.getSQLState();
        int vendorCode = sqle.getErrorCode();
        System.err.println("Exception occurred:");
        System.err.println("Message: " + sqlMessage);
        System.err.println("SQL state: " + sqlState);
        System.err.println("Vendor code: " + vendorCode + "\n----------------");
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    try {//from w  ww .j  a v a2  s  .  co m
        Connection conn = getHSQLConnection();

        conn.setAutoCommit(false);
        Statement st = conn.createStatement();
        st.executeUpdate("create table survey (id int,name varchar(30));");
        st.executeUpdate("insert into survey (id,name ) values (1,'nameValue')");
        st = conn.createStatement();
        ResultSet rs = st.executeQuery("SELECT * FROM survey");

        outputResultSet(rs);

        rs.close();
        st.close();
        conn.close();
    } catch (SQLException sqle) {
        String sqlMessage = sqle.getMessage();
        String sqlState = sqle.getSQLState();
        int vendorCode = sqle.getErrorCode();
        System.err.println("Exception occurred:");
        System.err.println("Message: " + sqlMessage);
        System.err.println("SQL state: " + sqlState);
        System.err.println("Vendor code: " + vendorCode + "\n----------------");
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {

    String driverName = "com.jnetdirect.jsql.JSQLDriver";
    Class.forName(driverName);/*  w  w  w.  j  ava2 s .c  o m*/

    String serverName = "127.0.0.1";
    String portNumber = "1433";
    String mydatabase = serverName + ":" + portNumber;
    String url = "jdbc:JSQLConnect://" + mydatabase;
    String username = "username";
    String password = "password";

    Connection connection = DriverManager.getConnection(url, username, password);

    try {
        connection.createStatement().execute("select wrong");
    } catch (SQLException e) {
        while (e != null) {
            String message = e.getMessage();

            String sqlState = e.getSQLState();

            int errorCode = e.getErrorCode();

            driverName = connection.getMetaData().getDriverName();
            if (driverName.equals("Oracle JDBC Driver") && errorCode == 123) {
            }

            e = e.getNextException();
        }
    }

}

From source file:Main.java

public static void main(String[] args) {
    Connection conn = null;/*from  www.  j  a v  a  2  s .  c  om*/
    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();
    }
}

From source file:com.redhat.satellite.search.DeleteIndexes.java

/**
 * @param args//  w  w w  .  j ava 2 s .  c  om
 */
public static void main(String[] args) {
    try {
        Configuration config = new Configuration();
        DatabaseManager databaseManager = new DatabaseManager(config);
        String indexWorkDir = config.getString("search.index_work_dir", null);
        if (StringUtils.isBlank(indexWorkDir)) {
            log.warn("Couldn't find path for where index files are stored.");
            log.warn("Looked in config for property: search.index_work_dir");
            return;
        }
        List<IndexInfo> indexes = new ArrayList<IndexInfo>();
        indexes.add(new IndexInfo("deleteLastErrata", indexWorkDir + File.separator + "errata"));
        indexes.add(new IndexInfo("deleteLastPackage", indexWorkDir + File.separator + "package"));
        indexes.add(new IndexInfo("deleteLastServer", indexWorkDir + File.separator + "server"));
        indexes.add(new IndexInfo("deleteLastHardwareDevice", indexWorkDir + File.separator + "hwdevice"));
        indexes.add(new IndexInfo("deleteLastSnapshotTag", indexWorkDir + File.separator + "snapshotTag"));
        indexes.add(new IndexInfo("deleteLastServerCustomInfo",
                indexWorkDir + File.separator + "serverCustomInfo"));
        indexes.add(new IndexInfo("deleteLastXccdfIdent", indexWorkDir + File.separator + "xccdfIdent"));
        for (IndexInfo info : indexes) {
            deleteQuery(databaseManager, info.getQueryName());
            if (!deleteIndexPath(info.getDirPath())) {
                log.warn("Failed to delete index for " + info.getDirPath());
            }
        }
    } catch (SQLException e) {
        log.error("Caught Exception: ", e);
        if (e.getErrorCode() == 17002) {
            log.error("Unable to establish database connection.");
            log.error("Ensure database is available and connection details are " + "correct, then retry");
        }
        System.exit(1);
    } catch (IOException e) {
        log.error("Caught Exception: ", e);
        System.exit(1);
    }
    log.info("Index files have been deleted and database has been cleaned up, " + "ready to reindex");
}

From source file:CreateUDTs.java

public static void main(String args[]) {

    String url = "jdbc:mySubprotocol:myDataSource";
    Connection con;//from  w  ww.  jav a  2s .  c  om
    Statement stmt;

    String createAddress = "CREATE TYPE ADDRESS (NUM INTEGER, " + "STREET VARCHAR(40), CITY VARCHAR(40), "
            + "STATE CHAR(2), ZIP CHAR(5))";

    String createManager = "CREATE TYPE MANAGER (MGR_ID INTEGER, "
            + "LAST_NAME VARCHAR(40), FIRST_NAME VARCHAR(40), " + "PHONE char(10))";

    try {

        Class.forName("myDriver.ClassName");

    } catch (java.lang.ClassNotFoundException e) {
        System.err.print("ClassNotFoundException: ");
        System.err.println(e.getMessage());
    }

    try {
        con = DriverManager.getConnection(url, "myLogin", "myPassword");

        stmt = con.createStatement();

        stmt.executeUpdate(createAddress);
        stmt.executeUpdate("CREATE TYPE PHONE_NO AS CHAR(10)");
        stmt.executeUpdate(createManager);

        stmt.close();
        con.close();

    } catch (SQLException ex) {
        System.err.println("-----SQLException-----");
        System.err.println("SQLState:  " + ex.getSQLState());
        System.err.println("Message:  " + ex.getMessage());
        System.err.println("Vendor:  " + ex.getErrorCode());
    }
}

From source file:TestBatchUpdate.java

public static void main(String args[]) {
    Connection conn = null;/*from  w w w  .  j a v  a 2 s .  com*/
    Statement stmt = null;
    ResultSet rs = null;
    try {
        conn = getConnection();
        stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
        conn.setAutoCommit(false);
        stmt.addBatch("INSERT INTO batch_table(id, name) VALUES('11', 'A')");
        stmt.addBatch("INSERT INTO batch_table(id, name) VALUES('22', 'B')");
        stmt.addBatch("INSERT INTO batch_table(id, name) VALUES('33', 'C')");
        int[] updateCounts = stmt.executeBatch();
        conn.commit();

        rs = stmt.executeQuery("SELECT * FROM batch_table");
        while (rs.next()) {
            String id = rs.getString("id");
            String name = rs.getString("name");
            System.out.println("id=" + id + "  name=" + name);
        }

    } catch (BatchUpdateException b) {
        System.err.println("SQLException: " + b.getMessage());
        System.err.println("SQLState: " + b.getSQLState());
        System.err.println("Message: " + b.getMessage());
        System.err.println("Vendor error code: " + b.getErrorCode());
        System.err.print("Update counts: ");
        int[] updateCounts = b.getUpdateCounts();
        for (int i = 0; i < updateCounts.length; i++) {
            System.err.print(updateCounts[i] + " ");
        }
    } catch (SQLException ex) {
        System.err.println("SQLException: " + ex.getMessage());
        System.err.println("SQLState: " + ex.getSQLState());
        System.err.println("Message: " + ex.getMessage());
        System.err.println("Vendor error code: " + ex.getErrorCode());
    } catch (Exception e) {
        System.err.println("Exception: " + e.getMessage());
    } finally {
        try {
            rs.close();
            stmt.close();
            conn.close();
        } catch (Exception ignore) {
        }
    }
}

From source file:TypeConcurrency.java

public static void main(String args[]) {

    String url = "jdbc:mySubprotocol:myDataSource";
    Connection con;//w  w w .ja v a2s . co  m
    Statement stmt;
    try {

        Class.forName("myDriver.ClassName");

    } catch (java.lang.ClassNotFoundException e) {
        System.err.print("ClassNotFoundException: ");
        System.err.println(e.getMessage());
    }

    try {

        con = DriverManager.getConnection(url, "myLogin", "myPassword");

        stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);

        ResultSet srs = stmt.executeQuery("SELECT * FROM COFFEES");

        int type = srs.getType();

        System.out.println("srs is type " + type);

        int concur = srs.getConcurrency();

        System.out.println("srs has concurrency " + concur);
        while (srs.next()) {
            String name = srs.getString("COF_NAME");
            int id = srs.getInt("SUP_ID");
            float price = srs.getFloat("PRICE");
            int sales = srs.getInt("SALES");
            int total = srs.getInt("TOTAL");
            System.out.print(name + "   " + id + "   " + price);
            System.out.println("   " + sales + "   " + total);
        }

        srs.close();
        stmt.close();
        con.close();

    } catch (SQLException ex) {
        System.err.println("-----SQLException-----");
        System.err.println("SQLState:  " + ex.getSQLState());
        System.err.println("Message:  " + ex.getMessage());
        System.err.println("Vendor:  " + ex.getErrorCode());
    }
}

From source file:BatchUpdate.java

public static void main(String args[]) {
    String url = "jdbc:mySubprotocol:myDataSource";
    Connection con;/*from  w  w w.  jav  a 2 s. c  o  m*/
    Statement stmt;

    try {
        Class.forName("myDriver.ClassName");
    } catch (java.lang.ClassNotFoundException e) {
        System.err.print("ClassNotFoundException: ");
        System.err.println(e.getMessage());
    }

    try {
        con = DriverManager.getConnection(url, "myLogin", "myPassword");
        stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
        con.setAutoCommit(false);
        stmt.addBatch("INSERT INTO COFFEES " + "VALUES('Amaretto', 49, 9.99, 0, 0)");
        stmt.addBatch("INSERT INTO COFFEES " + "VALUES('Hazelnut', 49, 9.99, 0, 0)");
        stmt.addBatch("INSERT INTO COFFEES " + "VALUES('Amaretto_decaf', 49, 10.99, 0, 0)");
        stmt.addBatch("INSERT INTO COFFEES " + "VALUES('Hazelnut_decaf', 49, 10.99, 0, 0)");
        int[] updateCounts = stmt.executeBatch();
        ResultSet uprs = stmt.executeQuery("SELECT * FROM COFFEES");
        System.out.println("Table COFFEES after insertion:");

        while (uprs.next()) {
            String name = uprs.getString("COF_NAME");
            int id = uprs.getInt("SUP_ID");
            float price = uprs.getFloat("PRICE");
            int sales = uprs.getInt("SALES");
            int total = uprs.getInt("TOTAL");
            System.out.print(name + " " + id + " " + price);
            System.out.println(" " + sales + " " + total);
        }
        uprs.close();
        stmt.close();
        con.close();

    } catch (BatchUpdateException b) {
        System.err.println("SQLException: " + b.getMessage());
        System.err.println("SQLState: " + b.getSQLState());
        System.err.println("Message: " + b.getMessage());
        System.err.println("Vendor: " + b.getErrorCode());
        System.err.print("Update counts: ");
        int[] updateCounts = b.getUpdateCounts();
        for (int i = 0; i < updateCounts.length; i++) {
            System.err.print(updateCounts[i] + " ");
        }

    } catch (SQLException ex) {
        System.err.println("SQLException: " + ex.getMessage());
        System.err.println("SQLState: " + ex.getSQLState());
        System.err.println("Message: " + ex.getMessage());
        System.err.println("Vendor: " + ex.getErrorCode());
    }
}