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 { String id = "0001"; float floatValue = 0001f; double doubleValue = 1.0001d; Connection conn = null; PreparedStatement pstmt = null; try {//from www. j a va 2 s. com conn = getConnection(); String query = "insert into double_table(id, float_column, double_column) values(?, ?, ?)"; pstmt = conn.prepareStatement(query); pstmt.setString(1, id); pstmt.setFloat(2, floatValue); pstmt.setDouble(3, doubleValue); // 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:DemoDisplayBlobFromDatabase.java
public static void main(String args[]) throws Exception { Connection conn = null; ResultSet rs = null;/* w w w .jav a 2s .co m*/ PreparedStatement pstmt = null; String query = "SELECT blob_column FROM blob_table WHERE id = ?"; try { conn = getConnection(); pstmt = conn.prepareStatement(query); pstmt.setString(1, "0001"); rs = pstmt.executeQuery(); rs.next(); // materialize binary data onto client java.sql.Blob blob = rs.getBlob(1); } finally { rs.close(); pstmt.close(); conn.close(); } }
From source file:Main.java
public static void main(String[] argv) throws Exception { Connection con = null; 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 args[]) throws Exception { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); String URL = "jdbc:odbc:dbName"; Connection dbConn = DriverManager.getConnection(URL, "user", "pass"); PrintWriter w = new PrintWriter(new OutputStreamWriter(System.out)); DriverManager.setLogWriter(w); dbConn.close();// ww w. j av a 2s .co m PreparedStatement prepstmt; prepstmt = dbConn.prepareStatement("SELECT id FROM employee"); prepstmt.execute(); prepstmt.close(); dbConn.close(); }
From source file:DemoPreparedStatementSetBigDecimal.java
public static void main(String[] args) throws Exception { Connection conn = null; PreparedStatement pstmt = null; String query = null;//from w ww . java2 s. c o m try { conn = getConnection(); query = "insert into BIG_DECIMAL_TABLE(id, big_decimal) values(?, ?)"; pstmt = conn.prepareStatement(query); pstmt.setString(1, "001"); pstmt.setBigDecimal(2, new java.math.BigDecimal("123456789")); // execute query, and return number of rows created int rowCount = pstmt.executeUpdate(); System.out.println("rowCount=" + rowCount); } catch (Exception e) { e.printStackTrace(); } finally { pstmt.close(); conn.close(); } }
From source file:SelectRecordsUsingPreparedStatement.java
public static void main(String[] args) { ResultSet rs = null;//w ww . j ava2 s . co m Connection conn = null; PreparedStatement pstmt = null; try { conn = getConnection(); String query = "select deptno, deptname, deptloc from dept where deptno > ?"; pstmt = conn.prepareStatement(query); // create a statement pstmt.setInt(1, 1001); // set input parameter rs = pstmt.executeQuery(); // extract data from the ResultSet while (rs.next()) { int dbDeptNumber = rs.getInt(1); String dbDeptName = rs.getString(2); String dbDeptLocation = rs.getString(3); System.out.println(dbDeptNumber + "\t" + dbDeptName + "\t" + dbDeptLocation); } } catch (Exception e) { e.printStackTrace(); } finally { try { rs.close(); pstmt.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); } } }
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 .j a v a 2 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 va2s . com*/ 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);/*ww w.j av a 2 s. co m*/ 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 ww w . ja v a 2 s.co m 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(); }