Example usage for java.sql PreparedStatement setDate

List of usage examples for java.sql PreparedStatement setDate

Introduction

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

Prototype

void setDate(int parameterIndex, java.sql.Date x) throws SQLException;

Source Link

Document

Sets the designated parameter to the given java.sql.Date value using the default time zone of the virtual machine that is running the application.

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);/*w  w w  .  ja v a2  s .co  m*/
    prest.setString(2, "R");
    prest.setDate(3, date.valueOf("1998-1-17"));
    int row = prest.executeUpdate();
}

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

    pstmt.executeUpdate();/* w  w  w.  ja v a2  s .co  m*/

    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 {
    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);

    pstmt.executeUpdate();//w w  w .j  a  v  a 2 s .  c  om

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

    while (rs.next()) {
        System.out.print(rs.getDate(2));
    }

    rs.close();
    st.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);

    pstmt.executeUpdate();/*from  ww w.jav  a2 s . c  om*/

    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[] 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);

    pstmt.executeUpdate();/*from  w  ww  .  j  a  v  a  2 s . c o m*/

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

    Calendar cal = Calendar.getInstance();

    // get the TimeZone for "America/Los_Angeles"
    TimeZone tz = TimeZone.getTimeZone("America/Los_Angeles");
    cal.setTimeZone(tz);

    while (rs.next()) {
        // the JDBC driver will use the time zone information in
        // Calendar to calculate the date, with the result that
        // the variable dateCreated contains a java.sql.Date object
        // that is accurate for "America/Los_Angeles".
        java.sql.Date dateCreated = rs.getDate(2, cal);
        System.out.println(dateCreated);
    }

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

From source file:Main.java

public static void main(String[] args) throws Exception {
    try {/*from   w  w w.j  a  v a2s .c om*/
        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);

        pstmt.executeUpdate();

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

        rs.close();
        st.close();
        conn.close();
    } catch (SQLException e) {
        while (e != null) {
            String errorMessage = e.getMessage();
            System.err.println("sql error message:" + errorMessage);

            // This vendor-independent string contains a code.
            String sqlState = e.getSQLState();
            System.err.println("sql state:" + sqlState);

            int errorCode = e.getErrorCode();
            System.err.println("error code:" + errorCode);
            // String driverName = conn.getMetaData().getDriverName();
            // System.err.println("driver name:"+driverName);
            // processDetailError(drivername, errorCode);
            e = e.getNextException();
        }

    }
}

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

    pstmt.executeUpdate();/*from   w  ww  . j  a  v  a 2s  . c om*/

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

    ResultSetMetaData rsmd = rs.getMetaData();

    int numCols = rsmd.getColumnCount();

    System.out.print("\t");
    for (int i = 1; i <= numCols; i++) {
        System.out.println(rsmd.getColumnLabel(i));
    }

    System.out.print("\nAuto Increment\t");
    for (int i = 1; i <= numCols; i++) {
        System.out.println(rsmd.isAutoIncrement(i));
    }
    for (int i = 1; i <= numCols; i++) {

        System.out.println(rsmd.isCaseSensitive(i));
    }
    System.out.print("\nSearchable\t");
    for (int i = 1; i <= numCols; i++) {

        System.out.println(rsmd.isSearchable(i));
    }
    System.out.print("\nCurrency\t");
    for (int i = 1; i <= numCols; i++) {
        System.out.println(rsmd.isCurrency(i));
    }
    System.out.print("\nAllows nulls\t");
    for (int i = 1; i <= numCols; i++) {

        System.out.println(rsmd.isNullable(i));
    }
    System.out.print("\nSigned\t");
    for (int i = 1; i <= numCols; i++) {

        System.out.println(rsmd.isSigned(i));
    }
    System.out.print("\nRead only\t");
    for (int i = 1; i <= numCols; i++) {

        System.out.println(rsmd.isReadOnly(i));
    }
    System.out.print("\nWritable\t");
    for (int i = 1; i <= numCols; i++) {

        System.out.print(rsmd.isWritable(i));
    }
    System.out.print("\nDefinitely Writable\t");
    for (int i = 1; i <= numCols; i++) {

        System.out.println(rsmd.isDefinitelyWritable(i));
    }

    conn.close();
}

From source file:DemoPreparedStatementSetDate.java

