Example usage for java.sql Statement executeQuery

List of usage examples for java.sql Statement executeQuery

Introduction

In this page you can find the example usage for java.sql Statement executeQuery.

Prototype

ResultSet executeQuery(String sql) throws SQLException;

Source Link

Document

Executes the given SQL statement, which returns a single ResultSet object.

Usage

From source file:geocodingsql.Main.java

public ResultSet giveName() {
    ResultSet rs = null;/*  www .  j  ava  2  s  .c  o  m*/
    Statement s = null;
    try {
        s = connection.createStatement();

        rs = s.executeQuery("SELECT * FROM words WHERE word_order < 3");
    } catch (Exception e) {
        System.out.println("Problem in searching the database 1");
    }
    return rs;
}

From source file:canreg.client.dataentry.Convert.java

public static void processChildNodes(
        canreg.client.gui.management.CanReg4MigrationInternalFrame.MigrationTask task, int dic_id,
        String dic_type, String filepath, String dictionaryfile) {
    Connection conn = null;//from  w  w w  .j a  v a2s .  c  o  m
    Statement stmt = null;
    ResultSet rs = null;

    try {
        String query = "SELECT * FROM \"" + dictionaryfile + "\"";
        conn = DriverManager.getConnection("jdbc:paradox:///" + filepath.replaceAll("\\\\", "/"));
        stmt = conn.createStatement(rs.TYPE_SCROLL_INSENSITIVE, rs.CONCUR_READ_ONLY);
        rs = stmt.executeQuery(query);

        while (rs.next()) {
            int id = Integer.parseInt(rs.getString(2).substring(0, 2));
            String optn = rs.getString(2).substring(3);
            if (dic_id + 1 == id) {
                String dic_optn = optn + "\t" + rs.getString(3) + "\n";
                txt_bw.write(dic_optn);
            }
        }
        txt_bw.write("\n");
    } catch (SQLException ex) {
        Logger.getLogger(Convert.class.getName()).log(Level.SEVERE, null, ex);
    }

    catch (IOException ex) {
        Logger.getLogger(Convert.class.getName()).log(Level.SEVERE, null, ex);
    } catch (NumberFormatException ex) {
        Logger.getLogger(Convert.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:edu.ku.brc.specify.conversion.ConvertMiscData.java

/**
 * @param oldDBConn//w w  w  . ja  v a2s.  c  o  m
 * @param newDBConn
 * @param disciplineID
 * @return
 */
public static boolean convertKUInvertsObsData(final Connection oldDBConn, final Connection newDBConn) {
    Timestamp now = new Timestamp(System.currentTimeMillis());

    IdMapperMgr.getInstance().setDBs(oldDBConn, newDBConn);
    IdMapperIFace coMapper = IdMapperMgr.getInstance().addTableMapper("collectionobjectcatalog",
            "CollectionObjectCatalogID", false);

    PreparedStatement pStmt1 = null;
    PreparedStatement pStmt2 = null;
    PreparedStatement pStmt3 = null;
    try {
        pStmt1 = newDBConn.prepareStatement(
                "INSERT INTO collectionobjectattribute (Remarks, Text1, Number1, CollectionMemberID, TimestampCreated, TimestampModified, Version) VALUES(?,?,?,?,?,?,?)",
                Statement.RETURN_GENERATED_KEYS);
        pStmt2 = newDBConn.prepareStatement(
                "UPDATE collectionobjectattribute SET Remarks=?, Text1=?, Number1=? WHERE CollectionObjectAttributeID = ?");

        pStmt3 = newDBConn.prepareStatement(
                "UPDATE collectionobject SET CollectionObjectAttributeID=? WHERE CollectionObjectID = ?");

        int cnt = 0;
        String sql = " SELECT BiologicalObjectID, Remarks, Description, Count, TimestampCreated, TimestampModified FROM observation WHERE (Remarks IS NOT NULL) OR (Description IS NOT NULL) OR (Count IS NOT NULL)";
        Statement stmt = oldDBConn.createStatement();
        ResultSet rs = stmt.executeQuery(sql);
        while (rs.next()) {
            int oldCOId = rs.getInt(1);
            Integer newCOId = coMapper.get(oldCOId);
            if (newCOId != null) {
                sql = "SELECT CollectionObjectAttributeID, CollectionMemberID FROM collectionobject WHERE CollectionObjectID = "
                        + newCOId;
                Object[] row = BasicSQLUtils.getRow(sql);
                if (row == null || row.length == 0) {
                    log.error("Couldn't get record for  newCOId " + newCOId);
                    continue;
                }

                Integer newCOAId = (Integer) row[0];
                Integer collMemId = (Integer) row[1];

                if (newCOAId != null) // Do Update
                {
                    pStmt2.setString(1, rs.getString(2));
                    pStmt2.setString(2, rs.getString(3));
                    pStmt2.setInt(3, rs.getInt(4));
                    pStmt2.setInt(4, newCOAId);
                    int rv = pStmt2.executeUpdate();
                    if (rv == 0) {
                        System.err.println("Error updating newCOAId " + newCOAId);
                    }

                } else // Do Insert
                {
                    Timestamp ts = rs.getTimestamp(5);
                    if (ts == null) {
                        ts = now;
                    }
                    pStmt1.setString(1, rs.getString(2));
                    pStmt1.setString(2, rs.getString(3));
                    pStmt1.setInt(3, rs.getInt(4));
                    pStmt1.setInt(4, collMemId);
                    pStmt1.setTimestamp(5, ts);
                    pStmt1.setTimestamp(6, rs.getTimestamp(6));
                    pStmt1.setInt(7, 1);

                    int rv = pStmt1.executeUpdate();
                    newCOAId = BasicSQLUtils.getInsertedId(pStmt1);
                    if (rv == 0) {
                        System.err.println("Error inserting newCOAId " + newCOAId);
                    }
                }

                pStmt3.setInt(1, newCOAId);
                pStmt3.setInt(2, newCOId);
                int rv = pStmt3.executeUpdate();
                if (rv == 0) {
                    System.err.println("Error updating newCOId " + newCOId);
                }

                cnt++;

            } else {
                log.error("No mapped CO for Obs.BiologicalObjectID " + oldCOId);
            }
        }
        rs.close();
        stmt.close();

        System.out.println(String.format("Updated %d ColObj Records", cnt));

        return true;

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

    } finally {
        try {
            if (pStmt1 != null)
                pStmt1.close();
            if (pStmt2 != null)
                pStmt2.close();
            if (pStmt3 != null)
                pStmt3.close();

        } catch (Exception ex) {
        }
    }

    return false;
}

From source file:AppletJDBCDrop.java

public void actionPerformed(ActionEvent e) {
    try {//from   w w  w .ja  v a 2  s.  c o m
        Statement statement = connection.createStatement();
        ResultSet rs = statement.executeQuery("DROP TABLE " + tableList.getSelectedValue());
    } catch (SQLException actionException) {
    }
}

From source file:org.drizzly.MySQLConnectionTest.java

@SuppressWarnings("CallToPrintStackTrace")
@Test//from  w w w  . ja va  2s  .  co m
public void testConnect() {
    String dbUrl = "jdbc:mysql://localhost:3306/DrizzlyDB";
    String dbClass = "com.mysql.jdbc.Driver";
    String query = "Select distinct(table_name) from INFORMATION_SCHEMA.TABLES";
    String username = "root";
    String password = "rajaguru";
    try {
        Class.forName(dbClass);
        try (Connection connection = DriverManager.getConnection(dbUrl, username, password)) {
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery(query);
            while (resultSet.next()) {
                String tableName = resultSet.getString(1);
                System.out.println("Table name : " + tableName);
            }
        }
    } catch (ClassNotFoundException | SQLException e) {
        e.printStackTrace();
    }
}

From source file:AppletJDBCDrop.java

private void loadTables() {
    Vector v = new Vector();
    try {//w ww.ja v a  2 s .  c om
        Statement statement = connection.createStatement();
        ResultSet rs = statement.executeQuery("SHOW TABLES");

        while (rs.next()) {
            v.addElement(rs.getString(1));
        }
        rs.close();
    } catch (SQLException e) {
    }
    v.addElement("acc_acc");
    v.addElement("acc_add");
    v.addElement("junk");
    tableList.setListData(v);
}

From source file:me.redstarstar.rdfx.duty.dao.jdbc.ScheduleJdbcDao.java

@Override
public int countTotalByParentId(long parentId) {
    return jdbcTemplate.execute((Statement statement) -> {
        ResultSet rs = statement.executeQuery("SELECT count(*) FROM schedule WHERE parent_id = " + parentId);
        int count = 0;
        if (rs.next()) {
            count = rs.getInt(1);//from w ww.  j  a  v a2  s . c  om
        }
        return count;
    });
}

From source file:me.redstarstar.rdfx.duty.dao.jdbc.ScheduleJdbcDao.java

@Override
public int getLastWeek() {
    return jdbcTemplate.execute((Statement statement) -> {
        ResultSet rs = statement.executeQuery("SELECT week FROM schedule ORDER BY week DESC LIMIT 0,1");
        int count = 0;
        if (rs.next()) {
            count = rs.getInt(1);//from   w w w .  jav a  2  s  . c  o m
        }
        return count;
    });
}

From source file:controladores.EnfermoController.java

private ArrayList<Enfermo> getEnfermos() {
    try {//  www  .j a  v  a2s .c  o  m
        ArrayList<Enfermo> listaEnf = new ArrayList<Enfermo>();
        Statement st = this.cn.createStatement();
        ResultSet rs = st.executeQuery("SELECT inscripcion, apellido, direccion, fecha_nac FROM enfermo");
        while (rs.next()) {
            Enfermo enf = new Enfermo();
            enf.setInscripcion(rs.getInt("INSCRIPCION"));
            enf.setApellido(rs.getString("APELLIDO"));
            enf.setDireccion(rs.getString("DIRECCION"));
            enf.setFechaNac(rs.getString("FECHA_NAC"));
            listaEnf.add(enf);
        }
        return listaEnf;
    } catch (SQLException ex) {
        System.out.println("Error leyendo enfermos: " + ex);
    }
    return null;
}

From source file:me.redstarstar.rdfx.duty.dao.jdbc.ScheduleJdbcDao.java

@Override
public Schedule getScheduleByWeek(long parentId, int week) {
    return jdbcTemplate.execute((Statement statement) -> {
        ResultSet rs = statement.executeQuery("SELECT * FROM schedule WHERE parent_id = " + parentId
                + " AND week = " + week + " ORDER BY activity_date");
        Schedule schedule = null;/*from w  w  w.j a v  a  2s .  com*/
        if (rs.next()) {
            schedule = new Schedule();
            schedule.setActivityDate(rs.getDate("activity_date").toLocalDate());
            schedule.setParentId(rs.getLong("parent_id"));
            schedule.setWeek(rs.getInt("week"));
        }
        return schedule;
    });
}