Example usage for java.sql PreparedStatement setURL

List of usage examples for java.sql PreparedStatement setURL

Introduction

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

Prototype

void setURL(int parameterIndex, java.net.URL x) throws SQLException;

Source Link

Document

Sets the designated parameter to the given java.net.URL value.

Usage

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,myURL CHAR);");
    String INSERT_RECORD = "insert into survey(id, myURL) values(?, ?)";

    PreparedStatement pstmt = conn.prepareStatement(INSERT_RECORD);
    pstmt.setString(1, "1");
    pstmt.setURL(2, new URL("http://www.java2s.com"));

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

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

    outputResultSet(rs);

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

From source file:DemoPreparedStatementSetURL.java

public static void main(String[] args) throws Exception {
    String id = "0001";
    String urlValue = "http://www.java2s.com";
    Connection conn = null;//from w  w w  . j a v a2s  .  com
    PreparedStatement pstmt = null;
    try {
        conn = getConnection();
        String query = "insert into url_table(id, url) values(?, ?)";
        pstmt = conn.prepareStatement(query);
        pstmt.setString(1, id);
        pstmt.setURL(2, new java.net.URL(urlValue));
        // 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:com.oracle.tutorial.jdbc.DatalinkSample.java

public void addURLRow(String description, String url) throws SQLException {

    PreparedStatement pstmt = null;

    try {//from  ww w . jav  a2  s .  co  m
        pstmt = this.con.prepareStatement("INSERT INTO data_repository(document_name,url) VALUES (?,?)");
        pstmt.setString(1, description);
        pstmt.setURL(2, new URL(url));
        pstmt.execute();
    } catch (SQLException sqlex) {
        JDBCTutorialUtilities.printSQLException(sqlex);
    } catch (Exception ex) {
        System.out.println("Unexpected exception");
        ex.printStackTrace();
    } finally {
        if (pstmt != null) {
            pstmt.close();
        }
    }
}

From source file:org.kawanfw.test.api.client.InsertPreparedStatementUrlTest.java

/**
 * Do a 100 row insert inside a loop/*from   w ww  . j a  v a  2 s. c  o  m*/
 * 
 * @param connection
 *            the AceQL Connection
 * 
 * @param useRawExecute
 *            if true, we will insert using execute()
 * 
 * @throws Exception
 *             it any Exception occurs
 */
public static void insertLoopPrepStatement(Connection connection, int numberToInsert, boolean useRawExecute)
        throws Exception {
    // We can now use our Remote JDBC Connection as a regular Connection!

    if (!useRawExecute) {
        connection.setAutoCommit(false);
    }

    // We will do all our remote insert in a SQL Transaction
    try {
        // 1) First create a Customer
        String sql = "insert into customer values ( ?, ?, ?, ?, ?, ?, ?, ? )";

        MessageDisplayer.display("Inserting " + numberToInsert + " customers...");

        for (int customerId = 1; customerId < numberToInsert + 1; customerId++) {
            PreparedStatement prepStatement = connection.prepareStatement(sql);
            prepStatement.setInt(1, customerId);
            prepStatement.setString(2, "Sir");
            // prepStatement.setString(3, "Jol_" + customerId);
            prepStatement.setNull(3, Types.VARCHAR);
            prepStatement.setString(4, "Smith_" + customerId);
            prepStatement.setURL(5, new URL("http://wwww.kawansoft.com"));
            prepStatement.setString(6, "JavaLand_" + customerId);
            prepStatement.setString(7, customerId + "45");
            prepStatement.setString(8, customerId + "-12345678");

            if (useRawExecute) {
                prepStatement.execute();
            } else {
                prepStatement.executeUpdate();
            }

            prepStatement.close();
        }

        MessageDisplayer.display(new Date() + " Before Commit...");

        // We do either everything in a single transaction or nothing

        if (!useRawExecute) {
            connection.commit(); // Commit is propagated on Server
        }

        MessageDisplayer.display(new Date() + " Remote Commit Done on AceQL Server!");
    } catch (Exception e) {
        e.printStackTrace();
        if (!useRawExecute) {
            connection.rollback();
        }
        throw e;
    } finally {
        connection.setAutoCommit(true);
    }

}