List of usage examples for java.sql PreparedStatement executeUpdate
int executeUpdate() throws SQLException;
PreparedStatement
object, which must be an SQL Data Manipulation Language (DML) statement, such as INSERT
, UPDATE
or DELETE
; or an SQL statement that returns nothing, such as a DDL statement. From source file:Main.java
public static void main(String[] args) throws Exception { Connection con = DriverManager.getConnection("jdbc:h2:mem:"); Statement s = con.createStatement(); s.execute("CREATE TABLE Table1 (Column1 CLOB)"); InputStream is = new FileInputStream("data.txt"); Reader rdr = new InputStreamReader(is, StandardCharsets.ISO_8859_1); PreparedStatement ps = con.prepareStatement("INSERT INTO Table1 (Column1) VALUES (?)"); ps.setCharacterStream(1, rdr);/*w w w . j a v a 2 s . c o m*/ ps.executeUpdate(); ResultSet rs = s.executeQuery("SELECT Column1 FROM Table1"); int rowNumber = 0; while (rs.next()) { String str = rs.getString("Column1"); System.out.println(String.format("Row %d: CLOB is %d character(s) long.", ++rowNumber, str.length())); } rs.close(); con.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 TIMESTAMP );"); String INSERT_RECORD = "insert into survey(id) values(?)"; PreparedStatement pstmt = conn.prepareStatement(INSERT_RECORD); pstmt.setString(1, "1"); pstmt.executeUpdate(); ResultSet rs = st.executeQuery("SELECT * FROM survey"); outputResultSet(rs);/*from w w w . jav a 2s . c o m*/ pstmt.setString(1, "2"); pstmt.executeUpdate(); rs = st.executeQuery("SELECT * FROM survey"); outputResultSet(rs); rs.close(); st.close(); conn.close(); }
From source file:Main.java
public static void main(String[] argv) throws Exception { Time time = new Time(0); Class.forName("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctutorial", "root", "root"); String sql = "INSERT child VALUES(?,?)"; PreparedStatement prest = con.prepareStatement(sql); prest.setString(1, "vinod"); prest.setTime(2, time.valueOf("1:60:60")); int row = prest.executeUpdate(); System.out.println(row + " row(s) affectec)"); }
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 w w w. j a va 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 { Connection con = null;/*from w w w. j a va 2 s. c o m*/ Class.forName("oracle.jdbc.driver.OracleDriver"); con = DriverManager.getConnection("jdbc:oracle:thin:@192.201.32.92:1521:psprd1", "username", "password"); String query = null; ResultSet rset = null; query = "UPDATE t1 " + " SET id = ?"; PreparedStatement stmt = con.prepareStatement(query); // stmt.setInt(paramIndex++, null); stmt.setNull(1, java.sql.Types.INTEGER); stmt.executeUpdate(); stmt.close(); query = "select id from t1 "; stmt = con.prepareStatement(query); rset = stmt.executeQuery(); rset.next(); System.out.println(rset.getString("id")); rset.close(); stmt.close(); con.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 . j a v a2 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_binarystream) VALUES(?)"; PreparedStatement pstmt = connection.prepareStatement(sql); byte[] buffer = "some data".getBytes(); pstmt.setBytes(1, buffer); pstmt.executeUpdate(); pstmt.close(); Statement stmt = connection.createStatement(); ResultSet resultSet = stmt.executeQuery("SELECT * FROM mysql_all_table"); while (resultSet.next()) { byte[] bytes = resultSet.getBytes("col_binarystream"); } }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = getConnection(); Statement stmt = conn.createStatement(); stmt.executeUpdate("create table survey (id DECIMAL, name BINARY );"); String sql = "INSERT INTO survey (id) VALUES(?)"; PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setBigDecimal(1, new BigDecimal("1.00000")); // insert the data pstmt.executeUpdate(); ResultSet rs = stmt.executeQuery("SELECT * FROM survey"); while (rs.next()) { System.out.print(rs.getString(1)); }/*from w w w.ja v a 2 s . c o m*/ rs.close(); stmt.close(); conn.close(); }
From source file:Main.java
public static void main(String[] argv) throws Exception { Timestamp tstamp = new Timestamp(0); Class.forName("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctutorial", "root", "root"); String sql = "INSERT myTable VALUES(?,?)"; PreparedStatement prest = con.prepareStatement(sql); prest.setString(1, "x"); prest.setTimestamp(2, tstamp.valueOf("2009-02-24 12:51:42.11")); int row = prest.executeUpdate(); System.out.println(row + " row(s) affected)"); }
From source file:Main.java
public static void main(String[] argv) throws Exception { Connection con = null;/* w ww. j a v a 2 s . c o m*/ Class.forName("com.mysql.jdbc.Driver"); con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctutorial", "root", "root"); String sql = "INSERT datatypes VALUES(?,?,?)"; PreparedStatement prest = con.prepareStatement(sql); prest.setByte(1, (byte) 5); prest.setShort(2, (short) 65); prest.setLong(3, (long) 254); int row = prest.executeUpdate(); System.out.println(row + " row(s) affected)"); }
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 ww w . j av a2 s . co 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(); }