Example usage for java.sql PreparedStatement executeUpdate

List of usage examples for java.sql PreparedStatement executeUpdate

Introduction

In this page you can find the example usage for java.sql PreparedStatement executeUpdate.

Prototype

int executeUpdate() throws SQLException;

Source Link

Document

Executes the SQL statement in this PreparedStatement object, which must be an SQL Data Manipulation Language (DML) statement, such as INSERT, UPDATE or DELETE; or an SQL statement that returns nothing, such as a DDL statement.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {

    Class.forName("com.mysql.jdbc.Driver");
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctutorial", "root", "root");

    String sql = "DELETE FROM product where year_made = ?";
    PreparedStatement prest = con.prepareStatement(sql);
    prest.setInt(1, 2008);//  w w w .  j  av  a  2  s .  c  o  m
    int del = prest.executeUpdate();
    System.out.println("Number of deleted records: " + del);
    con.close();
}

From source file:Main.java

License:asdf

public static void main(String[] args) throws Exception {
    Connection conn = null;/*from w w w .  j  a v a 2  s  .  c om*/
    conn = DriverManager.getConnection("jdbc:mysql://192.168.1.3/greektest?"
            + "useUnicode=yes&characterEncoding=UTF-8" + "&user=root&password=obfuscated");
    String sql = "INSERT INTO `chars` (`value`) VALUES (?)";
    PreparedStatement pst = conn.prepareStatement(sql);
    String var1 = "asdf";
    pst.setString(1, var1);
    pst.executeUpdate();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getConnection();
    Statement stmt = conn.createStatement();

    stmt.executeUpdate("create table survey (id int, register int );");

    String sql = "INSERT INTO survey (register) VALUES(?)";
    PreparedStatement pstmt = conn.prepareStatement(sql);

    pstmt.setBoolean(1, true);/*w w  w .j  ava  2  s  . c om*/
    pstmt.executeUpdate();

    pstmt.setBoolean(1, false);
    pstmt.executeUpdate();

    ResultSet rs = stmt.executeQuery("SELECT * FROM survey");
    while (rs.next()) {
        System.out.println(rs.getString(2));
    }
    rs.close();
    stmt.close();
    conn.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getConnection();
    Statement stmt = conn.createStatement();

    stmt.executeUpdate("create table survey (id int, register int );");

    String sql = "INSERT INTO survey (register) VALUES(?)";
    PreparedStatement pstmt = conn.prepareStatement(sql);

    pstmt.setBoolean(1, true);//ww w  . jav  a  2s. com
    pstmt.executeUpdate();

    pstmt.setBoolean(1, false);
    pstmt.executeUpdate();

    ResultSet rs = stmt.executeQuery("SELECT * FROM survey");
    while (rs.next()) {
        System.out.println(rs.getString(2));
    }

    rs.close();
    stmt.close();
    conn.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String url = "jdbc:mysql://localhost/sampledb";
    String username = "root";
    String password = "";

    Class.forName("com.mysql.jdbc.Driver");
    Connection connection = DriverManager.getConnection(url, username, password);

    String sql = "DELETE FROM users WHERE user_id = ?";
    int userId = 2;

    PreparedStatement statement = connection.prepareStatement(sql);

    statement.setInt(1, userId);/*from w w  w  . j av a  2  s.  co m*/

    int rows = statement.executeUpdate();

    System.out.println(rows + " record(s) deleted.");
    connection.close();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String driverName = "com.jnetdirect.jsql.JSQLDriver";
    Class.forName(driverName);//ww w .  ja  v  a 2s. c  om

    String serverName = "127.0.0.1";
    String portNumber = "1433";
    String mydatabase = serverName + ":" + portNumber;
    String url = "jdbc:JSQLConnect://" + mydatabase;
    String username = "username";
    String password = "password";

    Connection connection = DriverManager.getConnection(url, username, password);
    Statement stmt = connection.createStatement();
    String sql = "DELETE FROM my_table WHERE col_string='a string'";
    int deleteCount = stmt.executeUpdate(sql);
    sql = "DELETE FROM my_table WHERE col_string=?";
    PreparedStatement pstmt = connection.prepareStatement(sql);
    pstmt.setString(1, "a string");
    deleteCount = pstmt.executeUpdate();
}

From source file:Main.java

License:asdf

public static void main(String[] args) throws Exception {
    Connection conn = getConnection();
    Statement stmt = conn.createStatement();

    stmt.executeUpdate("create table survey (id int, name BINARY);");

    String sql = "INSERT INTO survey (name) VALUES(?)";
    PreparedStatement pstmt = conn.prepareStatement(sql);

    pstmt.setBytes(1, "asdfasdf".getBytes());
    pstmt.executeUpdate();

    ResultSet rs = stmt.executeQuery("SELECT * FROM survey");
    while (rs.next()) {
        System.out.println(rs.getBytes(2));
    }/* ww w  . ja va  2 s. co m*/

    rs.close();
    stmt.close();
    conn.close();
}

From source file:Main.java

License:asdf

public static void main(String[] argv) throws Exception {
    Class.forName("com.mysql.jdbc.Driver");
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctutorial", "root", "root");

    String sql = "INSERT product VALUES(?,?)";
    PreparedStatement prest = con.prepareStatement(sql);
    prest.setString(1, "asdf");
    prest.setInt(2, 2009);//from  w  w w.j  a va2  s  . co  m
    int count = prest.executeUpdate();
    System.out.println(count + "row(s) affected");
    con.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getConnection();
    conn.setAutoCommit(false);//w w w  .  j ava2  s. co  m
    Statement st = conn.createStatement();

    PreparedStatement pstmt = conn.prepareStatement("create table survey (id int, name VARCHAR(30) );");

    pstmt.executeUpdate();

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

    rs.close();
    st.close();
    conn.close();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Connection con = null;/*  www  . j a va2s . c  o m*/
    PreparedStatement prepstmt;
    prepstmt = con.prepareStatement("DELETE FROM tCust " + " WHERE custId = ?");

    prepstmt.setString(1, "1");
    prepstmt.executeUpdate();

    prepstmt.close();
    con.close();
}