Example usage for java.sql SQLException getSQLState

List of usage examples for java.sql SQLException getSQLState

Introduction

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

Prototype

public String getSQLState() 

Source Link

Document

Retrieves the SQLState for this SQLException object.

Usage

From source file:vitro.vspEngine.service.persistence.DBCommons.java

public Vector<String> getRegisteredUserRegNames() {
    Vector<String> retVect = new Vector<String>();
    java.sql.Connection conn = null;
    try {//  w  ww .  j  av  a2  s  . c  om
        Class.forName(jdbcdriverClassName).newInstance();
        conn = DriverManager.getConnection(connString, usrStr, pwdStr);
        String echomessage = "";
        if (!conn.isClosed()) {
            //echomessage =  "Successfully connected to "+ "MySQL server using TCP/IP...";
            Statement stmt = null;
            ResultSet rs = null;
            try {
                stmt = conn.createStatement();
                if (stmt.execute("SELECT login FROM `" + dbSchemaStr + "`.`users` ")) {
                    rs = stmt.getResultSet();
                }
                if (rs != null) {
                    while (rs.next()) {
                        String registeredLogin = rs.getString("login") == null ? "" : rs.getString("login");
                        if (!registeredLogin.isEmpty() && !registeredLogin.equalsIgnoreCase("")) {
                            retVect.add(registeredLogin);
                        }
                    }
                }
            } catch (SQLException ex) {
                // handle any errors
                System.err.println("SQLException2: " + ex.getMessage());
                System.err.println("SQLState2: " + ex.getSQLState());
                System.err.println("VendorError2: " + ex.getErrorCode());
            } finally {
                // it is a good idea to release
                // resources in a finally{} block
                // in reverse-order of their creation
                // if they are no-longer needed
                if (rs != null) {
                    try {
                        rs.close();
                    } catch (SQLException sqlEx) {
                    } // ignore
                    rs = null;
                }
                if (stmt != null) {
                    try {
                        stmt.close();
                    } catch (SQLException sqlEx) {
                    } // ignore
                    stmt = null;
                }
            }
        } else {
            echomessage = "Error accessing DB server...";
        }
        System.out.println(echomessage);
    } catch (Exception e) {
        System.err.println("Exception: " + e.getMessage());
    } finally {
        try {
            if (conn != null)
                conn.close();
        } catch (SQLException e) {
        }
    }
    return retVect;

}

From source file:vitro.vspEngine.service.persistence.DBCommons.java

synchronized public void updateRcvGatewayAdTimestamp(String pGatewayRegisteredName,
        boolean removeTimeStampFlag) {
    java.sql.Connection conn = null;
    try {//from w  w w  .  ja va2s  .  c o  m
        Class.forName(jdbcdriverClassName).newInstance();
        conn = DriverManager.getConnection(connString, usrStr, pwdStr);
        String echomessage = "";
        if (!conn.isClosed()) {
            //echomessage =  "Successfully connected to "+ "MySQL server using TCP/IP...";
            Statement stmt = null;
            ResultSet rs = null;
            try {
                stmt = conn.createStatement();
                if (!removeTimeStampFlag) {
                    if (stmt.execute("UPDATE `" + dbSchemaStr
                            + "`.`registeredgateway` SET lastadvtimestamp = UNIX_TIMESTAMP(now())  WHERE registeredName=\'"
                            + pGatewayRegisteredName + "\'")) {
                        rs = stmt.getResultSet(); // TODO: this is not needed here...
                    }
                } else {
                    if (stmt.execute("UPDATE `" + dbSchemaStr
                            + "`.`registeredgateway` SET lastadvtimestamp = 0  WHERE registeredName=\'"
                            + pGatewayRegisteredName + "\'")) {
                        rs = stmt.getResultSet(); // TODO: this is not needed here...
                    }
                }
            } catch (SQLException ex) {
                // handle any errors
                System.err.println("SQLException3: " + ex.getMessage());
                System.err.println("SQLState3: " + ex.getSQLState());
                System.err.println("VendorError3: " + ex.getErrorCode());
            } finally {
                // it is a good idea to release
                // resources in a finally{} block
                // in reverse-order of their creation
                // if they are no-longer needed
                if (rs != null) {
                    try {
                        rs.close();
                    } catch (SQLException sqlEx) {
                    } // ignore
                    rs = null;
                }
                if (stmt != null) {
                    try {
                        stmt.close();
                    } catch (SQLException sqlEx) {
                    } // ignore
                    stmt = null;
                }
            }
        } else {
            echomessage = "Error accessing DB server...";
        }
        // DEBUG
        //System.out.println(echomessage);
    } catch (Exception e) {
        System.err.println("Exception: " + e.getMessage());
    } finally {
        try {
            if (conn != null)
                conn.close();
        } catch (SQLException e) {
        }
    }
}

