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 {
    Connection con = null;
    PreparedStatement prepstmt = con.prepareStatement("select Name, Addr from Employee where Id = ?");
    prepstmt.setString(1, "1");

    ResultSet rs;//from   ww w  .jav a2s .c om
    rs = prepstmt.executeQuery();

    boolean found = rs.next();
    if (found)
        System.out.println(rs.getString(1));
    prepstmt.close();

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Connection con = null;
    PreparedStatement prepstmt;//w  w w. j ava  2s.c om
    prepstmt = con.prepareStatement("DELETE FROM tCust " + " WHERE custId = ?");

    prepstmt.setString(1, "1");
    prepstmt.executeUpdate();

    prepstmt.close();
    con.close();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Connection con = null;
    PreparedStatement prepstmt;//from   ww  w.j  av  a2  s  . co  m
    prepstmt = con.prepareStatement("UPDATE employee SET Name = ? " + " WHERE Id = ?");
    prepstmt.setString(1, "Smith");
    prepstmt.setString(2, "1");
    prepstmt.executeUpdate();

    prepstmt.close();
    con.close();
}

From source file:DynaBeansExampleV2.java

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

    Connection conn = getConnection();
    PreparedStatement ps = conn
            .prepareStatement("SELECT * from movie, person " + "WHERE movie.director = person.Id");
    ResultSet rs = ps.executeQuery();

    ResultSetDynaClass rsdc = new ResultSetDynaClass(rs);

    Iterator itr = rsdc.iterator();
    while (itr.hasNext()) {
        DynaBean bean = (DynaBean) itr.next();
        System.err.println(bean.get("title"));
    }/*from   ww w .j av  a 2 s .co  m*/

    conn.close();
}

From source file:DynaBeansExampleV3.java

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

    Connection conn = getConnection();
    PreparedStatement ps = conn
            .prepareStatement("SELECT * from movie, person " + "WHERE movie.director = person.Id");
    ResultSet rs = ps.executeQuery();

    RowSetDynaClass rsdc = new RowSetDynaClass(rs);

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

    Iterator itr = rsdc.getRows().iterator();
    while (itr.hasNext()) {
        DynaBean bean = (DynaBean) itr.next();
        System.err.println(bean.get("title"));
    }

}

From source file:PreparedStmt.java

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

    String query = "SELECT * FROM Stock WHERE Item_Number = ?";
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection con = DriverManager.getConnection("jdbc:odbc:Inventory");
    PreparedStatement pstmt = con.prepareStatement(query);
    pstmt.setInt(1, 2);//from  w ww  .ja  va  2  s . c  o m
    ResultSet rs = pstmt.executeQuery();
    while (rs.next()) {
        String name = rs.getString("Name");
        String desc = rs.getString("Description");
        int qty = rs.getInt("Qty");
        float cost = rs.getFloat("Cost");
        System.out.println(name + ", " + desc + "\t: " + qty + "\t@ $" + cost);
    }
}

From source file:com.mycompany.parcinghtml.Main.java

public static void main(String[] args) throws SQLException, IOException {
    DataSource primaryDS = dataSource();

    ParsingClassPlayers psp = new ParsingClassPlayers(primaryDS);
    psp.downloadSource();/*from  ww w . j a  v  a2  s . co m*/

    ParsingAllMatches parsing = new ParsingAllMatches(primaryDS);
    parsing.parsingMatches(2014, 2014);
    Connection con = primaryDS.getConnection();
    java.sql.PreparedStatement ps = con.prepareStatement("SELECT * FROM \"MATCH\"");
    java.sql.ResultSet rs = ps.executeQuery();
    while (rs.next()) {

        System.out.println(rs.getInt("season"));
    }
    MatchManagerImpl pl = new MatchManagerImpl(primaryDS);
    System.out.print(pl.getMatchesVSSingleOpponent(2014, "Chomutov", 1).get(0).toString());
}

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);/*w  w w .  j  a  v  a2s.  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:Main.java

public static void main(String[] args) throws Exception {
    Class.forName("com.mysql.jdbc.Driver");
    Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/book", "root", "");

    PreparedStatement ps = connection.prepareStatement("UPDATE books SET title = ? WHERE id = ?");
    ps.setString(1, "Java");
    ps.setInt(2, 1);/*  ww w  .  j a v a 2  s  . c  o  m*/
    int rows = ps.executeUpdate();

    System.out.printf("%d row(s) updated!", rows);
    connection.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Class.forName("oracle.jdbc.driver.OracleDriver");
    Connection conn = DriverManager.getConnection(url, username, password);
    PreparedStatement stmt = conn.prepareStatement("SELECT name, description, data FROM documents ");
    ResultSet resultSet = stmt.executeQuery();
    while (resultSet.next()) {
        String name = resultSet.getString(1);
        String description = resultSet.getString(2);
        File data = new File("C:\\a.txt");
        Reader reader = resultSet.getCharacterStream(3);
        FileWriter writer = new FileWriter(data);
        char[] buffer = new char[1];
        while (reader.read(buffer) > 0) {
            writer.write(buffer);/*  ww  w .  java2 s  . c o m*/
        }
        writer.close();
    }
    conn.close();
}