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) throws SQLException;

Source Link

Document

Creates a PreparedStatement object for sending parameterized SQL statements to the database.

Usage

From source file:Main.java

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

    PreparedStatement prest = con.prepareStatement("INSERT Records VALUES(?,?,?)");
    prest.setInt(1, 1);//from  ww w .j  av  a  2s  .  com
    prest.setString(2, "R");
    prest.setDate(3, date.valueOf("1998-1-17"));
    int row = prest.executeUpdate();
}

From source file:CreateTableAllTypesInOracle.java

public static void main(String[] args) {
    PreparedStatement pstmt = null;
    Connection conn = null;
    try {/*w  w w .j  a  v  a  2s. c  o  m*/
        conn = getConnection();
        pstmt = conn.prepareStatement("CREATE TYPE varray_type is VARRAY(5) OF VARCHAR(10)");
        pstmt.executeUpdate();
        // Create an OBJECT type
        pstmt = conn.prepareStatement(
                "CREATE TYPE oracle_object is OBJECT(column_string VARCHAR(128), column_integer INTEGER)");
        pstmt.executeUpdate();

        StringBuffer allTypesTable = new StringBuffer("CREATE TABLE oracle_all_types(");
        //                    Column Name          Oracle Type              Java Type
        allTypesTable.append("column_short           SMALLINT, "); // short
        allTypesTable.append("column_int             INTEGER, "); // int
        allTypesTable.append("column_float           REAL, "); // float; can also be NUMBER
        allTypesTable.append("column_double          DOUBLE PRECISION, "); // double; can also be FLOAT or NUMBER
        allTypesTable.append("column_bigdecimal      DECIMAL(13,0), "); // BigDecimal
        allTypesTable.append("column_string          VARCHAR2(254), "); // String; can also be CHAR(n)
        allTypesTable.append("column_characterstream LONG, "); // CharacterStream or AsciiStream
        allTypesTable.append("column_bytes           RAW(2000), "); // byte[]; can also be LONG RAW(n)
        allTypesTable.append("column_binarystream    RAW(2000), "); // BinaryStream; can also be LONG RAW(n)
        allTypesTable.append("column_timestamp       DATE, "); // Timestamp
        allTypesTable.append("column_clob            CLOB, "); // Clob
        allTypesTable.append("column_blob            BLOB, "); // Blob; can also be BFILE
        allTypesTable.append("column_bfile           BFILE, "); // oracle.sql.BFILE
        allTypesTable.append("column_array           varray_type, "); // oracle.sql.ARRAY
        allTypesTable.append("column_object          oracle_object)"); // oracle.sql.OBJECT

        pstmt.executeUpdate(allTypesTable.toString());
    } catch (Exception e) {
        // creation of table failed.
        // handle the exception
        e.printStackTrace();
    }
}

From source file:EmployeeInit.java

public static void main(String[] args) throws Exception {
    Connection con;
    con = DriverManager.getConnection("jdbc:derby://localhost:1527/" + "c:\\db\\employee");

    PreparedStatement ps;/*from  w  ww.  j a v  a 2  s . c  om*/
    ps = con.prepareStatement("insert into employee(name,photo) " + "values(?,?)");
    ps.setString(1, "Duke");

    Blob blob = con.createBlob();
    ImageIcon ii = new ImageIcon("duke.png");

    ObjectOutputStream oos;
    oos = new ObjectOutputStream(blob.setBinaryStream(1));
    oos.writeObject(ii);
    oos.close();
    ps.setBlob(2, blob);
    ps.execute();
    blob.free();
    ps.close();
}

From source file:InsertDateToOracle.java

public static void main(String args[]) throws Exception {
    String INSERT_RECORD = "insert into TestDates(id, date_column, "
            + "time_column, timestamp_column) values(?, ?, ?, ?)";
    Connection conn = null;
    PreparedStatement pstmt = null;
    try {//from  ww w.j av a 2s .  c om
        conn = getConnection();
        pstmt = conn.prepareStatement(INSERT_RECORD);
        pstmt.setString(1, "001");

        java.util.Date date = new java.util.Date();
        long t = date.getTime();
        java.sql.Date sqlDate = new java.sql.Date(t);
        java.sql.Time sqlTime = new java.sql.Time(t);
        java.sql.Timestamp sqlTimestamp = new java.sql.Timestamp(t);
        System.out.println("sqlDate=" + sqlDate);
        System.out.println("sqlTime=" + sqlTime);
        System.out.println("sqlTimestamp=" + sqlTimestamp);
        pstmt.setDate(2, sqlDate);
        pstmt.setTime(3, sqlTime);
        pstmt.setTimestamp(4, sqlTimestamp);
        pstmt.executeUpdate();
    } catch (Exception e) {
        e.printStackTrace();
        System.out.println("Failed to insert the record.");
    } finally {
        pstmt.close();
        conn.close();
    }
}

From source file:GetDateFromOracle.java

public static void main(String args[]) {
    String GET_RECORD = "select date_column, time_column, " + "timestamp_column from TestDates where id = ?";
    ResultSet rs = null;//from   w  ww  . j a  va  2  s .c o m
    Connection conn = null;
    PreparedStatement pstmt = null;
    try {
        conn = getConnection();
        pstmt = conn.prepareStatement(GET_RECORD);
        pstmt.setString(1, "0001");
        rs = pstmt.executeQuery();
        while (rs.next()) {
            java.sql.Date dbSqlDate = rs.getDate(1);
            java.sql.Time dbSqlTime = rs.getTime(2);
            java.sql.Timestamp dbSqlTimestamp = rs.getTimestamp(3);
            System.out.println("dbSqlDate=" + dbSqlDate);
            System.out.println("dbSqlTime=" + dbSqlTime);
            System.out.println("dbSqlTimestamp=" + dbSqlTimestamp);
        }
    } 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 conn = getConnection();
    conn.setAutoCommit(false);// ww  w  .  j  a  v a  2s.c om
    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

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  v  a 2  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[] argv) throws Exception {
    Time time = new Time(0);

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

    String sql = "INSERT child VALUES(?,?)";
    PreparedStatement prest = con.prepareStatement(sql);
    prest.setString(1, "vinod");
    prest.setTime(2, time.valueOf("1:60:60"));
    int row = prest.executeUpdate();
    System.out.println(row + " row(s) affectec)");
}

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 = "INSERT bigdecimal VALUES(?,?)";
    PreparedStatement prest = con.prepareStatement(sql);
    prest.setString(1, "D");
    BigDecimal b = new BigDecimal("111111111111111111111111111111111");
    prest.setBigDecimal(2, b);/*from   www .java  2 s . c om*/
    prest.executeUpdate();
}

From source file:Main.java

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

    Timestamp tstamp = new Timestamp(0);

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

    String sql = "INSERT myTable VALUES(?,?)";
    PreparedStatement prest = con.prepareStatement(sql);
    prest.setString(1, "x");
    prest.setTimestamp(2, tstamp.valueOf("2009-02-24 12:51:42.11"));
    int row = prest.executeUpdate();
    System.out.println(row + " row(s) affected)");

}