Example usage for java.sql PreparedStatement setString

List of usage examples for java.sql PreparedStatement setString

Introduction

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

Prototype

void setString(int parameterIndex, String x) throws SQLException;

Source Link

Document

Sets the designated parameter to the given Java String value.

Usage

From source file:Main.java

License:asdf

public static void main(String[] args) throws Exception {
    Connection conn = null;//from  w  w  w. ja v a2s  . c  o m
    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();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    File file = new File("myimage.gif");
    FileInputStream fis = new FileInputStream(file);
    Class.forName("oracle.jdbc.driver.OracleDriver");
    Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@//server.local:1521/prod", "scott",
            "tiger");
    conn.setAutoCommit(false);//from   w  w  w.  jav  a2 s.c o  m
    PreparedStatement ps = conn.prepareStatement("insert into images values (?,?)");
    ps.setString(1, file.getName());
    ps.setBinaryStream(2, fis, (int) file.length());
    ps.executeUpdate();
    ps.close();
    fis.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);// w w w  .  j ava 2s.  co m

    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");
    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);//from  ww  w  .j  a  va  2  s .  co 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 {
    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);/* www.j a  va 2  s. c o m*/

    pstmt.executeUpdate();

    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 {
    try {/*from   w w w  . j  a v  a 2s.c  o  m*/
        String url = "jdbc:odbc:databaseName";
        String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
        String user = "guest";
        String password = "guest";
        FileInputStream fis = new FileInputStream("somefile.txt");
        Class.forName(driver);
        Connection connection = DriverManager.getConnection(url, user, password);
        Statement createTable = connection.createStatement();

        createTable.executeUpdate("CREATE TABLE source_code (name CHAR(20), source LONGTEXT)");

        String ins = "INSERT INTO source_code VALUES(?,?)";
        PreparedStatement statement = connection.prepareStatement(ins);

        statement.setString(1, "TryInputStream"); // Set first field
        statement.setAsciiStream(2, fis, fis.available()); // Stream is source

        int rowsUpdated = statement.executeUpdate();
        System.out.println("Rows affected: " + rowsUpdated);
        connection.close();
    } catch (Exception e) {
        System.err.println(e);
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Connection con = null;/*w  w  w  . j  av  a 2s .  c om*/
    PreparedStatement prepstmt = con.prepareStatement("select Name, Addr from Employee where Id = ?");
    prepstmt.setString(1, "1");

    ResultSet rs;
    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 {
    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  .j a v a 2  s . c o 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 {
    try {/*from  w  w w .  j a v  a  2s  .c o  m*/
        String url = "jdbc:odbc:databaseName";
        String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
        String user = "guest";
        String password = "guest";
        Class.forName(driver);
        Connection connection = DriverManager.getConnection(url, user, password);
        String changeLastName = "UPDATE authors SET lastname = ? WHERE authid = ?";
        PreparedStatement updateLastName = connection.prepareStatement(changeLastName);

        updateLastName.setString(1, "Martin"); // Set lastname placeholder value
        updateLastName.setInt(2, 4); // Set author ID placeholder value

        int rowsUpdated = updateLastName.executeUpdate(); // execute the update
        System.out.println("Rows affected: " + rowsUpdated);
        connection.close();
    } catch (ClassNotFoundException cnfe) {
        System.err.println(cnfe);
    } catch (SQLException sqle) {
        System.err.println(sqle);
    }
}

From source file:Main.java

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

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

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