Example usage for java.sql PreparedStatement execute

List of usage examples for java.sql PreparedStatement execute

Introduction

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

Prototype

boolean execute() throws SQLException;

Source Link

Document

Executes the SQL statement in this PreparedStatement object, which may be any kind of SQL statement.

Usage

From source file:Main.java

public static void main(String args[]) throws Exception {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    String URL = "jdbc:odbc:dbName";
    Connection dbConn = DriverManager.getConnection(URL, "user", "pass");
    PrintWriter w = new PrintWriter(new OutputStreamWriter(System.out));
    DriverManager.setLogWriter(w);
    dbConn.close();//  w w w.jav a  2 s .co m

    PreparedStatement prepstmt;
    prepstmt = dbConn.prepareStatement("SELECT id FROM employee");
    prepstmt.execute();
    prepstmt.close();
    dbConn.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Class.forName("com.mysql.jdbc.Driver").newInstance();
    Connection conn = null;/*w w w .j  a  v  a 2  s  . co m*/
    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);
    pstmt.execute();
    conn.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection conn = DriverManager
            .getConnection("jdbc:odbc:Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};"
                    + "Dbq=C://Book1.xlsx;");

    PreparedStatement s = conn.prepareStatement("SELECT * FROM [Sheet1$] WHERE [MetricMonth] = ?");
    s.setString(1, "Jul-2013");
    s.execute();
    ResultSet rs = s.getResultSet();
    if (rs != null) {
        while (rs.next()) {
            System.out.println(rs.getInt("All"));
        }/*  w  w w. j a  va 2s  .c  o  m*/
    }
    s.close();

    conn.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    byte[] data = new byte[SIZE];
    for (int i = 0; i < SIZE; ++i) {
        data[i] = (byte) (64 + (i % 32));
    }// ww w  . j  a v a2s .c o  m
    ByteArrayInputStream stream = new ByteArrayInputStream(data);
    // DriverManager.registerDriver(new OracleDriver());
    Connection c = DriverManager.getConnection("jdbc:oracle:thin:@some_database", "user", "password");

    String sql = "DECLARE\n" + //
            "  l_line    CLOB;\n" + //
            "BEGIN\n" + //
            "  l_line := ?;\n" + //
            "  UPDATE table SET log = log || l_line || CHR(10) WHERE id = ?;\n" + //
            "END;\n"; //

    PreparedStatement stmt = c.prepareStatement(sql);
    stmt.setAsciiStream(1, stream, SIZE);
    stmt.setInt(2, 1);
    stmt.execute();
    stmt.close();
    c.commit();
    c.close();
}

From source file:Main.java

public static void main(String[] args) throws ClassNotFoundException, SQLException {
    Class.forName("com.mysql.jdbc.Driver");
    Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/booltest", "booltest", "booltest");
    conn.prepareStatement("create table booltest (id bigint, truefalse varchar(10));").execute();
    PreparedStatement stmt = conn.prepareStatement("insert into booltest (id, truefalse) values (?, ?);");
    stmt.setLong(1, (long) 123);
    stmt.setBoolean(2, true);// ww w  .  j a v  a2  s. c  o  m
    stmt.execute();
    stmt.setLong(1, (long) 456);
    stmt.setBoolean(2, false);
    stmt.execute();
    ResultSet rs = conn.createStatement().executeQuery("select id, truefalse from booltest");
    while (rs.next()) {
        System.out.println(rs.getLong(1) + " => " + rs.getBoolean(2));
    }
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    Class.forName("COM.cloudscape.core.JDBCDriver").newInstance();

    Connection conn = DriverManager.getConnection("jdbc:cloudscape:GAMETRADER");

    conn.setAutoCommit(false);/*from   w w  w .j  ava  2s. co m*/

    Statement s = conn.createStatement();
    s.executeUpdate("CREATE TABLE MANUALS(GAMEID INT, MANUAL LONG VARCHAR)");
    conn.commit();

    File file = new File("manuals.xml");
    InputStream is = new FileInputStream(file);

    PreparedStatement ps = conn.prepareStatement("INSERT INTO MANUALS VALUES(?,?)");

    ps.setInt(1, 1285757);

    ps.setAsciiStream(2, is, (int) file.length());

    ps.execute();
    conn.commit();
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
    Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL", "yourName", "mypwd");
    Statement stmt = conn.createStatement();

    String streamingDataSql = "CREATE TABLE XML_Data (id INTEGER, Data LONG)";
    try {//  ww w .  j  a va 2  s  .  c  o m
        stmt.executeUpdate("DROP TABLE XML_Data");
    } catch (SQLException se) {
        if (se.getErrorCode() == 942)
            System.out.println("Error dropping XML_Data table:" + se.getMessage());
    }
    stmt.executeUpdate(streamingDataSql);

    File f = new File("employee.xml");
    long fileLength = f.length();
    FileInputStream fis = new FileInputStream(f);
    PreparedStatement pstmt = conn.prepareStatement("INSERT INTO XML_Data VALUES (?,?)");
    pstmt.setInt(1, 100);
    pstmt.setAsciiStream(2, fis, (int) fileLength);
    pstmt.execute();
    fis.close();
    ResultSet rset = stmt.executeQuery("SELECT Data FROM XML_Data WHERE id=100");
    if (rset.next()) {
        InputStream xmlInputStream = rset.getAsciiStream(1);
        int c;
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        while ((c = xmlInputStream.read()) != -1)
            bos.write(c);
        System.out.println(bos.toString());
    }
    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, b BLOB);");

    PreparedStatement pstmt = conn.prepareStatement("INSERT INTO survey VALUES(1,?)");

    File file = new File("blob.txt");
    FileInputStream fis = new FileInputStream(file);
    pstmt.setBinaryStream(1, fis, (int) file.length());
    pstmt.execute();

    fis.close();//w ww  .  j  a va 2s . c  o m
    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, b CLOB);");

    PreparedStatement pstmt = conn.prepareStatement("INSERT INTO survey VALUES(1,?)");

    File file = new File("c:/Java_Dev/data.txt");
    FileInputStream fis = new FileInputStream(file);
    pstmt.setAsciiStream(1, fis, (int) file.length());
    pstmt.execute();

    fis.close();/*from ww w. java  2s  . c om*/
    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, b CLOB);");

    PreparedStatement pstmt = conn.prepareStatement("INSERT INTO survey VALUES(1,?)");

    File file = new File("c:/Java_Dev/data.txt");

    FileReader reader = new FileReader(file);
    pstmt.setCharacterStream(1, reader);

    pstmt.execute();

    ResultSet resultSet = pstmt.executeQuery("select b from survey ");

    File data = new File("C:\\a.txt");
    Reader dataReader = resultSet.getCharacterStream(1);
    FileWriter writer = new FileWriter(data);
    char[] buffer = new char[1];
    while (dataReader.read(buffer) > 0) {
        writer.write(buffer);/*from ww  w. j  av  a 2 s .co m*/
    }
    writer.close();

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