Example usage for java.sql Connection prepareStatement

List of usage examples for java.sql Connection prepareStatement

Introduction

In this page you can find the example usage for java.sql Connection prepareStatement.

Prototype

PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) throws SQLException;

Source Link

Document

Creates a PreparedStatement object that will generate ResultSet objects with the given type and concurrency.

Usage

From source file:DemoUpdatableResultSet.java

public static void main(String[] args) {
    ResultSet rs = null;// w  w  w  .  ja va2 s  .c  o  m
    Connection conn = null;
    PreparedStatement pstmt = null;
    try {
        conn = getConnection();
        String query = "select id, name, age from employees where age > ?";
        pstmt = conn.prepareStatement(query, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
        pstmt.setInt(1, 20); // set input values
        rs = pstmt.executeQuery(); // create an updatable ResultSet
                                   // update a column value in the current row.
        rs.absolute(2); // moves the cursor to the 2nd row of rs
        rs.updateString("name", "newName"); // updates the 'name' column of row 2 to newName
        rs.updateRow(); // updates the row in the data source
                        // insert column values into the insert row.
        rs.moveToInsertRow(); // moves cursor to the insert row
        rs.updateInt(1, 1234); // 1st column id=1234
        rs.updateString(2, "newName"); // updates the 2nd column
        rs.updateInt(3, 99); // updates the 3rd column to 99
        rs.insertRow();
        rs.moveToCurrentRow();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            rs.close();
            pstmt.close();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection con = DriverManager.getConnection("jdbc:mysql://192.168.1.3/zzzTest?" + //
            "useUnicode=yes&characterEncoding=UTF-8" + //
            "&user=root&password=whatever");
    String newName = "G";
    String newEmail = "g@example.com";
    String newMobile = "444-555-2222";
    String sql = "SELECT " + //
            "id, " + //
            "name, " + //
            "email, " + //
            "mobile " + //
            "FROM registerSmsUsers " + //
            "WHERE mobile = ? " + //
            "FOR UPDATE";
    PreparedStatement pst = con.prepareStatement(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE);
    pst.setString(1, newMobile);/*from   w  w  w .j av a  2 s . c om*/
    ResultSet rs = pst.executeQuery();
    if (rs.next()) {
        rs.moveToCurrentRow();
        rs.updateString("name", newName);
        rs.updateString("email", newEmail);
        rs.updateRow();
        System.out.println("Existing row updated.");
    } else {
        rs.moveToInsertRow();
        rs.updateString("name", newName);
        rs.updateString("email", newEmail);
        rs.updateString("mobile", newMobile);
        rs.insertRow();
        System.out.println("New row inserted.");
    }
}

From source file:com.example.querybuilder.server.Jdbc.java

public static PreparedStatement prepareStatement(Connection connection, String sql, int resultSetType,
        int resultSetConcurrency) {
    try {/*  w  w w . jav  a 2 s  .  c  om*/
        return connection.prepareStatement(sql, resultSetType, resultSetConcurrency);
    } catch (SQLException e) {
        throw new SqlRuntimeException(e.toString() + "\n" + sql);
    }
}