From source file:vitro.vspEngine.service.persistence.DBCommons.java

synchronized public void deleteRegisteredGateway(String pGatewayRegisteredName) {
    StringBuilder tmpIgnoredOutput = new StringBuilder();
    if (isRegisteredGateway(pGatewayRegisteredName, tmpIgnoredOutput)) {
        java.sql.Connection conn = null;
        try {//w  ww  .ja v a 2 s .co  m
            Class.forName(jdbcdriverClassName).newInstance();
            conn = DriverManager.getConnection(connString, usrStr, pwdStr);
            String echomessage = "";
            if (!conn.isClosed()) {
                //echomessage =  "Successfully connected to "+ "MySQL server using TCP/IP...";
                Statement stmt = null;
                ResultSet rs = null;
                try {
                    stmt = conn.createStatement();

                    if (stmt.execute(
                            "DELETE FROM `" + dbSchemaStr + "`.`registeredgateway` WHERE registeredName=\'"
                                    + pGatewayRegisteredName + "\'")) {
                        rs = stmt.getResultSet(); // TODO: this is not needed here...
                    }

                } catch (SQLException ex) {
                    // handle any errors
                    System.err.println("SQLException3: " + ex.getMessage());
                    System.err.println("SQLState3: " + ex.getSQLState());
                    System.err.println("VendorError3: " + ex.getErrorCode());
                } finally {
                    // it is a good idea to release
                    // resources in a finally{} block
                    // in reverse-order of their creation
                    // if they are no-longer needed
                    if (rs != null) {
                        try {
                            rs.close();
                        } catch (SQLException sqlEx) {
                        } // ignore
                        rs = null;
                    }
                    if (stmt != null) {
                        try {
                            stmt.close();
                        } catch (SQLException sqlEx) {
                        } // ignore
                        stmt = null;
                    }
                }
            } else {
                echomessage = "Error accessing DB server...";
            }
        } catch (Exception e) {
            System.err.println("Exception: " + e.getMessage());
        } finally {
            try {
                if (conn != null)
                    conn.close();
            } catch (SQLException e) {
            }
        }
    }
}

From source file:vitro.vspEngine.service.persistence.DBCommons.java

synchronized public void insertRegisteredGateway(String pGatewayRegisteredName, String pFriendlyName) {
    StringBuilder tmpIgnoredOutput = new StringBuilder();
    if (!isRegisteredGateway(pGatewayRegisteredName, tmpIgnoredOutput)) {
        if (pFriendlyName == null || pFriendlyName.trim().isEmpty()) {
            pFriendlyName = "unnamed island";
        }//from  www  .  j  a v  a  2  s  .c o  m
        java.sql.Connection conn = null;
        try {
            Class.forName(jdbcdriverClassName).newInstance();
            conn = DriverManager.getConnection(connString, usrStr, pwdStr);
            String echomessage = "";
            if (!conn.isClosed()) {
                //echomessage =  "Successfully connected to "+ "MySQL server using TCP/IP...";
                Statement stmt = null;
                ResultSet rs = null;
                try {
                    stmt = conn.createStatement();

                    if (stmt.execute("INSERT `" + dbSchemaStr
                            + "`.`registeredgateway`(registeredName, friendlyName) VALUES (\'"
                            + pGatewayRegisteredName + "\',\'" + pFriendlyName + "\')")) {
                        rs = stmt.getResultSet(); // TODO: this is not needed here...
                    }

                } catch (SQLException ex) {
                    // handle any errors
                    System.err.println("SQLException3: " + ex.getMessage());
                    System.err.println("SQLState3: " + ex.getSQLState());
                    System.err.println("VendorError3: " + ex.getErrorCode());
                } finally {
                    // it is a good idea to release
                    // resources in a finally{} block
                    // in reverse-order of their creation
                    // if they are no-longer needed
                    if (rs != null) {
                        try {
                            rs.close();
                        } catch (SQLException sqlEx) {
                        } // ignore
                        rs = null;
                    }
                    if (stmt != null) {
                        try {
                            stmt.close();
                        } catch (SQLException sqlEx) {
                        } // ignore
                        stmt = null;
                    }
                }
            } else {
                echomessage = "Error accessing DB server...";
            }
        } catch (Exception e) {
            System.err.println("Exception: " + e.getMessage());
        } finally {
            try {
                if (conn != null)
                    conn.close();
            } catch (SQLException e) {
            }
        }
    }
}

