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: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();//from w w w .ja v a 2 s. c om PreparedStatement prepstmt; prepstmt = dbConn.prepareStatement("SELECT id FROM employee"); prepstmt.execute(); prepstmt.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)); }/*from ww w .j a v a 2 s. c o m*/ 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 con = null;/*from w w w .j a va 2 s.co m*/ PreparedStatement prepstmt; prepstmt = con.prepareStatement("DELETE FROM tCust " + " WHERE custId = ?"); prepstmt.setString(1, "1"); prepstmt.executeUpdate(); prepstmt.close(); con.close(); }
From source file:Main.java
public static void main(String args[]) throws Exception { Connection con = null;// w ww . ja v a 2 s .co m Class.forName("oracle.jdbc.driver.OracleDriver"); con = DriverManager.getConnection("jdbc:oracle:thin:@192.201.32.92:1521:psprd1", "username", "password"); String query = null; ResultSet rset = null; query = "UPDATE t1 " + " SET id = ?"; PreparedStatement stmt = con.prepareStatement(query); // stmt.setInt(paramIndex++, null); stmt.setNull(1, java.sql.Types.INTEGER); stmt.executeUpdate(); stmt.close(); query = "select id from t1 "; stmt = con.prepareStatement(query); rset = stmt.executeQuery(); rset.next(); System.out.println(rset.getString("id")); rset.close(); stmt.close(); con.close(); }
From source file:Main.java
public static void main(String[] argv) throws Exception { String driverName = "com.jnetdirect.jsql.JSQLDriver"; Class.forName(driverName);// w ww . j a v a2 s . c o m String serverName = "127.0.0.1"; String portNumber = "1433"; String mydatabase = serverName + ":" + portNumber; String url = "jdbc:JSQLConnect://" + mydatabase; String username = "username"; String password = "password"; Connection connection = DriverManager.getConnection(url, username, password); String sql = "INSERT INTO mysql_all_table (col_binarystream) VALUES(?)"; PreparedStatement pstmt = connection.prepareStatement(sql); byte[] buffer = "some data".getBytes(); pstmt.setBytes(1, buffer); pstmt.executeUpdate(); pstmt.close(); Statement stmt = connection.createStatement(); ResultSet resultSet = stmt.executeQuery("SELECT * FROM mysql_all_table"); while (resultSet.next()) { byte[] bytes = resultSet.getBytes("col_binarystream"); } }
From source file:Main.java
public static void main(String[] argv) throws Exception { File file = new File("myimage.gif"); FileInputStream fis = new FileInputStream(file); Class.forName("oracle.jdbc.driver.OracleDriver"); Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@//server.local:1521/prod", "scott", "tiger"); conn.setAutoCommit(false);/*from w w w . j a v a 2 s . c o m*/ PreparedStatement ps = conn.prepareStatement("insert into images values (?,?)"); ps.setString(1, file.getName()); ps.setBinaryStream(2, fis, (int) file.length()); ps.executeUpdate(); ps.close(); fis.close(); }
From source file:Main.java
public static void main(String[] argv) throws Exception { Connection con = null;/*from w ww . ja va2s . co m*/ PreparedStatement prepstmt; prepstmt = con.prepareStatement("UPDATE employee SET Name = ? " + " WHERE Id = ?"); prepstmt.setString(1, "Smith"); prepstmt.setString(2, "1"); prepstmt.executeUpdate(); prepstmt.close(); con.close(); }
From source file:Main.java
public static void main(String[] argv) throws Exception { String url = "jdbc:mysql://localhost:3306/"; String dbName = "javatutorial"; String userName = "root"; String password = "root"; Class.forName("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnection(url + dbName, userName, password); File imgfile = new File("images.jpg"); FileInputStream fin = new FileInputStream(imgfile); PreparedStatement pre = con.prepareStatement("insert into Image values(?,?,?)"); pre.setInt(1, 5);/*from w w w . java2s. c o m*/ pre.setString(2, "A"); pre.setBinaryStream(3, fin, (int) imgfile.length()); pre.executeUpdate(); pre.close(); con.close(); }
From source file:Main.java
public static void main(String[] argv) throws Exception { Connection con = null;/*from www . j a va 2 s . com*/ PreparedStatement prepstmt = con.prepareStatement("select Name, Addr from Employee where Id = ?"); prepstmt.setString(1, "1"); ResultSet rs; rs = prepstmt.executeQuery(); boolean found = rs.next(); if (found) System.out.println(rs.getString(1)); prepstmt.close(); }
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 a v 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(); }