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 { try {/*ww w .jav a2 s .com*/ String url = "jdbc:odbc:yourdatabasename"; String driver = "sun.jdbc.odbc.JdbcOdbcDriver"; String user = "guest"; String password = "guest"; FileInputStream fis = new FileInputStream("sometextfile.txt"); Class.forName(driver); Connection connection = DriverManager.getConnection(url, user, password); Statement createTable = connection.createStatement(); createTable.executeUpdate("CREATE TABLE source_code (name char(20), source LONGTEXT)"); String ins = "INSERT INTO source_code VALUES(?,?)"; PreparedStatement statement = connection.prepareStatement(ins); statement.setString(1, "TryInputStream2"); statement.setAsciiStream(2, fis, fis.available()); int rowsUpdated = statement.executeUpdate(); System.out.println("Rows affected: " + rowsUpdated); Statement getCode = connection.createStatement(); ResultSet theCode = getCode.executeQuery("SELECT name,source FROM source_code"); BufferedReader reader = null; String input = null; while (theCode.next()) { reader = new BufferedReader(new InputStreamReader(theCode.getAsciiStream("source"))); while ((input = reader.readLine()) != null) { System.out.println(input); } } connection.close(); } catch (Exception e) { System.err.println(e); } }
From source file:Main.java
public static void main(String[] argv) throws Exception { Connection dbConnection = null; PreparedStatement preparedStatement = null; Class.forName(DB_DRIVER);// w w w. ja v a 2s . co m dbConnection = DriverManager.getConnection(DB_CONNECTION, DB_USER, DB_PASSWORD); String insertTableSQL = "INSERT INTO Person" + "(USER_ID, USERNAME, CREATED_BY, CREATED_DATE) VALUES" + "(?,?,?,?)"; preparedStatement = dbConnection.prepareStatement(insertTableSQL); preparedStatement.setInt(1, 11); preparedStatement.setString(2, "yourName"); preparedStatement.setString(3, "system"); java.util.Date today = new java.util.Date(); preparedStatement.setTimestamp(4, new java.sql.Timestamp(today.getTime())); preparedStatement.executeUpdate(); preparedStatement.close(); dbConnection.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = getConnection(); conn.setAutoCommit(false);//from w w w . j a va 2s.c o m Statement st = conn.createStatement(); st.executeUpdate("create table survey (id int, name VARCHAR(30) );"); String INSERT_RECORD = "insert into survey(id, name) values(?,?)"; PreparedStatement pstmt = conn.prepareStatement(INSERT_RECORD); pstmt.setString(1, "1"); pstmt.setString(2, "name1"); pstmt.addBatch(); pstmt.setString(1, "2"); pstmt.setString(2, "name2"); pstmt.addBatch(); try { // execute the batch int[] updateCounts = pstmt.executeBatch(); } catch (BatchUpdateException e) { int[] updateCounts = e.getUpdateCounts(); checkUpdateCounts(updateCounts); try { conn.rollback(); } catch (Exception e2) { e.printStackTrace(); System.exit(1); } } // since there were no errors, commit conn.commit(); ResultSet rs = st.executeQuery("SELECT * FROM survey"); outputResultSet(rs); rs.close(); st.close(); conn.close(); }
From source file:Batch.java
static public void main(String[] args) { Connection conn = null; try {// w w w .jav a 2 s.co m ArrayList breakable = new ArrayList(); PreparedStatement stmt; Iterator users; ResultSet rs; Class.forName(args[0]).newInstance(); conn = DriverManager.getConnection(args[1], args[2], args[3]); stmt = conn.prepareStatement("SELECT user_id, password " + "FROM user"); rs = stmt.executeQuery(); while (rs.next()) { String uid = rs.getString(1); String pw = rs.getString(2); // Assume PasswordCracker is some class that provides // a single static method called crack() that attempts // to run password cracking routines on the password // if( PasswordCracker.crack(uid, pw) ) { // breakable.add(uid); // } } stmt.close(); if (breakable.size() < 1) { return; } stmt = conn.prepareStatement("UPDATE user " + "SET bad_password = 'Y' " + "WHERE uid = ?"); users = breakable.iterator(); while (users.hasNext()) { String uid = (String) users.next(); stmt.setString(1, uid); stmt.addBatch(); } stmt.executeBatch(); } catch (Exception e) { e.printStackTrace(); } finally { if (conn != null) { try { conn.close(); } catch (Exception e) { } } } }
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 .ja v a 2 s.co 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 { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); String URL = "jdbc:odbc:dbname"; Connection dbConn = DriverManager.getConnection(URL, "user", "passw"); Employee employee = new Employee(42, "AA", 9); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(employee);//from w ww . j av a 2 s . co m byte[] employeeAsBytes = baos.toByteArray(); PreparedStatement pstmt = dbConn.prepareStatement("INSERT INTO EMPLOYEE (emp) VALUES(?)"); ByteArrayInputStream bais = new ByteArrayInputStream(employeeAsBytes); pstmt.setBinaryStream(1, bais, employeeAsBytes.length); pstmt.executeUpdate(); pstmt.close(); Statement stmt = dbConn.createStatement(); ResultSet rs = stmt.executeQuery("SELECT emp FROM Employee"); while (rs.next()) { byte[] st = (byte[]) rs.getObject(1); ByteArrayInputStream baip = new ByteArrayInputStream(st); ObjectInputStream ois = new ObjectInputStream(baip); Employee emp = (Employee) ois.readObject(); } stmt.close(); rs.close(); dbConn.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { byte[] data = new byte[SIZE]; for (int i = 0; i < SIZE; ++i) { data[i] = (byte) (64 + (i % 32)); }//w w w . j a v a 2s . c om ByteArrayInputStream stream = new ByteArrayInputStream(data); // DriverManager.registerDriver(new OracleDriver()); Connection c = DriverManager.getConnection("jdbc:oracle:thin:@some_database", "user", "password"); String sql = "DECLARE\n" + // " l_line CLOB;\n" + // "BEGIN\n" + // " l_line := ?;\n" + // " UPDATE table SET log = log || l_line || CHR(10) WHERE id = ?;\n" + // "END;\n"; // PreparedStatement stmt = c.prepareStatement(sql); stmt.setAsciiStream(1, stream, SIZE); stmt.setInt(2, 1); stmt.execute(); stmt.close(); c.commit(); c.close(); }
From source file:Main.java
public static void main(String[] argv) throws Exception { Connection dbConnection = null; PreparedStatement preparedStatement = null; Class.forName(DB_DRIVER);/* www .ja v a 2 s . c om*/ dbConnection = DriverManager.getConnection(DB_CONNECTION, DB_USER, DB_PASSWORD); String insertTableSQL = "INSERT INTO Person" + "(USER_ID, USERNAME, CREATED_BY, CREATED_DATE) VALUES" + "(?,?,?,?)"; preparedStatement = dbConnection.prepareStatement(insertTableSQL); dbConnection.setAutoCommit(false); java.util.Date today = new java.util.Date(); preparedStatement.setInt(1, 101); preparedStatement.setString(2, "101"); preparedStatement.setString(3, "system"); preparedStatement.setTimestamp(4, new java.sql.Timestamp(today.getTime())); preparedStatement.addBatch(); preparedStatement.setInt(1, 102); preparedStatement.setString(2, "102"); preparedStatement.setString(3, "system"); preparedStatement.setTimestamp(4, new java.sql.Timestamp(today.getTime())); preparedStatement.addBatch(); preparedStatement.setInt(1, 103); preparedStatement.setString(2, "103"); preparedStatement.setString(3, "system"); preparedStatement.setTimestamp(4, new java.sql.Timestamp(today.getTime())); preparedStatement.addBatch(); preparedStatement.executeBatch(); dbConnection.commit(); preparedStatement.close(); dbConnection.close(); }
From source file:XMLDBDOM.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 . ja va 2s. c o m*/ Statement s = conn.createStatement(); s.executeUpdate("CREATE TABLE XMLData(GAMEID INT, MANUAL SERIALIZE(org.w3c.dom.Document))"); conn.commit(); File file = new File("XMLData.xml"); InputStream is = new FileInputStream(file); PreparedStatement ps = conn.prepareStatement("INSERT INTO XMLData VALUES(?,?)"); ps.setInt(1, 1285757); DOMParser parser = new DOMParser(); parser.parse("XMLData.xml"); Document manual = parser.getDocument(); ps.setObject(2, manual); ps.execute(); conn.commit(); }
From source file:MainClass.java
public static void main(String[] args) { Connection connection = null; PreparedStatement statement = null; try {/* w ww.j a v a 2s . co m*/ Class.forName("com.mysql.jdbc.Driver").newInstance(); String url = "jdbc:mysql://localhost/database"; connection = DriverManager.getConnection(url, "username", "password"); String sql = "UPDATE employees SET email = ? WHERE employee_id = ?"; statement = connection.prepareStatement(sql); statement.setString(1, "a@a.com"); statement.setLong(2, 1); statement.addBatch(); statement.setString(1, "b@b.com"); statement.setLong(2, 2); statement.addBatch(); statement.setString(1, "c@c.com"); statement.setLong(2, 3); statement.addBatch(); statement.executeBatch(); } catch (Exception e) { e.printStackTrace(); } finally { if (statement != null) { try { statement.close(); } catch (SQLException e) { } // nothing we can do } if (connection != null) { try { connection.close(); } catch (SQLException e) { } // nothing we can do } } }