From source file:vitro.vspEngine.service.persistence.DBCommons.java

public Vector<DBRegisteredUsers> getRegisteredUsersEntries() {
    Vector<DBRegisteredUsers> retVect = new Vector<DBRegisteredUsers>();
    java.sql.Connection conn = null;
    try {//from   w w w .j a v  a 2s  . c o m
        Class.forName(jdbcdriverClassName).newInstance();
        conn = DriverManager.getConnection(connString, usrStr, pwdStr);
        String echomessage = "";
        if (!conn.isClosed()) {
            //echomessage =  "Successfully connected to "+ "MySQL server using TCP/IP...";
            Statement stmt = null;
            ResultSet rs = null;
            try {
                stmt = conn.createStatement();
                if (stmt.execute(
                        "SELECT idusers, passwd, login, email, idrole, role_name, lastadvtimestamp, disabled, FROM_UNIXTIME(lastadvtimestamp, \'%d/%m/%Y %H:%i:%s\') lastdate FROM vitrofrontenddb.roles AS r JOIN (vitrofrontenddb.userinrolesmr as ur JOIN vitrofrontenddb.users AS u ON u.idusers = ur.iduser) ON r.idroles=ur.idrole ")) {
                    rs = stmt.getResultSet();
                }
                if (rs != null) {
                    while (rs.next()) {
                        int userId = rs.getInt("idusers");
                        String passwd = rs.getString("passwd") == null ? "" : rs.getString("passwd"); // this is the one used in registration messages
                        String loginName = rs.getString("login") == null ? "" : rs.getString("login");
                        String emailAddress = rs.getString("email") == null ? "" : rs.getString("email");
                        String role_name = rs.getString("role_name") == null ? "" : rs.getString("role_name");
                        int role = rs.getInt("idrole");
                        int lastadvtimestampInt = rs.getInt("lastadvtimestamp");
                        String lastdate = rs.getString("lastdate") == null ? "N/A" : rs.getString("lastdate");
                        Boolean status = rs.getBoolean("disabled");
                        if (!loginName.isEmpty() && !loginName.equalsIgnoreCase("")) {
                            DBRegisteredUsers entryRegisterUsers = new DBRegisteredUsers(userId, loginName,
                                    passwd, emailAddress, role, role_name, lastadvtimestampInt, lastdate,
                                    status);
                            retVect.addElement(entryRegisterUsers);
                        }
                    }
                }
            } catch (SQLException ex) {
                // handle any errors
                System.out.println("SQLException: " + ex.getMessage());
                System.out.println("SQLState: " + ex.getSQLState());
                System.out.println("VendorError: " + ex.getErrorCode());
            } finally {
                // it is a good idea to release
                // resources in a finally{} block
                // in reverse-order of their creation
                // if they are no-longer needed
                if (rs != null) {
                    try {
                        rs.close();
                    } catch (SQLException sqlEx) {
                        System.out.println("SQLException on rs close(): " + sqlEx.getMessage());
                    } // ignore
                    rs = null;
                }
                if (stmt != null) {
                    try {
                        stmt.close();
                    } catch (SQLException sqlEx) {
                        System.out.println("SQLException on stmt close(): " + sqlEx.getMessage());
                    } // ignore
                    stmt = null;
                }
            }
        } else {
            echomessage = "Error accessing DB server...";
        }
        System.out.println(echomessage);
    } catch (Exception e) {
        System.err.println("Exception: " + e.getMessage());
    } finally {
        try {
            if (conn != null)
                conn.close();
        } catch (SQLException e) {
        }
    }
    return retVect;
}

From source file:vitro.vspEngine.service.persistence.DBCommons.java

