List of usage examples for java.sql Connection prepareStatement
PreparedStatement prepareStatement(String sql) throws SQLException;
PreparedStatement
object for sending parameterized SQL statements to the database. 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, register int );"); String sql = "INSERT INTO survey (register) VALUES(?)"; PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setBoolean(1, true);/*from www . jav a 2 s . co m*/ pstmt.executeUpdate(); pstmt.setBoolean(1, false); pstmt.executeUpdate(); ResultSet rs = stmt.executeQuery("SELECT * FROM survey"); while (rs.next()) { System.out.println(rs.getString(2)); } 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); // create some binary data String myData = "some string data ..."; byte[] binaryData = myData.getBytes(); // set value for the prepared statement pstmt.setBytes(1, binaryData);/*from w w w .j av a 2 s .co m*/ // insert the data 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 = 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); // create some binary data String myData = "some string data ..."; byte[] binaryData = myData.getBytes(); // set value for the prepared statement pstmt.setBytes(1, binaryData);//from w w w. j av a 2s . co m // insert the data pstmt.executeUpdate(); ResultSet rs = stmt.executeQuery("SELECT * FROM survey"); while (rs.next()) { System.out.print(new String(rs.getBytes(2))); } 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); // prepare small binary stream File smallFile = new File("yourFileName.txt"); int smallFileLength = (int) smallFile.length(); InputStream smallStream = (InputStream) new FileInputStream(smallFile); pstmt.setBinaryStream(2, smallStream, smallFileLength); // insert the data pstmt.executeUpdate();// w w w.ja v a2 s. c o m ResultSet rs = stmt.executeQuery("SELECT * FROM survey"); while (rs.next()) { System.out.print(rs.getString(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); // prepare text stream File file = new File("yourFileName.txt"); int fileLength = (int) file.length(); InputStream stream = (InputStream) new FileInputStream(file); pstmt.setString(1, "001"); pstmt.setAsciiStream(2, stream, fileLength); // insert the data pstmt.executeUpdate();// w w w. j a v a2 s. c o m ResultSet rs = stmt.executeQuery("SELECT * FROM survey"); while (rs.next()) { System.out.print(new String(rs.getBytes(2))); } rs.close(); stmt.close(); 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));"); String sql = "INSERT INTO survey (id) VALUES(?)"; PreparedStatement pstmt = conn.prepareStatement(sql); ParameterMetaData pmd = pstmt.getParameterMetaData(); int totalDigits = pmd.getPrecision(1); int digitsAfterDecimal = pmd.getScale(1); boolean b = pmd.isSigned(1); System.out.println("The first parameter "); System.out.println(" has precision " + totalDigits); System.out.println(" has scale " + digitsAfterDecimal); System.out.println(" may be a signed number " + b); int count = pmd.getParameterCount(); System.out.println("count is " + count); for (int i = 1; i <= count; i++) { int type = pmd.getParameterType(i); String typeName = pmd.getParameterTypeName(i); System.out.println("Parameter " + i + ":"); System.out.println(" type is " + type); System.out.println(" type name is " + typeName); }/* w w w. jav a 2s. co m*/ st.close(); 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,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. ja v a 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[] argv) throws Exception { Connection dbConnection = null; PreparedStatement preparedStatement = null; Class.forName(DB_DRIVER);// w ww . ja v a 2 s . co 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 conn = getConnection(); Statement st = conn.createStatement(); st.executeUpdate("create table survey (id int,myURL CHAR);"); String INSERT_RECORD = "insert into survey(id, myURL) values(?, ?)"; PreparedStatement pstmt = conn.prepareStatement(INSERT_RECORD); pstmt.setString(1, "1"); pstmt.setURL(2, new URL("http://www.java2s.com")); pstmt.executeUpdate();//from ww w. j a v a 2 s. c o m ResultSet rs = st.executeQuery("SELECT * FROM survey"); outputResultSet(rs); rs.close(); st.close(); conn.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { String id = "0001"; Connection conn = null; PreparedStatement pstmt = null; try {/*from w ww . jav a2 s .co m*/ conn = getConnection(); String query = "insert into nullable_table(id,string_column, int_column) values(?, ?, ?)"; // create PrepareStatement object pstmt = conn.prepareStatement(query); pstmt.setString(1, id); pstmt.setNull(2, java.sql.Types.VARCHAR); pstmt.setNull(3, java.sql.Types.INTEGER); // execute query, and return number of rows created int rowCount = pstmt.executeUpdate(); System.out.println("rowCount=" + rowCount); } finally { pstmt.close(); conn.close(); } }