List of usage examples for java.sql PreparedStatement close
void close() throws SQLException;
Statement
object's database and JDBC resources immediately instead of waiting for this to happen when it is automatically closed. From source file:DeleteRecordsUsingPreparedStatement.java
public static void main(String[] args) throws Exception { Connection conn = null;/*from w w w . ja v a 2 s .c o m*/ PreparedStatement pstmt = null; try { conn = getConnection(); String query = "delete from tableName"; pstmt = conn.prepareStatement(query); pstmt.executeUpdate(); } catch (Exception e) { e.printStackTrace(); } finally { pstmt.close(); 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();/*from ww w .j a v a 2s .c o m*/ ResultSet rs = s.getResultSet(); if (rs != null) { while (rs.next()) { System.out.println(rs.getInt("All")); } } s.close(); conn.close(); }
From source file:Main.java
public static void main(String[] argv) throws Exception { Connection dbConnection = null; PreparedStatement preparedStatement = null; Class.forName(DB_DRIVER);// ww w .j a va 2 s.c o m dbConnection = DriverManager.getConnection(DB_CONNECTION, DB_USER, DB_PASSWORD); String deleteSQL = "DELETE Person WHERE USER_ID = ?"; preparedStatement = dbConnection.prepareStatement(deleteSQL); preparedStatement.setInt(1, 1001); preparedStatement.executeUpdate(); preparedStatement.close(); dbConnection.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection dbConnection = null; String myConnectionString = ""; myConnectionString = "jdbc:mysql://192.168.1.3:3306/mytestdb"; dbConnection = DriverManager.getConnection(myConnectionString, "root", "whatever"); PreparedStatement stmt = dbConnection.prepareStatement("SELECT * FROM jdbctest"); ResultSet rs = stmt.executeQuery(); int i = 0;//from ww w. ja v a 2s .c om int j = 0; String s = ""; while (rs.next()) { i++; j = rs.getInt("id"); s = rs.getString("textcol"); } System.out.println(String.format("Finished reading %d rows.", i)); rs.close(); stmt.close(); dbConnection.close(); }
From source file:Main.java
public static void main(String[] argv) throws Exception { int count = 0; Class.forName("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctutorial", "root", "root"); String sql = "SELECT title,year_made FROM product WHERE year_made >= ? AND year_made <= ?"; PreparedStatement prest = con.prepareStatement(sql); prest.setInt(1, 2000);/*from w w w . j a va 2 s.c o m*/ prest.setInt(2, 2009); ResultSet rs = prest.executeQuery(); while (rs.next()) { String mov_name = rs.getString(1); int mov_year = rs.getInt(2); count++; System.out.println(mov_name + "\t" + "- " + mov_year); } System.out.println("Number of records: " + count); prest.close(); con.close(); }
From source file:Main.java
public static final void main(String[] argv) throws Exception { Class.forName("oracle.jdbc.OracleDriver"); Connection conn = DriverManager.getConnection("your_connection_string", "your_user_name", "your_password"); Date nowDate = new Date(); Timestamp nowTimestamp = new Timestamp(nowDate.getTime()); PreparedStatement insertStmt = conn.prepareStatement( "INSERT INTO MyTable" + " (os_name, ts, ts_with_tz, ts_with_local_tz)" + " VALUES (?, ?, ?, ?)"); insertStmt.setString(1, System.getProperty("os.name")); insertStmt.setTimestamp(2, nowTimestamp); insertStmt.setTimestamp(3, nowTimestamp); insertStmt.setTimestamp(4, nowTimestamp); insertStmt.executeUpdate();// w w w .ja va2 s. co m insertStmt.close(); System.out.println("os_name, ts, ts_with_tz, ts_with_local_tz"); PreparedStatement selectStmt = conn .prepareStatement("SELECT os_name, ts, ts_with_tz, ts_with_local_tz" + " FROM MyTable"); ResultSet result = null; result = selectStmt.executeQuery(); while (result.next()) { System.out.println(String.format("%s,%s,%s,%s", result.getString(1), result.getTimestamp(2).toString(), result.getTimestamp(3).toString(), result.getTimestamp(4).toString())); } result.close(); selectStmt.close(); conn.close(); }
From source file:DemoPreparedStatementSetBoolean.java
public static void main(String[] args) throws Exception { boolean booleanValue = true; Connection conn = null;//from w w w. j a v a2s . c o m PreparedStatement pstmt = null; try { conn = getConnection(); String query = "insert into boolean_table(id, boolean_column) values(?, ?)"; pstmt = conn.prepareStatement(query); pstmt.setString(1, "0001"); pstmt.setBoolean(2, booleanValue); int rowCount = pstmt.executeUpdate(); System.out.println("rowCount=" + rowCount); } finally { pstmt.close(); conn.close(); } }
From source file:EmployeeInit.java
public static void main(String[] args) throws Exception { Connection con;//from www . j a v a 2 s.c om con = DriverManager.getConnection("jdbc:derby://localhost:1527/" + "c:\\db\\employee"); PreparedStatement ps; ps = con.prepareStatement("insert into employee(name,photo) " + "values(?,?)"); ps.setString(1, "Duke"); Blob blob = con.createBlob(); ImageIcon ii = new ImageIcon("duke.png"); ObjectOutputStream oos; oos = new ObjectOutputStream(blob.setBinaryStream(1)); oos.writeObject(ii); oos.close(); ps.setBlob(2, blob); ps.execute(); blob.free(); ps.close(); }
From source file:DemoPreparedStatementSetString.java
public static void main(String[] args) throws Exception { String stringValue = "stringValueToBeInserted"; Connection conn = null;// w w w. j a va2s. c o m PreparedStatement pstmt = null; try { conn = getConnection(); String query = "insert into string_table(string_column) values(?)"; pstmt = conn.prepareStatement(query); pstmt.setString(1, stringValue); // execute query, and return number of rows created int rowCount = pstmt.executeUpdate(); System.out.println("rowCount=" + rowCount); } finally { pstmt.close(); conn.close(); } }
From source file:InsertPictureToMySql.java
public static void main(String[] args) throws Exception, IOException, SQLException { Class.forName("org.gjt.mm.mysql.Driver"); Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/databaseName", "root", "root"); String INSERT_PICTURE = "insert into MyPictures(id, name, photo) values (?, ?, ?)"; FileInputStream fis = null;//from www. j a v a 2 s . co m PreparedStatement ps = null; try { conn.setAutoCommit(false); File file = new File("myPhoto.png"); fis = new FileInputStream(file); ps = conn.prepareStatement(INSERT_PICTURE); ps.setString(1, "001"); ps.setString(2, "name"); ps.setBinaryStream(3, fis, (int) file.length()); ps.executeUpdate(); conn.commit(); } finally { ps.close(); fis.close(); } }