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:TestRegisterDriverApp.java

public static void main(String args[]) {

    try {/*from  ww w .  j  av  a 2  s.  c  o  m*/
        DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
    } catch (SQLException e) {
        System.out.println("Oops! Got a SQL error: " + e.getMessage());
        System.exit(1);
    }

    Connection conn = null;
    Statement stmt = null;
    ResultSet rset = null;
    try {
        conn = DriverManager.getConnection("jdbc:oracle:thin:@dssw2k01:1521:orcl", "scott", "tiger");

        stmt = conn.createStatement();
        rset = stmt.executeQuery("select 'Hello '||USER||'!' result from dual");
        while (rset.next())
            System.out.println(rset.getString(1));
        rset.close();
        rset = null;
        stmt.close();
        stmt = null;
        conn.close();
        conn = null;
    } catch (SQLException e) {
        System.out.println("Darn! A SQL error: " + e.getMessage());
    } finally {
        if (rset != null)
            try {
                rset.close();
            } catch (SQLException ignore) {
            }
        if (stmt != null)
            try {
                stmt.close();
            } catch (SQLException ignore) {
            }
        if (conn != null)
            try {
                conn.close();
            } catch (SQLException ignore) {
            }
    }
}

From source file:CreateCoffees.java

public static void main(String args[]) {

    String url = "jdbc:mySubprotocol:myDataSource";
    Connection con;/*from   w  w  w .ja v  a 2s. c o m*/
    String createString;
    createString = "create table COFFEES " + "(COF_NAME varchar(32), " + "SUP_ID int, " + "PRICE float, "
            + "SALES int, " + "TOTAL int)";
    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:CreateSuppliers.java

public static void main(String args[]) {
    String url = "jdbc:mySubprotocol:myDataSource";
    Connection con;//w  ww  .j  av a  2s  .  c  o m
    String createString;
    createString = "create table SUPPLIERS " + "(SUP_ID int, " + "SUP_NAME varchar(40), "
            + "STREET varchar(40), " + "CITY varchar(20), " + "STATE char(2), ZIP char(5))";

    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 {
    try {//from  w  ww.  ja va  2  s  .  co  m
        Connection conn = getConnection();
        Statement st = conn.createStatement();

        st.executeUpdate("create table survey (id int,myDate DATE );");
        String INSERT_RECORD = "insert into survey(id, myDate) values(?, ?)";

        PreparedStatement pstmt = conn.prepareStatement(INSERT_RECORD);
        pstmt.setString(1, "1");
        java.sql.Date sqlDate = new java.sql.Date(new java.util.Date().getTime());
        pstmt.setDate(2, sqlDate);

        pstmt.executeUpdate();

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

        rs.close();
        st.close();
        conn.close();
    } catch (SQLException e) {
        while (e != null) {
            String errorMessage = e.getMessage();
            System.err.println("sql error message:" + errorMessage);

            // This vendor-independent string contains a code.
            String sqlState = e.getSQLState();
            System.err.println("sql state:" + sqlState);

            int errorCode = e.getErrorCode();
            System.err.println("error code:" + errorCode);
            // String driverName = conn.getMetaData().getDriverName();
            // System.err.println("driver name:"+driverName);
            // processDetailError(drivername, errorCode);
            e = e.getNextException();
        }

    }
}

From source file:TestBatchUpdate.java

public static void main(String args[]) {
    Connection conn = null;//from  w ww .  j a v a  2s .  co m
    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:CreateStores.java

public static void main(String args[]) {

    String url = "jdbc:mySubprotocol:myDataSource";
    Connection con;//  w w  w . ja  v  a  2 s. com
    String createTable;
    String createArray;
    createArray = "CREATE TYPE COF_ARRAY AS ARRAY(10) OF VARCHAR(40)";
    createTable = "CREATE TABLE STORES ( " + "STORE_NO INTEGER, LOCATION ADDRESS, "
            + "COF_TYPES COF_ARRAY, MGR REF MANAGER )";
    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(createArray);
        stmt.executeUpdate(createTable);

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

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

From source file:TypeInfo.java

public static void main(String args[]) {

    String url = "jdbc:mySubprotocol:myDataSource";
    Connection con;/* ww  w  .  j  a  va 2s.  c  om*/
    DatabaseMetaData dbmd;

    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");

        dbmd = con.getMetaData();

        ResultSet rs = dbmd.getTypeInfo();
        while (rs.next()) {
            String typeName = rs.getString("TYPE_NAME");
            short dataType = rs.getShort("DATA_TYPE");
            String createParams = rs.getString("CREATE_PARAMS");
            int nullable = rs.getInt("NULLABLE");
            boolean caseSensitive = rs.getBoolean("CASE_SENSITIVE");
            System.out.println("DBMS type " + typeName + ":");
            System.out.println("     java.sql.Types:  " + dataType);
            System.out.print("     parameters used to create: ");
            System.out.println(createParams);
            System.out.println("     nullable?:  " + nullable);
            System.out.print("     case sensitive?:  ");
            System.out.println(caseSensitive);
            System.out.println("");

        }

        con.close();

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

From source file:InsertSuppliers.java

public static void main(String args[]) {

    String url = "jdbc:mySubprotocol:myDataSource";
    Connection con;//www  .  jav  a 2  s .  c  o  m
    Statement stmt;
    String query = "select SUP_NAME, SUP_ID from SUPPLIERS";

    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("insert into SUPPLIERS " + "values(49, 'Superior Coffee', '1 Party Place', "
                + "'Mendocino', 'CA', '95460')");

        stmt.executeUpdate("insert into SUPPLIERS " + "values(101, 'Acme, Inc.', '99 Market Street', "
                + "'Groundsville', 'CA', '95199')");

        stmt.executeUpdate("insert into SUPPLIERS " + "values(150, 'The High Ground', '100 Coffee Lane', "
                + "'Meadows', 'CA', '93966')");

        ResultSet rs = stmt.executeQuery(query);

        System.out.println("Suppliers and their ID Numbers:");
        while (rs.next()) {
            String s = rs.getString("SUP_NAME");
            int n = rs.getInt("SUP_ID");
            System.out.println(s + "   " + n);
        }

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

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

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 {// w  w  w.j av  a 2 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:InsertCoffees.java

public static void main(String args[]) {

    String url = "jdbc:mySubprotocol:myDataSource";
    Connection con;/*from   w  ww .j a  v  a 2 s . c om*/
    Statement stmt;
    String query = "select COF_NAME, PRICE from COFFEES";

    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("insert into COFFEES " + "values('Colombian', 00101, 7.99, 0, 0)");

        stmt.executeUpdate("insert into COFFEES " + "values('French_Roast', 00049, 8.99, 0, 0)");

        stmt.executeUpdate("insert into COFFEES " + "values('Espresso', 00150, 9.99, 0, 0)");

        stmt.executeUpdate("insert into COFFEES " + "values('Colombian_Decaf', 00101, 8.99, 0, 0)");

        stmt.executeUpdate("insert into COFFEES " + "values('French_Roast_Decaf', 00049, 9.99, 0, 0)");

        ResultSet rs = stmt.executeQuery(query);

        System.out.println("Coffee Break Coffees and Prices:");
        while (rs.next()) {
            String s = rs.getString("COF_NAME");
            float f = rs.getFloat("PRICE");
            System.out.println(s + "   " + f);
        }

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

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