List of usage examples for java.sql Statement executeUpdate
int executeUpdate(String sql) throws SQLException;
INSERT
, UPDATE
, or DELETE
statement or an SQL statement that returns nothing, such as an SQL DDL statement. From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = getConnection(); conn.setAutoCommit(false);// ww w . java2s . c o m Statement st = conn.createStatement(); st.executeUpdate("create table survey (id int,myURL CHAR);"); st.executeUpdate("insert into survey(id) values(01)"); st.executeUpdate("insert into survey(id) values(02)"); Savepoint mySavepoint = conn.setSavepoint("MYSAVEPOINT"); st.executeUpdate("insert into survey(id) values(03)"); conn.commit(); conn.rollback(mySavepoint); st.close(); conn.close(); }
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 int, name BINARY );"); String sql = "INSERT INTO survey (name) VALUES(?)"; PreparedStatement pstmt = conn.prepareStatement(sql); String myData = "some string data ..."; byte[] binaryData = myData.getBytes(); pstmt.setBytes(1, binaryData);//from ww w .j av a2 s . c om pstmt.executeUpdate(); ResultSet rs = stmt.executeQuery("SELECT * FROM survey"); while (rs.next()) { System.out.print(rs.getBytes("name").length + " "); } rs.close(); stmt.close(); conn.close(); }
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 int, name BINARY );"); String sql = "INSERT INTO survey (name) VALUES(?)"; PreparedStatement pstmt = conn.prepareStatement(sql); String myData = "some string data ..."; byte[] binaryData = myData.getBytes(); pstmt.setBytes(1, binaryData);/*from w ww . j a v a2s . c o m*/ pstmt.executeUpdate(); ResultSet rs = stmt.executeQuery("SELECT * FROM survey"); while (rs.next()) { System.out.println(rs.getByte("id")); } rs.close(); stmt.close(); conn.close(); }
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 int, name BINARY );"); String sql = "INSERT INTO survey (name) VALUES(?)"; PreparedStatement pstmt = conn.prepareStatement(sql); String myData = "some string data ..."; byte[] binaryData = myData.getBytes(); pstmt.setBytes(1, binaryData);/*w w w . j a va 2 s. c om*/ pstmt.executeUpdate(); ResultSet rs = stmt.executeQuery("SELECT * FROM survey"); while (rs.next()) { System.out.print(rs.getByte(1)); } rs.close(); stmt.close(); conn.close(); }
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 int, name BINARY );"); String sql = "INSERT INTO survey (name) VALUES(?)"; PreparedStatement pstmt = conn.prepareStatement(sql); String myData = "some string data ..."; byte[] binaryData = myData.getBytes(); pstmt.setBytes(1, binaryData);/*from w w w . ja v a 2 s . c om*/ pstmt.executeUpdate(); ResultSet rs = stmt.executeQuery("SELECT * FROM survey"); while (rs.next()) { System.out.print(rs.getBytes(2).length + " "); } rs.close(); stmt.close(); conn.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = getHSQLConnection(); conn.setAutoCommit(true);/*from w ww . j a v a 2 s . c om*/ Statement st = conn.createStatement(); st.executeUpdate("create table survey (id int,name varchar(30));"); st.executeUpdate("insert into survey (id,name ) values (1,'nameValue')"); st = conn.createStatement(); ResultSet rs = st.executeQuery("SELECT * FROM survey"); ResultSetMetaData rsMetaData = rs.getMetaData(); int numberOfColumns = rsMetaData.getColumnCount(); System.out.println("resultSet MetaData column Count=" + numberOfColumns); rs.close(); st.close(); conn.close(); }
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 a va 2 s . c o 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: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);/*from ww w . j a v a 2 s .c o 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 { String url = "jdbc:mysql://localhost:3306/"; String dbName = "jdbc4"; String driver = "com.mysql.jdbc.Driver"; String userName = "root"; String password = "root"; Class.forName(driver).newInstance(); Connection conn = DriverManager.getConnection(url + dbName, userName, password); Statement st = conn.createStatement(); int rows = st.executeUpdate("INSERT INTO Copyemployee SELECT * FROM employee"); if (rows == 0) { System.out.println("Don't add any row!"); } else {/*from www.j a v a2 s . c o m*/ System.out.println(rows + " row(s)affected."); 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,name varchar(30));"); st.executeUpdate("insert into survey (id,name ) values (10,'nameValue')"); st.executeUpdate("insert into survey (id,name ) values (null,null)"); st = conn.createStatement();/*from w ww. j a va2 s. co m*/ ResultSet rs = st.executeQuery("SELECT * FROM survey"); while (rs.next()) { int id = rs.getInt(1); System.out.println(id); if (rs.wasNull()) { id = -1; } System.out.println(id); } rs.close(); st.close(); conn.close(); }