public static void main(String[] args) throws Exception {
    Connection conn = null;/*from   w  ww.j a v  a 2s. c om*/
    PreparedStatement pstmt = null;
    try {
        conn = getConnection();
        String query = "insert into date_table(id, date_column) values(?, ?)";
        pstmt = conn.prepareStatement(query);
        pstmt.setString(1, "0001");
        java.sql.Date date = getCurrentJavaSqlDate();
        pstmt.setDate(2, date);

        // 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

public static void main(String[] argv) throws Exception {
    String driverName = "com.jnetdirect.jsql.JSQLDriver";
    Class.forName(driverName);//  w  w w .ja va 2  s. 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);
    String sql = "INSERT INTO mysql_all_table(" + "col_boolean," + "col_byte," + "col_short," + "col_int,"
            + "col_long," + "col_float," + "col_double," + "col_bigdecimal," + "col_string," + "col_date,"
            + "col_time," + "col_timestamp," + "col_asciistream," + "col_binarystream," + "col_blob) "
            + "VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    PreparedStatement pstmt = connection.prepareStatement(sql);

    pstmt.setBoolean(1, true);
    pstmt.setByte(2, (byte) 123);
    pstmt.setShort(3, (short) 123);
    pstmt.setInt(4, 123);
    pstmt.setLong(5, 123L);
    pstmt.setFloat(6, 1.23F);
    pstmt.setDouble(7, 1.23D);
    pstmt.setBigDecimal(8, new BigDecimal(1.23));
    pstmt.setString(9, "a string");
    pstmt.setDate(10, new java.sql.Date(System.currentTimeMillis()));
    pstmt.setTime(11, new Time(System.currentTimeMillis()));
    pstmt.setTimestamp(12, new Timestamp(System.currentTimeMillis()));

    File file = new File("infilename1");
    FileInputStream is = new FileInputStream(file);
    pstmt.setAsciiStream(13, is, (int) file.length());

    file = new File("infilename2");
    is = new FileInputStream(file);
    pstmt.setBinaryStream(14, is, (int) file.length());

    file = new File("infilename3");
    is = new FileInputStream(file);
    pstmt.setBinaryStream(15, is, (int) file.length());

    pstmt.executeUpdate();
}

From source file:ca.uwaterloo.iss4e.algorithm.PARX.java

public static void main(String[] args) {

    String url = "jdbc:postgresql://localhost/essex";
    Properties props = new Properties();
    props.setProperty("user", "xiliu");
    props.setProperty("password", "Abcd1234");

    try {/*from www .  ja  v  a2 s .c  o  m*/
        /* Random generator = new Random();
         BigDecimal[] d = new BigDecimal[40];
         for (int i=0; i<40; ++i){
        d[i] = new BigDecimal(generator.nextInt(10));
        System.out.print(d[i].doubleValue()+"("+i+") |");
         }
         System.out.println("\n ---------------");
         Pair<double[], double[][]> values = PARX.prepareVariable(d, 11, 12, 2, 4);
         double[] Y = values.getKey();
         double X[][] = values.getValue();
         for (int i=0; i<Y.length; ++i){
                
        for (int j=0; j<X[i].length; ++j) {
            System.out.print(X[i][j] + " |");
        }
        System.out.print("|" +Y[i]);
        System.out.println();
         }
        */

        Class.forName("org.postgresql.Driver");
        Connection conn = DriverManager.getConnection(url, props);
        PreparedStatement pstmt = conn.prepareStatement(
                "select array_agg(A.reading) from (select reading from meterreading where homeid=? and readdate between ? and ? order by readdate desc, readtime desc) A ");
        pstmt.setInt(1, 19419);
        pstmt.setDate(2, java.sql.Date.valueOf("2011-04-03"));
        pstmt.setDate(3, java.sql.Date.valueOf("2011-09-07"));

        ResultSet rs = pstmt.executeQuery();

        int order = 3;

        int numOfSeasons = 24;
        int intervalOfUpdateModel = 4; //Every four hours
        int startPredict = 6 * 24;
        int numOfPointsForTraining = 24 * 5;
        double[] pointsForPredict = new double[order];

        if (rs.next()) {
            BigDecimal[] readings = (BigDecimal[]) (rs.getArray(1).getArray());
            double[][] data = new double[2][readings.length + 1];
            int i = 0;
            for (; i < startPredict; ++i) {
                data[0][i] = readings[i].doubleValue();
                data[1][i] = 0.0;
            }

            double[] beta = null;
            int updateModelCount = 0;
            for (; i < readings.length; ++i) {
                // if (updateModelCount%intervalOfUpdateModel==0) {
                // beta = PARX.computePARModel(readings, i, i+1, order, numOfSeasons);
                ++updateModelCount;
                //}
                for (int p = 0; p < order; ++p) {
                    pointsForPredict[order - 1 - p] = readings[i - p].doubleValue();
                }
                data[0][i] = readings[i].doubleValue();
                //   data[1][i+1] = PARX.predict(pointsForPredict, order, beta);
            }

            for (int j = 0; j < readings.length + 1; ++j) {
                System.out.println(data[0][j] + ", " + data[1][j]);
            }

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