synchronized public boolean isRegisteredGateway(String pGatewayRegisteredName, StringBuilder out_descFromDB) {
    boolean retBool = false;
    java.sql.Connection conn = null;
    try {//from   w  ww . j  a  va  2  s.  c o m
        Class.forName(jdbcdriverClassName).newInstance();
        conn = DriverManager.getConnection(connString, usrStr, pwdStr);
        String echomessage = "";
        if (!conn.isClosed()) {
            //echomessage =  "Successfully connected to "+ "MySQL server using TCP/IP...";
            Statement stmt = null;
            ResultSet rs = null;
            try {
                stmt = conn.createStatement();
                if (stmt.execute(
                        "SELECT idregisteredgateway, registeredName, friendlyName, friendlyDescription, ip, listeningport, FROM_UNIXTIME(lastadvtimestamp, \"%d/%m/%Y %H:%i:%s\") lastdate FROM `"
                                + dbSchemaStr + "`.`registeredgateway` WHERE registeredName=\'"
                                + pGatewayRegisteredName + "\'")) {
                    rs = stmt.getResultSet();
                }
                if (rs != null) {
                    while (rs.next()) {
                        int gateId = rs.getInt("idregisteredgateway");
                        String registeredName = rs.getString("registeredName") == null ? ""
                                : rs.getString("registeredName"); // this is the one used in registration messages
                        String friendlyName = rs.getString("friendlyName") == null ? ""
                                : rs.getString("friendlyName");
                        String friendlyDescription = rs.getString("friendlyDescription") == null ? ""
                                : rs.getString("friendlyDescription");
                        out_descFromDB.append(friendlyName);
                        String gateIp = rs.getString("ip") == null ? "" : rs.getString("ip");
                        String gatePort = rs.getString("listeningport") == null ? ""
                                : rs.getString("listeningport");
                        String lastdate = rs.getString("lastdate") == null ? "N/A" : rs.getString("lastdate");
                        if (registeredName.equalsIgnoreCase(pGatewayRegisteredName)) {
                            retBool = true;
                            break;
                        }
                    }
                }
            } catch (SQLException ex) {
                // handle any errors
                System.err.println("SQLException4: " + ex.getMessage());
                System.err.println("SQLState4: " + ex.getSQLState());
                System.err.println("VendorError4: " + ex.getErrorCode());
            } finally {
                // it is a good idea to release
                // resources in a finally{} block
                // in reverse-order of their creation
                // if they are no-longer needed
                if (rs != null) {
                    try {
                        rs.close();
                    } catch (SQLException sqlEx) {
                    } // ignore
                    rs = null;
                }
                if (stmt != null) {
                    try {
                        stmt.close();
                    } catch (SQLException sqlEx) {
                    } // ignore
                    stmt = null;
                }
            }
        } else {
            echomessage = "Error accessing DB server...";
        }
        // DEBUG
        //System.out.println(echomessage);
    } catch (Exception e) {
        System.err.println("Exception: " + e.getMessage());
    } finally {
        try {
            if (conn != null)
                conn.close();
        } catch (SQLException e) {
        }
    }
    return retBool;
}

From source file:vitro.vspEngine.service.persistence.DBCommons.java

