Example usage for java.sql SQLException printStackTrace

List of usage examples for java.sql SQLException printStackTrace

Introduction

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

Prototype

public void printStackTrace() 

Source Link

Document

Prints this throwable and its backtrace to the standard error stream.

Usage

From source file:com.wso2.raspberrypi.Util.java

public static List<KVPair> getKeyValuePairs() {
    List<KVPair> results = new ArrayList<KVPair>();

    BasicDataSource ds = getBasicDataSource();

    Connection dbConnection = null;
    PreparedStatement prepStmt = null;
    ResultSet rs = null;/*  w  ww.jav  a2s.co m*/
    try {
        dbConnection = ds.getConnection();
        prepStmt = dbConnection.prepareStatement("SELECT * FROM KV_PAIR ORDER BY k");
        rs = prepStmt.executeQuery();

        while (rs.next()) {
            results.add(new KVPair(rs.getString("k"), rs.getString("v")));
        }
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        try {
            if (dbConnection != null) {
                dbConnection.close();
            }
            if (prepStmt != null) {
                prepStmt.close();
            }
            if (rs != null) {
                rs.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    return results;
}

From source file:com.wso2.raspberrypi.Util.java

public static List<RaspberryPi> getSelectedPis() {
    List<RaspberryPi> results = new ArrayList<RaspberryPi>();

    BasicDataSource ds = getBasicDataSource();

    Connection dbConnection = null;
    PreparedStatement prepStmt = null;
    ResultSet rs = null;/*from  w  w w  .  j a v  a 2  s  .  c o  m*/
    try {
        dbConnection = ds.getConnection();
        prepStmt = dbConnection.prepareStatement("SELECT * FROM RASP_PI WHERE selected=true");
        rs = prepStmt.executeQuery();

        while (rs.next()) {
            RaspberryPi pi = toRaspberryPi(rs);
            results.add(pi);
        }
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        try {
            if (dbConnection != null) {
                dbConnection.close();
            }
            if (prepStmt != null) {
                prepStmt.close();
            }
            if (rs != null) {
                rs.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    return results;
}

From source file:com.wso2.raspberrypi.Util.java

public static RaspberryPi getRaspberryPi(String macAddress) {
    System.out.println("Listing Raspberry Pi with Mac Address: " + macAddress);
    RaspberryPi pi = null;//w  ww  . j  a v  a2 s  .  co m

    BasicDataSource ds = getBasicDataSource();

    Connection dbConnection = null;
    PreparedStatement prepStmt = null;
    ResultSet rs = null;
    try {
        dbConnection = ds.getConnection();
        prepStmt = dbConnection.prepareStatement("SELECT * FROM RASP_PI WHERE mac='" + macAddress + "'");
        rs = prepStmt.executeQuery();

        while (rs.next()) {
            pi = toRaspberryPi(rs);
            break;
        }
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        try {
            if (dbConnection != null) {
                dbConnection.close();
            }
            if (prepStmt != null) {
                prepStmt.close();
            }
            if (rs != null) {
                rs.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    return pi;
}

From source file:com.wso2.raspberrypi.Util.java

public static void registerRaspberryPi(String macAddress, String ipAddress) {
    System.out.println("Registering Raspberry Pi: " + macAddress + "/" + ipAddress);
    BasicDataSource ds = getBasicDataSource();
    Connection dbConnection = null;
    PreparedStatement prepStmt = null;
    ResultSet rs = null;//  w w  w  .  j  a v  a  2  s  .c om
    try {
        dbConnection = ds.getConnection();
        prepStmt = dbConnection.prepareStatement("SELECT * FROM RASP_PI WHERE mac='" + macAddress + "'");
        rs = prepStmt.executeQuery();

        if (rs.next()) { // If it exists
            prepStmt = dbConnection.prepareStatement("UPDATE RASP_PI SET ip='" + ipAddress + "',last_updated='"
                    + System.currentTimeMillis() + "' WHERE mac='" + macAddress + "'");
            prepStmt.executeUpdate();
        } else {
            prepStmt = dbConnection.prepareStatement("INSERT INTO RASP_PI (mac,ip,last_updated) VALUES ('"
                    + macAddress + "','" + ipAddress + "','" + System.currentTimeMillis() + "' )");
            prepStmt.execute();
        }
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        try {
            if (dbConnection != null) {
                dbConnection.close();
            }
            if (prepStmt != null) {
                prepStmt.close();
            }
            if (rs != null) {
                rs.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

From source file:com.wso2.raspberrypi.Util.java

public static void reservePi(String owner, String macAddress) {
    System.out.println("Changing owner of RPi " + macAddress + " to " + owner);
    BasicDataSource ds = getBasicDataSource();
    Connection dbConnection = null;
    PreparedStatement prepStmt = null;
    ResultSet rs = null;//from   w w w .  j  ava 2s .c  om
    try {
        dbConnection = ds.getConnection();
        prepStmt = dbConnection.prepareStatement("SELECT * FROM RASP_PI where mac='" + macAddress + "'");
        rs = prepStmt.executeQuery();
        boolean alreadyOwned = false;
        while (rs.next()) {
            String oldOwner = rs.getString("owner");
            if (oldOwner != null && !oldOwner.isEmpty()) {
                alreadyOwned = true;
            }
        }
        if (!alreadyOwned) {
            prepStmt = dbConnection.prepareStatement(
                    "UPDATE RASP_PI SET owner='" + owner + "' where mac='" + macAddress + "'");
            prepStmt.execute();
        }
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        try {
            if (dbConnection != null) {
                dbConnection.close();
            }
            if (prepStmt != null) {
                prepStmt.close();
            }
            if (rs != null) {
                rs.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

From source file:com.wso2.raspberrypi.Util.java

public static List<RaspberryPi> getRaspberryPis(String orderBy) {
    System.out.println("Listing registered Raspberry Pis...");

    if (orderBy == null) {
        orderBy = "ip";
    }//from   w w  w  .j av  a 2  s  .c  om
    List<RaspberryPi> results = new ArrayList<RaspberryPi>();

    BasicDataSource ds = getBasicDataSource();

    Connection dbConnection = null;
    PreparedStatement prepStmt = null;
    ResultSet rs = null;
    try {
        dbConnection = ds.getConnection();
        prepStmt = dbConnection.prepareStatement("SELECT * FROM RASP_PI ORDER BY " + orderBy);
        rs = prepStmt.executeQuery();

        while (rs.next()) {
            RaspberryPi pi = toRaspberryPi(rs);
            results.add(pi);
        }
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        try {
            if (dbConnection != null) {
                dbConnection.close();
            }
            if (prepStmt != null) {
                prepStmt.close();
            }
            if (rs != null) {
                rs.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    return results;
}

From source file:es.tekniker.framework.ktek.commons.mng.db.CommonsLoadFile.java

public static boolean loadData(List<String> data) {
    boolean boolOK = true;

    PersistentSession session = null;/*from  w  ww  . j  a  v  a  2  s.c o m*/
    PersistentTransaction tr = null;
    Statement st;

    StringBuffer sql = null;
    String[] dataline = null;
    String tablename = null;
    boolean boolExec = false;
    try {

        session = KTEKPersistentManager.instance().getSession();

        tr = session.beginTransaction();

        try {
            st = session.connection().createStatement();
            System.out.println(data.size());
            for (int i = 0; i < data.size(); i++) {

                dataline = data.get(i).split(";");

                log.debug("data by line " + data.get(i) + " num items " + dataline.length + " data line 0 "
                        + dataline[0]);

                tablename = dataline[0];

                tablename = tablename.trim();

                sql = null;
                if (tablename.equals(TABLE_ktek_language)) {
                    log.debug("language ");
                    sql = getSQL4TABLE_LANGUAGE(dataline);
                } else if (tablename.equals(TABLE_ktek_tpsettings))
                    sql = getSQL4TABLE_TPSETTINGS(dataline);
                else if (tablename.equals(TABLE_ktek_pathology))
                    sql = getSQL4TABLE_PATHOLOGY(dataline);
                else if (tablename.equals(TABLE_ktek_translation_text))
                    sql = getSQL4TABLE_TRANSLATION_TEXT(dataline);
                else if (tablename.equals(TABLE_ktek_user))
                    sql = getSQL4TABLE_USER(dataline);
                else if (tablename.equals(TABLE_ktek_user_ext))
                    sql = getSQL4TABLE_USER_EXT(dataline);
                else if (tablename.equals(TABLE_ktek_usersessiondata))
                    sql = getSQL4TABLE_USERSESSIONDATA(dataline);
                else {
                    log.debug("table name not found " + dataline[0]);
                }

                //log.debug(sql);

                log.debug("i : " + i + " SQL : " + sql);
                if (sql != null) {
                    boolExec = st.execute(sql.toString());

                    log.debug(" executed " + boolExec);
                }
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block

            log.debug(" SQLException " + e.getMessage());
            e.printStackTrace();
            boolOK = false;
        }

        //if(boolOK)   
        tr.commit();

        session.close();

    } catch (PersistentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return boolOK;
}

From source file:controler.DbModules.java

public static String findName(String EmailAddress) {
    String usql = "SELECT name FROM customer WHERE email_adress=\"" + EmailAddress + "\"";
    Connection conn = DbModules.getConnection();
    String name = null;/*www  .  ja  va2s  .co m*/
    try {
        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery(usql);
        if (rs.next()) {
            name = rs.getString("name");
        }
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return name;
}

From source file:controler.DbModules.java

public static int findCustomerId(String EmailAddress) {
    String usql = "SELECT cust_id FROM customer WHERE email_adress=\"" + EmailAddress + "\"";
    Connection conn = DbModules.getConnection();
    int id = 0;/*w w w  .ja  v  a 2s.c  o  m*/
    try {
        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery(usql);
        if (rs.next()) {
            id = rs.getInt("cust_id");
        }
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return id;
}

From source file:database.HashTablesTools.java

public static void createTablesIfTheyDontExists(Connection connection, String tableSequenceName,
        String tableFailureName) {

    ResultSet resultTables = null;
    try {//from   w  w w .j av a  2  s  .com

        /**
         DatabaseMetaData dmd = connection.getMetaData();
         ResultSet rs = dmd.getSchemas();
         List<String> schemas = new ArrayList<String>();
         while (rs.next()) {
         schemas.add(rs.getString("TABLE_SCHEM"));
         }
         rs.close();
         System.out.println("Schemas : ");
         for (String schema : schemas) {
         System.out.println(schema);
         }
         */
        // get database metadata
        DatabaseMetaData metaData = connection.getMetaData();
        ResultSet rsSchema = metaData.getTables(null, "ME", "%", null);
        List<String> tables = new ArrayList<String>();
        while (rsSchema.next()) {
            tables.add(rsSchema.getString(3)); // 3: table name
        }
        rsSchema.close();

        if (!tables.contains(tableSequenceName.toUpperCase())) {
            System.out.println(tableSequenceName + " dont exist");
            createTables(connection, tableSequenceName, tableFailureName);
        }
    } catch (SQLException e) {
        e.printStackTrace();
    }

}