Example usage for java.sql SQLWarning getErrorCode

List of usage examples for java.sql SQLWarning getErrorCode

Introduction

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

Prototype

public int getErrorCode() 

Source Link

Document

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

Usage

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 a va 2  s  .c  om*/

    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 {
        SQLWarning warning = connection.getWarnings();
        while (warning != null) {
            String message = warning.getMessage();
            String sqlState = warning.getSQLState();
            int errorCode = warning.getErrorCode();
            warning = warning.getNextWarning();
        }
        Statement stmt = connection.createStatement();
        warning = stmt.getWarnings();
        if (warning != null) {

        }
        ResultSet resultSet = stmt.executeQuery("SELECT * FROM my_table");
        while (resultSet.next()) {
            warning = resultSet.getWarnings();
            if (warning != null) {
            }
        }
    } catch (SQLException e) {
    }

}

From source file:Main.java

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

    try {/* w  w  w . j a va 2s  . com*/
        Connection conn = getConnection(); // get a java.sql.Connection object
        SQLWarning warning = conn.getWarnings();
        while (warning != null) {
            // process connection warning
            String message = warning.getMessage();
            String sqlState = warning.getSQLState();
            int errorCode = warning.getErrorCode();
            warning = warning.getNextWarning();
        }
    } catch (SQLException e) {
        // ignore the exception
    } finally {
        // close JDBC resources: ResultSet, Statement, Connection
    }

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String dbURL = "jdbc:odbc:Companies";
    try {/*from  w  w  w.  j av a  2  s. c  o m*/
        // Load the jdbc-odbc bridge driver
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        // Enable logging
        DriverManager.setLogWriter(new PrintWriter((System.err)));

        System.out.println("Getting Connection");
        Connection conn = DriverManager.getConnection(dbURL, "user", "password");

        SQLWarning warn = conn.getWarnings();
        while (warn != null) {
            System.out.println("SQLState: " + warn.getSQLState());
            System.out.println("Message:  " + warn.getMessage());
            System.out.println("Vendor:   " + warn.getErrorCode());
            System.out.println("");
            warn = warn.getNextWarning();
        }

        conn.close();
    } catch (ClassNotFoundException e) {
        System.out.println("Can't load driver " + e);
    } catch (SQLException e) {
        System.out.println("Database access failed " + e);
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getConnection();
    conn.setAutoCommit(false);/*from   w  w w  .java 2s  .c o m*/
    Statement st = conn.createStatement();

    st.executeUpdate("create table survey (id int, name VARCHAR(30) );");

    String INSERT_RECORD = "insert into survey(id, name) values(?,?)";

    PreparedStatement pstmt = conn.prepareStatement(INSERT_RECORD);

    pstmt.setInt(1, 1);
    pstmt.setString(2, "name");
    pstmt.executeUpdate();

    // Get warnings on PreparedStatement object
    SQLWarning warning = pstmt.getWarnings();
    while (warning != null) {
        // Process statement warnings...
        String message = warning.getMessage();
        String sqlState = warning.getSQLState();
        int errorCode = warning.getErrorCode();
        warning = warning.getNextWarning();
    }

    ResultSet rs = st.executeQuery("SELECT * FROM survey");
    outputResultSet(rs);

    rs.close();
    st.close();
    conn.close();
}

From source file:Connect.java

public static void main(String[] av) {
    String dbURL = "jdbc:odbc:Companies";
    try {//from w  ww.j  av  a2  s  . c  om
        // Load the jdbc-odbc bridge driver
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

        // Enable logging
        DriverManager.setLogWriter(new PrintWriter((System.err)));

        System.out.println("Getting Connection");
        Connection conn = DriverManager.getConnection(dbURL, "ian", ""); // user,
        // passwd

        // If a SQLWarning object is available, print its
        // warning(s). There may be multiple warnings chained.

        SQLWarning warn = conn.getWarnings();
        while (warn != null) {
            System.out.println("SQLState: " + warn.getSQLState());
            System.out.println("Message:  " + warn.getMessage());
            System.out.println("Vendor:   " + warn.getErrorCode());
            System.out.println("");
            warn = warn.getNextWarning();
        }

        // Do something with the connection here...

        conn.close(); // All done with that DB connection

    } catch (ClassNotFoundException e) {
        System.out.println("Can't load driver " + e);
    } catch (SQLException e) {
        System.out.println("Database access failed " + e);
    }
}

From source file:Main.java

static boolean checkForWarning(SQLWarning w) {
    if (w == null) {
        return false;
    }//from www . j ava  2s.  c  o m
    do {
        System.err.println("Warning:\nMessage: " + w.getMessage());
        System.err.println("SQL state: " + w.getSQLState());
        System.err.println("Vendor code: " + w.getErrorCode() + "\n----------------");
    } while ((w = w.getNextWarning()) != null);
    return true;
}

From source file:JDBCQuery.java

private static void checkForWarning(SQLWarning warn) throws SQLException {

    // If a SQLWarning object was given, display the
    // warning messages.  Note that there could be
    // multiple warnings chained together

    if (warn != null) {
        System.out.println("*** Warning ***\n");
        while (warn != null) {
            System.out.println("SQLState: " + warn.getSQLState());
            System.out.println("Message:  " + warn.getMessage());
            System.out.println("Vendor:   " + warn.getErrorCode());
            System.out.println("");
            warn = warn.getNextWarning();
        }//www.  j  a va 2 s .c  o m
    }
}

From source file:com.oracle.tutorial.jdbc.JDBCTutorialUtilities.java

public static void printWarnings(SQLWarning warning) throws SQLException {
    if (warning != null) {
        System.out.println("\n---Warning---\n");
        while (warning != null) {
            System.out.println("Message: " + warning.getMessage());
            System.out.println("SQLState: " + warning.getSQLState());
            System.out.print("Vendor error code: ");
            System.out.println(warning.getErrorCode());
            System.out.println("");
            warning = warning.getNextWarning();
        }/*from  ww w  .  j  a v a  2 s . c  o  m*/
    }
}

From source file:com.xqdev.sql.MLSQL.java

private static void addWarnings(Element meta, SQLWarning w) {
    if (w == null)
        return;/*from  w w w . j a v  a2s  . c o  m*/

    Namespace sql = meta.getNamespace();
    Element warnings = new Element("warnings", sql);
    meta.addContent(warnings);
    do {
        warnings.addContent(new Element("warning", sql).setAttribute("type", w.getClass().getName())
                .addContent(new Element("reason", sql).setText(w.getMessage()))
                .addContent(new Element("sql-state", sql).setText(w.getSQLState()))
                .addContent(new Element("vendor-code", sql).setText("" + w.getErrorCode())));
        w = w.getNextWarning();
    } while (w != null);
}

From source file:edu.lternet.pasta.doi.DOIScannerTest.java

private static Connection getConnection() throws Exception {
    Connection conn = null;//from  w  w  w  .  ja v  a 2s . com
    SQLWarning warn;

    Class.forName(dbDriver);

    // Make the database connection
    conn = DriverManager.getConnection(dbUrl, dbUser, dbPassword);

    // If a SQLWarning object is available, print its warning(s).
    // There may be multiple warnings chained.
    warn = conn.getWarnings();

    if (warn != null) {
        while (warn != null) {
            System.err.println("SQLState: " + warn.getSQLState());
            System.err.println("Message:  " + warn.getMessage());
            System.err.println("Vendor: " + warn.getErrorCode());
            warn = warn.getNextWarning();
        }
    }

    return conn;

}