public Vector<DBRegisteredGateway> getRegisteredGatewayEntries() {
    Vector<DBRegisteredGateway> retVect = new Vector<DBRegisteredGateway>();
    java.sql.Connection conn = null;
    try {/*from  w  w w.  j  av  a2  s  . co  m*/
        Class.forName(jdbcdriverClassName).newInstance();
        conn = DriverManager.getConnection(connString, usrStr, pwdStr);
        String echomessage = "";
        if (!conn.isClosed()) {
            //echomessage =  "Successfully connected to "+ "MySQL server using TCP/IP...";
            Statement stmt = null;
            ResultSet rs = null;
            try {
                stmt = conn.createStatement();
                if (stmt.execute(
                        "SELECT idregisteredgateway, registeredName, friendlyName, friendlyDescription, ip, listeningport, lastadvtimestamp, disabled, FROM_UNIXTIME(lastadvtimestamp, \'%d/%m/%Y %H:%i:%s\') lastdate FROM `"
                                + dbSchemaStr + "`.`registeredgateway` ")) {
                    rs = stmt.getResultSet();
                }
                if (rs != null) {
                    while (rs.next()) {
                        int gateId = rs.getInt("idregisteredgateway");
                        String registeredName = rs.getString("registeredName") == null ? ""
                                : rs.getString("registeredName"); // this is the one used in registration messages
                        String friendlyName = rs.getString("friendlyName") == null ? ""
                                : rs.getString("friendlyName");
                        String friendlyDescription = rs.getString("friendlyDescription") == null ? ""
                                : rs.getString("friendlyDescription");
                        String gateIp = rs.getString("ip") == null ? "" : rs.getString("ip");
                        String gatePort = rs.getString("listeningport") == null ? ""
                                : rs.getString("listeningport");
                        int lastadvtimestampInt = rs.getInt("lastadvtimestamp");
                        String lastdate = rs.getString("lastdate") == null ? "N/A" : rs.getString("lastdate");
                        Boolean status = rs.getBoolean("disabled");
                        if (!registeredName.isEmpty() && !registeredName.equalsIgnoreCase("")) {
                            DBRegisteredGateway entryRegisterGateway = new DBRegisteredGateway(gateId,
                                    registeredName, friendlyName, friendlyDescription, gateIp, gatePort,
                                    lastadvtimestampInt, lastdate, status);
                            retVect.addElement(entryRegisterGateway);
                        }
                    }
                }
            } catch (SQLException ex) {
                // handle any errors
                System.out.println("SQLException: " + ex.getMessage());
                System.out.println("SQLState: " + ex.getSQLState());
                System.out.println("VendorError: " + ex.getErrorCode());
            } finally {
                // it is a good idea to release
                // resources in a finally{} block
                // in reverse-order of their creation
                // if they are no-longer needed
                if (rs != null) {
                    try {
                        rs.close();
                    } catch (SQLException sqlEx) {
                        System.out.println("SQLException on rs close(): " + sqlEx.getMessage());
                    } // ignore
                    rs = null;
                }
                if (stmt != null) {
                    try {
                        stmt.close();
                    } catch (SQLException sqlEx) {
                        System.out.println("SQLException on stmt close(): " + sqlEx.getMessage());
                    } // ignore
                    stmt = null;
                }
            }
        } else {
            echomessage = "Error accessing DB server...";
        }
        System.out.println(echomessage);
    } catch (Exception e) {
        System.err.println("Exception: " + e.getMessage());
    } finally {
        try {
            if (conn != null)
                conn.close();
        } catch (SQLException e) {
        }
    }
    return retVect;
}

From source file:vitro.vspEngine.service.persistence.DBCommons.java

synchronized public void updateStatusUser(String pUserRegisteredName) {
    java.sql.Connection conn = null;
    try {//from  w  w w  .j a  v  a2s .  c  o  m
        String echomessage = "";
        Class.forName(jdbcdriverClassName).newInstance();
        conn = DriverManager.getConnection(connString, usrStr, pwdStr);
        if (!conn.isClosed()) {
            Statement stmt = null;
            ResultSet rs = null;
            try {
                stmt = conn.createStatement();
                if (stmt.execute(
                        "SELECT idusers, passwd, login, email, idrole, role_name, lastadvtimestamp, disabled, FROM_UNIXTIME(lastadvtimestamp, \'%d/%m/%Y %H:%i:%s\') lastdate FROM vitrofrontenddb.roles AS r JOIN (vitrofrontenddb.userinrolesmr as ur JOIN vitrofrontenddb.users AS u ON u.idusers = ur.iduser) ON r.idroles=ur.idrole ")) {
                    rs = stmt.getResultSet();
                }
                if (rs != null) {
                    while (rs.next()) {
                        int userId = rs.getInt("idusers");
                        String passwd = rs.getString("passwd") == null ? "" : rs.getString("passwd"); // this is the one used in registration messages
                        String loginName = rs.getString("login") == null ? "" : rs.getString("login");
                        String emailAddress = rs.getString("email") == null ? "" : rs.getString("email");
                        String role_name = rs.getString("role_name") == null ? "" : rs.getString("role_name");
                        int role = rs.getInt("idrole");
                        int lastadvtimestampInt = rs.getInt("lastadvtimestamp");
                        String lastdate = rs.getString("lastdate") == null ? "N/A" : rs.getString("lastdate");
                        Boolean status = rs.getBoolean("disabled");
                        if (loginName.equalsIgnoreCase(pUserRegisteredName)) {
                            if (status == false) {
                                if (stmt.execute(
                                        "UPDATE `" + dbSchemaStr + "`.`users` SET disabled = 1  WHERE login=\'"
                                                + pUserRegisteredName + "\'")) {
                                    rs = stmt.getResultSet(); // TODO: this is not needed here...
                                }
                            } else {
                                if (stmt.execute(
                                        "UPDATE `" + dbSchemaStr + "`.`users` SET disabled = 0  WHERE login=\'"
                                                + pUserRegisteredName + "\'")) {
                                    rs = stmt.getResultSet(); // TODO: this is not needed here...
                                }
                            }
                            break;
                        }
                    }
                }

            } catch (SQLException ex) {
                // handle any errors
                System.err.println("SQLException3: " + ex.getMessage());
                System.err.println("SQLState3: " + ex.getSQLState());
                System.err.println("VendorError3: " + ex.getErrorCode());
            } finally {
                // it is a good idea to release
                // resources in a finally{} block
                // in reverse-order of their creation
                // if they are no-longer needed
                if (rs != null) {
                    try {
                        rs.close();
                    } catch (SQLException sqlEx) {
                    } // ignore
                    rs = null;
                }
                if (stmt != null) {
                    try {
                        stmt.close();
                    } catch (SQLException sqlEx) {
                    } // ignore
                    stmt = null;
                }
            }
        } else {
            echomessage = "Error accessing DB server...";
        }
        // DEBUG
        //System.out.println(echomessage);
    } catch (Exception e) {
        System.err.println("Exception: " + e.getMessage());
    } finally {
        try {
            if (conn != null)
                conn.close();
        } catch (SQLException e) {
        }
    }
}

