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:DemoPreparedStatementSetTimeAndTimestamp.java

public static void main(String[] args) throws Exception {
    String id = "0001";
    Connection conn = null;
    PreparedStatement pstmt = null;
    try {//from   ww  w. j  ava2s  . com
        conn = getConnection();
        String query = "insert into time_table(id,time_column, timestamp_column) values(?, ?, ?)";
        pstmt = conn.prepareStatement(query);
        pstmt.setString(1, id);
        java.sql.Time time = getCurrentJavaSqlTime();
        System.out.println("time=" + time);
        pstmt.setTime(2, time);
        java.sql.Timestamp timestamp = getCurrentJavaSqlTimestamp();
        System.out.println("timestamp=" + timestamp);
        pstmt.setTimestamp(3, timestamp);
        // execute query, and return number of rows created
        int rowCount = pstmt.executeUpdate();
        System.out.println("rowCount=" + rowCount);
    } finally {
        pstmt.close();
        conn.close();
    }
}

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();/*  w  w w  .  j av  a  2 s  . c  o  m*/

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

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

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection dbConnection = null;
    String myConnectionString = "";
    myConnectionString = "jdbc:mysql://192.168.1.3:3306/mytestdb";
    dbConnection = DriverManager.getConnection(myConnectionString, "root", "whatever");
    PreparedStatement stmt = dbConnection.prepareStatement("SELECT * FROM jdbctest");
    ResultSet rs = stmt.executeQuery();
    int i = 0;//from w ww .ja  v  a 2 s  .c  om
    int j = 0;
    String s = "";
    while (rs.next()) {
        i++;
        j = rs.getInt("id");
        s = rs.getString("textcol");
    }
    System.out.println(String.format("Finished reading %d rows.", i));
    rs.close();
    stmt.close();
    dbConnection.close();
}

From source file:Main.java

public static final void main(String[] argv) throws Exception {
    Class.forName("oracle.jdbc.OracleDriver");
    Connection conn = DriverManager.getConnection("your_connection_string", "your_user_name", "your_password");
    Date nowDate = new Date();
    Timestamp nowTimestamp = new Timestamp(nowDate.getTime());
    PreparedStatement insertStmt = conn.prepareStatement(
            "INSERT INTO MyTable" + " (os_name, ts, ts_with_tz, ts_with_local_tz)" + " VALUES (?, ?, ?, ?)");
    insertStmt.setString(1, System.getProperty("os.name"));
    insertStmt.setTimestamp(2, nowTimestamp);
    insertStmt.setTimestamp(3, nowTimestamp);
    insertStmt.setTimestamp(4, nowTimestamp);
    insertStmt.executeUpdate();//from w w w  .j  a v a  2 s.  com
    insertStmt.close();
    System.out.println("os_name, ts, ts_with_tz, ts_with_local_tz");
    PreparedStatement selectStmt = conn
            .prepareStatement("SELECT os_name, ts, ts_with_tz, ts_with_local_tz" + " FROM MyTable");
    ResultSet result = null;
    result = selectStmt.executeQuery();
    while (result.next()) {
        System.out.println(String.format("%s,%s,%s,%s", result.getString(1), result.getTimestamp(2).toString(),
                result.getTimestamp(3).toString(), result.getTimestamp(4).toString()));
    }
    result.close();
    selectStmt.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 DECIMAL, name BINARY );");

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

    pstmt.setBigDecimal(1, new BigDecimal("1.00000"));

    // insert the data
    pstmt.executeUpdate();/*from w  w w .j  a  v  a2 s  . c o m*/

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

    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 a2s.  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 st = conn.createStatement();

    st.executeUpdate("create table survey (id int,myDate DATE);");
    String INSERT_RECORD = "insert into survey(id, myDate) values(?, ?)";

    PreparedStatement pstmt = conn.prepareStatement(INSERT_RECORD);
    pstmt.setString(1, "1");
    java.sql.Date sqlDate = new java.sql.Date(new java.util.Date().getTime());
    pstmt.setDate(2, sqlDate);//from  ww w  .  java  2  s.c om

    pstmt.executeUpdate();

    System.out.println(
            "The type of the first parameter is: " + pstmt.getParameterMetaData().getParameterTypeName(1));
    System.out.println(pstmt.getParameterMetaData().isNullable(1));

    conn.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Class.forName("com.mysql.jdbc.Driver").newInstance();
    Connection conn = null;
    conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "password");
    byte[] bkey = "This is some binary stuff".getBytes();
    String query = "INSERT INTO keytable (name, `key`) VALUES (?,?)";
    PreparedStatement pstmt = conn.prepareStatement(query);
    pstmt.setString(1, "test");
    pstmt.setBytes(2, bkey);/*from   w ww.  j av a 2  s  .c  o  m*/
    pstmt.execute();
    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, name BINARY);");

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

    File file = new File("yourFileName.txt");
    long fileLength = file.length();
    Reader fileReader = (Reader) new BufferedReader(new FileReader(file));

    pstmt.setCharacterStream(1, fileReader, (int) fileLength);

    int rowCount = pstmt.executeUpdate();

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

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

From source file:Main.java

License:asdf

public static void main(String[] args) throws Exception {
    Connection conn = null;
    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();//ww w  .  j ava2  s.c  om
}