Example usage for java.sql SQLException getMessage

List of usage examples for java.sql SQLException getMessage

Introduction

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

Prototype

public String getMessage() 

Source Link

Document

Returns the detail message string of this throwable.

Usage

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 {/*w w w  . j  a v  a  2 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) {
    Connection conn = null;/* w  ww . j  a va 2  s .c  o  m*/
    Statement stmt = null;
    boolean executeResult;
    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();
        conn.setAutoCommit(false);
        if (!conn.getAutoCommit())
            System.out.println("Auto-commit is set to false");
        String sql = "INSERT INTO Location VALUES(715,'Houston')";
        stmt.executeUpdate(sql);
        sql = "INSERT INTO Employees VALUES" + "(8,'K','4351',{d '2000-02-00'},715)";
        stmt.executeUpdate(sql);
        conn.commit();
    } catch (SQLException se) {
        String msg = se.getMessage();
        msg = "SQLException occured with message: " + msg;
        System.out.println(msg);
        System.out.println("Starting rollback operations...");
        try {
            conn.rollback();
        } catch (SQLException se2) {
            se2.printStackTrace();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    try {/*from   w  w  w . j av a 2  s.c  o  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:SqlWarning.java

public static void main(String[] args) {
    try {/*from  w w  w.j a va2  s. c om*/
        Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();

        String jdbcUrl = "jdbc:oracle:thin:@localhost:1521:ORCL";
        Connection conn = DriverManager.getConnection(jdbcUrl, "yourName", "mypwd");

        Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
        SQLWarning sw = null;

        ResultSet rs = stmt.executeQuery("Select * from employees");
        sw = stmt.getWarnings();
        System.out.println(sw.getMessage());

        while (rs.next()) {
            System.out.println("Employee name: " + rs.getString(2));
        }
        rs.previous();
        rs.updateString("name", "Jon");

    } catch (SQLException se) {
        System.out.println("SQLException occurred: " + se.getMessage());

    } catch (Exception e) {
        e.printStackTrace();

    }
}

From source file:Main.java

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

    String driverName = "com.jnetdirect.jsql.JSQLDriver";
    Class.forName(driverName);/*from w  ww. jav  a  2s  .  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 {
        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) throws Exception {
    String driver = "sun.jdbc.odbc.JdbcOdbcDriver";

    Connection con;//from  w  w w  .j av a 2s  . co  m
    Statement stmt;
    ResultSet uprs;

    try {
        Class.forName(driver);
        con = DriverManager.getConnection("jdbc:odbc:RainForestDSN", "student", "student");
        stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
        uprs = stmt.executeQuery("SELECT * FROM Records");

        // Check the column count
        ResultSetMetaData md = uprs.getMetaData();
        System.out.println("Resultset has " + md.getColumnCount() + " cols.");

        int rowNum = uprs.getRow();
        System.out.println("row1 " + rowNum);
        uprs.absolute(1);
        rowNum = uprs.getRow();
        System.out.println("row2 " + rowNum);
        uprs.next();
        uprs.moveToInsertRow();
        uprs.updateInt(1, 150);
        uprs.updateString(2, "Madonna");
        uprs.updateString(3, "Dummy");
        uprs.updateString(4, "Jazz");
        uprs.updateString(5, "Image");
        uprs.updateInt(6, 5);
        uprs.updateDouble(7, 5);
        uprs.updateInt(8, 15);
        uprs.insertRow();
        uprs.close();
        stmt.close();
        con.close();
    } catch (SQLException ex) {
        System.err.println("SQLException: " + ex.getMessage());
    }
}

From source file:CreateCoffees.java

public static void main(String args[]) {
    String url = "jdbc:mySubprotocol:myDataSource";
    Connection con;/*from w w w .  jav  a2  s. c  o m*/
    String createString;
    createString = "create table COFFEES " + "(COF_NAME VARCHAR(32), " + "SUP_ID INTEGER, " + "PRICE FLOAT, "
            + "SALES INTEGER, " + "TOTAL INTEGER)";
    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();
        stmt.executeUpdate(createString);
        stmt.close();
        con.close();

    } catch (SQLException ex) {
        System.err.println("SQLException: " + ex.getMessage());
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getMySQLConnection();
    Statement stmt = null;/*from w w  w . jav  a2 s .co  m*/
    try {
        stmt = conn.createStatement();
        stmt.executeUpdate("DELETE FROM Mytable");
        displayError(stmt.getWarnings());
        stmt.executeUpdate(
                "INSERT INTO Mytable(id, name)" + "VALUES(1, 'NameLongerThanColumnLengthInDatabase')");
        displayError(stmt.getWarnings());
    } catch (DataTruncation dt) {
        displayError(dt);
        dt.printStackTrace();
    } catch (SQLException se) {
        System.out.println("Database error message: " + se.getMessage());
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        stmt.close();
        conn.close();
    }
}

From source file:DemoDataTruncation.java

public static void main(String[] args) throws Exception {
    Connection conn = getMySQLConnection();
    Statement stmt = null;/*  w  w  w.  j a v a2s  . c  o  m*/
    try {
        stmt = conn.createStatement();
        stmt.executeUpdate("DELETE FROM animals_table");
        displayError(stmt.getWarnings());
        stmt.executeUpdate(
                "INSERT INTO animals_table(id, name)" + "VALUES(1, 'NameLongerThanColumnLengthInDatabase')");
        displayError(stmt.getWarnings());
    } catch (DataTruncation dt) {
        displayError(dt);
        dt.printStackTrace();
    } catch (SQLException se) {
        System.out.println("Database error message: " + se.getMessage());
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        stmt.close();
        conn.close();
    }
}

From source file:ResultSetUpdate.java

public static void main(String args[]) {

    String url;//w  w  w  .  j a  va2 s  . c o m
    url = "jdbc:odbc:UserDB";

    String user, pass;
    user = "ian";
    pass = "stjklsq";

    Connection con;
    Statement stmt;
    ResultSet rs;

    try {
        con = DriverManager.getConnection(url, user, pass);
        stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
        rs = stmt.executeQuery("SELECT * FROM Users where nick=\"ian\"");

        // Get the resultset ready, update the passwd field, commit
        rs.first();
        rs.updateString("password", "unguessable");
        rs.updateRow();

        rs.close();
        stmt.close();
        con.close();
    } catch (SQLException ex) {
        System.err.println("SQLException: " + ex.getMessage());
    }
}