From source file:vitro.vspEngine.service.persistence.DBCommons.java

synchronized public void insertUser(String ploginName, String pemailAddress, String ppasswd, String proleName) {
    java.sql.Connection conn = null;
    try {//from   w  w  w. j  a va 2s .  c om
        String echomessage = "";
        Class.forName(jdbcdriverClassName).newInstance();
        conn = DriverManager.getConnection(connString, usrStr, pwdStr);
        if (!conn.isClosed()) {
            Statement stmt = null;
            ResultSet rs = null;
            try {
                stmt = conn.createStatement();
                if (stmt.execute(
                        "SELECT * FROM `" + dbSchemaStr + "`.`users` where `login`=\'" + ploginName + "\'")) {
                    rs = stmt.getResultSet();
                }
                if (rs == null) {
                    int refRoleId = -1;
                    int refUserId = -1;

                    if (stmt.execute("START TRANSACTION")) {
                        rs = stmt.getResultSet(); // TODO: this is not needed here...
                    }
                    if (stmt.execute("INSERT INTO `" + dbSchemaStr
                            + "`.`users` (`login`, `email`, `passwd`, `lastadvtimestamp`, `disabled`) VALUES (\'"
                            + ploginName + "\',\'" + pemailAddress + "\',\'" + ppasswd + "\',0,0)")) {
                        rs = stmt.getResultSet(); // TODO: this is not needed here...
                    }
                    if (stmt.execute("SELECT `idusers` from `" + dbSchemaStr + "`.`users` where `login`= \'"
                            + ploginName + "\'")) {
                        rs = stmt.getResultSet();
                        refUserId = rs.getInt("idusers");
                    }
                    if (stmt.execute("SELECT `idroles` from `" + dbSchemaStr + "`.`roles` where `role_name`= \'"
                            + proleName + "\'")) {
                        rs = stmt.getResultSet();
                        refRoleId = rs.getInt("idroles");
                    }
                    if (stmt.execute(
                            "INSERT INTO `" + dbSchemaStr + "`.`userinrolesmr` (`idrole`, `iduser`) VALUES (\'"
                                    + refRoleId + "\',\'" + refUserId + "\')")) {
                        rs = stmt.getResultSet(); // TODO: this is not needed here...
                    }
                    if (stmt.execute("COMMIT")) {
                        rs = stmt.getResultSet(); // TODO: this is not needed here...
                    }

                } else
                    System.err.println("The inserted value already exists");

            } catch (SQLException ex) {
                // handle any errors
                System.err.println("SQLException3: " + ex.getMessage());
                System.err.println("SQLState3: " + ex.getSQLState());
                System.err.println("VendorError3: " + ex.getErrorCode());
            } finally {
                // it is a good idea to release
                // resources in a finally{} block
                // in reverse-order of their creation
                // if they are no-longer needed
                if (rs != null) {
                    try {
                        rs.close();
                    } catch (SQLException sqlEx) {
                    } // ignore
                    rs = null;
                }
                if (stmt != null) {
                    try {
                        stmt.close();
                    } catch (SQLException sqlEx) {
                    } // ignore
                    stmt = null;
                }
            }
        } else {
            echomessage = "Error accessing DB server...";
        }
        // DEBUG
        //System.out.println(echomessage);
    } catch (Exception e) {
        System.err.println("Exception: " + e.getMessage());
    } finally {
        try {
            if (conn != null)
                conn.close();
        } catch (SQLException e) {
        }
    }
}

From source file:vitro.vspEngine.service.persistence.DBCommons.java

synchronized public void updateStatus(String pGatewayRegisteredName) {
    java.sql.Connection conn = null;
    try {//from   w w w  . j ava2s .c o m
        String echomessage = "";
        Class.forName(jdbcdriverClassName).newInstance();
        conn = DriverManager.getConnection(connString, usrStr, pwdStr);
        if (!conn.isClosed()) {
            Statement stmt = null;
            ResultSet rs = null;
            try {
                stmt = conn.createStatement();
                if (stmt.execute(
                        "SELECT idregisteredgateway, registeredName, friendlyName, friendlyDescription, ip, listeningport, lastadvtimestamp, disabled, FROM_UNIXTIME(lastadvtimestamp, \'%d/%m/%Y %H:%i:%s\') lastdate FROM `"
                                + dbSchemaStr + "`.`registeredgateway` ")) {
                    rs = stmt.getResultSet();
                }
                if (rs != null) {
                    while (rs.next()) {
                        int gateId = rs.getInt("idregisteredgateway");
                        String registeredName = rs.getString("registeredName") == null ? ""
                                : rs.getString("registeredName"); // this is the one used in registration messages
                        String friendlyName = rs.getString("friendlyName") == null ? ""
                                : rs.getString("friendlyName");
                        String friendlyDescription = rs.getString("friendlyDescription") == null ? ""
                                : rs.getString("friendlyDescription");
                        String gateIp = rs.getString("ip") == null ? "" : rs.getString("ip");
                        String gatePort = rs.getString("listeningport") == null ? ""
                                : rs.getString("listeningport");
                        int lastadvtimestampInt = rs.getInt("lastadvtimestamp");
                        String lastdate = rs.getString("lastdate") == null ? "N/A" : rs.getString("lastdate");
                        Boolean status = rs.getBoolean("disabled");
                        if (registeredName.equalsIgnoreCase(pGatewayRegisteredName)) {
                            if (status == false) {
                                if (stmt.execute("UPDATE `" + dbSchemaStr
                                        + "`.`registeredgateway` SET disabled = 1  WHERE registeredName=\'"
                                        + pGatewayRegisteredName + "\'")) {
                                    rs = stmt.getResultSet(); // TODO: this is not needed here...
                                }
                            } else {
                                if (stmt.execute("UPDATE `" + dbSchemaStr
                                        + "`.`registeredgateway` SET disabled = 0  WHERE registeredName=\'"
                                        + pGatewayRegisteredName + "\'")) {
                                    rs = stmt.getResultSet(); // TODO: this is not needed here...
                                }
                            }
                            break;
                        }
                    }
                }

            } catch (SQLException ex) {
                // handle any errors
                System.err.println("SQLException3: " + ex.getMessage());
                System.err.println("SQLState3: " + ex.getSQLState());
                System.err.println("VendorError3: " + ex.getErrorCode());
            } finally {
                // it is a good idea to release
                // resources in a finally{} block
                // in reverse-order of their creation
                // if they are no-longer needed
                if (rs != null) {
                    try {
                        rs.close();
                    } catch (SQLException sqlEx) {
                    } // ignore
                    rs = null;
                }
                if (stmt != null) {
                    try {
                        stmt.close();
                    } catch (SQLException sqlEx) {
                    } // ignore
                    stmt = null;
                }
            }
        } else {
            echomessage = "Error accessing DB server...";
        }
        // DEBUG
        //System.out.println(echomessage);
    } catch (Exception e) {
        System.err.println("Exception: " + e.getMessage());
    } finally {
        try {
            if (conn != null)
                conn.close();
        } catch (SQLException e) {
        }
    }
}