List of usage examples for java.sql Connection createStatement
Statement createStatement() throws SQLException;
Statement
object for sending SQL statements to the database. From source file:mx.com.pixup.portal.demo.DemoDisqueraUpdate.java
public static void main(String[] args) { System.out.println("BIENVENIDO A PIXUP"); System.out.println("Mantenimiento catlogo disquera"); System.out.println("Actualizacin de Disquera"); InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr); Connection connection = null; Statement statement = null;/* ww w. j a v a 2 s . c o m*/ ResultSet resultSet = null; try { BasicDataSource dataSource = new BasicDataSource(); dataSource.setDriverClassName("com.mysql.jdbc.Driver"); dataSource.setUsername("root"); dataSource.setPassword("admin"); dataSource.setUrl("jdbc:mysql://127.0.0.1:3306/pixup"); connection = dataSource.getConnection(); statement = connection.createStatement(); String sql = "select id, nombre from disquera order by nombre"; resultSet = statement.executeQuery(sql); System.out.println("Id Disquera: \t Nombre Disquera"); while (resultSet.next()) { System.out.println(resultSet.getInt("id") + " \t " + resultSet.getString("nombre")); } System.out.println("Proporcione el id de la disquera a actualizar: "); String idDisquera = br.readLine(); System.out.println("Proporcione el nuevo nombre de la disquera: "); String nombreDisquera = br.readLine(); sql = "update disquera set nombre = '" + nombreDisquera + "' where id = " + idDisquera; statement.execute(sql); System.out.println("Disqueras Actualizadas:"); sql = "select id, nombre from disquera order by nombre desc"; resultSet = statement.executeQuery(sql); System.out.println("Id Disquera: \t Nombre Disquera"); while (resultSet.next()) { System.out.println(resultSet.getInt("id") + " \t " + resultSet.getString("nombre")); } } catch (Exception e) { System.out.println("Error en el sistema, intente ms tarde!!"); } finally { if (resultSet != null) { try { resultSet.close(); } catch (Exception e) { } } if (statement != null) { try { statement.close(); } catch (Exception e) { } } if (connection != null) { try { connection.close(); } catch (Exception e) { } } } }
From source file:com.jt.dbcp.example.ManualPoolingDriverExample.java
public static void main(String[] args) { ////from w w w . jav a 2 s .com // First we load the underlying JDBC driver. // You need this if you don't use the jdbc.drivers // system property. // System.out.println("Loading underlying JDBC driver."); try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { e.printStackTrace(); } System.out.println("Done."); // // Then we set up and register the PoolingDriver. // Normally this would be handled auto-magically by // an external configuration, but in this example we'll // do it manually. // System.out.println("Setting up driver."); try { setupDriver(args[0]); } catch (Exception e) { e.printStackTrace(); } System.out.println("Done."); // // Now, we can use JDBC as we normally would. // Using the connect string // jdbc:apache:commons:dbcp:example // The general form being: // jdbc:apache:commons:dbcp:<name-of-pool> // Connection conn = null; Statement stmt = null; ResultSet rset = null; try { System.out.println("Creating connection."); //pool conn = DriverManager.getConnection("jdbc:apache:commons:dbcp:example"); System.out.println("Creating statement."); stmt = conn.createStatement(); System.out.println("Executing statement."); rset = stmt.executeQuery(args[1]); System.out.println("Results:"); int numcols = rset.getMetaData().getColumnCount(); int count = 0; while (rset.next()) { count++; if (count == 10) { break; } for (int i = 1; i <= numcols; i++) { System.out.print("\t" + rset.getString(i)); } System.out.println(""); } } catch (SQLException e) { e.printStackTrace(); } finally { try { if (rset != null) rset.close(); } catch (Exception e) { } try { if (stmt != null) stmt.close(); } catch (Exception e) { } try { if (conn != null) conn.close(); } catch (Exception e) { } } // Display some pool statistics try { printDriverStats(); } catch (Exception e) { e.printStackTrace(); } // closes the pool try { shutdownDriver(); } catch (Exception e) { e.printStackTrace(); } }
From source file:InsertStores.java
public static void main(String args[]) { String url = "jdbc:mySubprotocol:myDataSource"; Connection con; Statement stmt;/*from www. j av a2 s. c o m*/ try { Class.forName("myDriver.ClassName"); } catch (java.lang.ClassNotFoundException e) { System.err.print("ClassNotFoundException: "); System.err.println(e.getMessage()); } try { con = DriverManager.getConnection(url, "myLogin", "myPassword"); stmt = con.createStatement(); con.setAutoCommit(false); String insertStore1 = "INSERT INTO STORES VALUES (" + "100001, " + "ADDRESS(888, 'Main_Street', 'Rancho_Alegre', " + "'CA', '94049'), " + "COF_ARRAY('Colombian', 'French_Roast', 'Espresso', " + "'Colombian_Decaf', 'French_Roast_Decaf'), " + "(SELECT OID FROM MANAGERS WHERE MGR_ID = 000001))"; stmt.addBatch(insertStore1); String insertStore2 = "INSERT INTO STORES VALUES (" + "100002, " + "ADDRESS(1560, 'Alder', 'Ochos_Pinos', " + "'CA', '94049'), " + "COF_ARRAY('Colombian', 'French_Roast', 'Espresso', " + "'Colombian_Decaf', 'French_Roast_Decaf', " + "'Kona', 'Kona_Decaf'), " + "(SELECT OID FROM MANAGERS WHERE MGR_ID = 000001))"; stmt.addBatch(insertStore2); String insertStore3 = "INSERT INTO STORES VALUES (" + "100003, " + "ADDRESS(4344, 'First_Street', 'Verona', " + "'CA', '94545'), " + "COF_ARRAY('Colombian', 'French_Roast', 'Espresso', " + "'Colombian_Decaf', 'French_Roast_Decaf', " + "'Kona', 'Kona_Decaf'), " + "(SELECT OID FROM MANAGERS WHERE MGR_ID = 000002))"; stmt.addBatch(insertStore3); String insertStore4 = "INSERT INTO STORES VALUES (" + "100004, " + "ADDRESS(321, 'Sandy_Way', 'La_Playa', " + "'CA', '94544'), " + "COF_ARRAY('Colombian', 'French_Roast', 'Espresso', " + "'Colombian_Decaf', 'French_Roast_Decaf', " + "'Kona', 'Kona_Decaf'), " + "(SELECT OID FROM MANAGERS WHERE MGR_ID = 000002))"; stmt.addBatch(insertStore4); String insertStore5 = "INSERT INTO STORES VALUES (" + "100005, " + "ADDRESS(1000, 'Clover_Road', 'Happyville', " + "'CA', '90566'), " + "COF_ARRAY('Colombian', 'French_Roast', 'Espresso', " + "'Colombian_Decaf', 'French_Roast_Decaf'), " + "(SELECT OID FROM MANAGERS WHERE MGR_ID = 000003))"; stmt.addBatch(insertStore5); int[] updateCounts = stmt.executeBatch(); ResultSet rs = stmt.executeQuery("SELECT * FROM STORES"); System.out.println("Table STORES after insertion:"); System.out.println("STORE_NO LOCATION COF_TYPE MGR"); while (rs.next()) { int storeNo = rs.getInt("STORE_NO"); Struct location = (Struct) rs.getObject("LOCATION"); Object[] locAttrs = location.getAttributes(); Array coffeeTypes = rs.getArray("COF_TYPE"); String[] cofTypes = (String[]) coffeeTypes.getArray(); Ref managerRef = rs.getRef("MGR"); PreparedStatement pstmt = con.prepareStatement("SELECT MANAGER FROM MANAGERS WHERE OID = ?"); pstmt.setRef(1, managerRef); ResultSet rs2 = pstmt.executeQuery(); rs2.next(); Struct manager = (Struct) rs2.getObject("MANAGER"); Object[] manAttrs = manager.getAttributes(); System.out.print(storeNo + " "); System.out.print(locAttrs[0] + " " + locAttrs[1] + " " + locAttrs[2] + ", " + locAttrs[3] + " " + locAttrs[4] + " "); for (int i = 0; i < cofTypes.length; i++) System.out.print(cofTypes[i] + " "); System.out.println(manAttrs[1] + ", " + manAttrs[2]); rs2.close(); pstmt.close(); } rs.close(); stmt.close(); con.close(); } catch (BatchUpdateException b) { System.err.println("-----BatchUpdateException-----"); System.err.println("SQLState: " + b.getSQLState()); System.err.println("Message: " + b.getMessage()); System.err.println("Vendor: " + b.getErrorCode()); System.err.print("Update counts: "); int[] updateCounts = b.getUpdateCounts(); for (int i = 0; i < updateCounts.length; i++) { System.err.print(updateCounts[i] + " "); } System.err.println(""); } catch (SQLException ex) { System.err.println("SQLException: " + ex.getMessage()); System.err.println("SQLState: " + ex.getSQLState()); System.err.println("Message: " + ex.getMessage()); System.err.println("Vendor: " + ex.getErrorCode()); } }
From source file:CreateCoffees.java
public static void main(String args[]) { String url = "jdbc:mySubprotocol:myDataSource"; Connection con; String createString;//from w w w . j a va 2 s . c o m createString = "create table COFFEES " + "(COF_NAME VARCHAR(32), " + "SUP_ID INTEGER, " + "PRICE FLOAT, " + "SALES INTEGER, " + "TOTAL INTEGER)"; Statement stmt; try { Class.forName("myDriver.ClassName"); } catch (java.lang.ClassNotFoundException e) { System.err.print("ClassNotFoundException: "); System.err.println(e.getMessage()); } try { con = DriverManager.getConnection(url, "myLogin", "myPassword"); stmt = con.createStatement(); stmt.executeUpdate(createString); stmt.close(); con.close(); } catch (SQLException ex) { System.err.println("SQLException: " + ex.getMessage()); } }
From source file:CreateCoffees.java
public static void main(String args[]) { String url = "jdbc:mySubprotocol:myDataSource"; Connection con; String createString;/*from w w w .j a v a2 s .c om*/ createString = "create table COFFEES " + "(COF_NAME varchar(32), " + "SUP_ID int, " + "PRICE float, " + "SALES int, " + "TOTAL int)"; Statement stmt; try { Class.forName("myDriver.ClassName"); } catch (java.lang.ClassNotFoundException e) { System.err.print("ClassNotFoundException: "); System.err.println(e.getMessage()); } try { con = DriverManager.getConnection(url, "myLogin", "myPassword"); stmt = con.createStatement(); stmt.executeUpdate(createString); stmt.close(); con.close(); } catch (SQLException ex) { System.err.println("SQLException: " + ex.getMessage()); } }
From source file:Join.java
public static void main(String args[]) { String url = "jdbc:mySubprotocol:myDataSource"; Connection con; String query = "select SUPPLIERS.SUP_NAME, COFFEES.COF_NAME " + "from COFFEES, SUPPLIERS " + "where SUPPLIERS.SUP_NAME like 'Acme, Inc.' and " + "SUPPLIERS.SUP_ID = COFFEES.SUP_ID"; Statement stmt;//from ww w . j a v a 2 s. com try { Class.forName("myDriver.ClassName"); } catch (java.lang.ClassNotFoundException e) { System.err.print("ClassNotFoundException: "); System.err.println(e.getMessage()); } try { con = DriverManager.getConnection(url, "myLogin", "myPassword"); stmt = con.createStatement(); ResultSet rs = stmt.executeQuery(query); System.out.println("Supplier, Coffee:"); while (rs.next()) { String supName = rs.getString(1); String cofName = rs.getString(2); System.out.println(" " + supName + ", " + cofName); } stmt.close(); con.close(); } catch (SQLException ex) { System.err.print("SQLException: "); System.err.println(ex.getMessage()); } }
From source file:CreateSuppliers.java
public static void main(String args[]) { String url = "jdbc:mySubprotocol:myDataSource"; Connection con; String createString;/*from w w w . ja v a 2 s . c o m*/ createString = "create table SUPPLIERS " + "(SUP_ID int, " + "SUP_NAME varchar(40), " + "STREET varchar(40), " + "CITY varchar(20), " + "STATE char(2), ZIP char(5))"; Statement stmt; try { Class.forName("myDriver.ClassName"); } catch (java.lang.ClassNotFoundException e) { System.err.print("ClassNotFoundException: "); System.err.println(e.getMessage()); } try { con = DriverManager.getConnection(url, "myLogin", "myPassword"); stmt = con.createStatement(); stmt.executeUpdate(createString); stmt.close(); con.close(); } catch (SQLException ex) { System.err.println("SQLException: " + ex.getMessage()); } }
From source file:SQLStatement.java
public static void main(String args[]) { String url = "jdbc:mySubprotocol:myDataSource"; Connection con; String query = "select SUPPLIERS.SUP_NAME, COFFEES.COF_NAME " + "from COFFEES, SUPPLIERS " + "where SUPPLIERS.SUP_NAME like 'Acme, Inc.' and " + "SUPPLIERS.SUP_ID = COFFEES.SUP_ID"; Statement stmt;//from w w w .ja va2 s . co m try { Class.forName("myDriver.ClassName"); } catch (java.lang.ClassNotFoundException e) { System.err.print("ClassNotFoundException: "); System.err.println(e.getMessage()); } try { con = DriverManager.getConnection(url, "myLogin", "myPassword"); stmt = con.createStatement(); ResultSet rs = stmt.executeQuery(query); ResultSetMetaData rsmd = rs.getMetaData(); int numberOfColumns = rsmd.getColumnCount(); int rowCount = 1; while (rs.next()) { System.out.println("Row " + rowCount + ": "); for (int i = 1; i <= numberOfColumns; i++) { System.out.print(" Column " + i + ": "); System.out.println(rs.getString(i)); } System.out.println(""); rowCount++; } stmt.close(); con.close(); } catch (SQLException ex) { System.err.print("SQLException: "); System.err.println(ex.getMessage()); } }
From source file:fr.eo.util.dumper.Dumper.java
/** * @param args/*from ww w. j av a 2 s. co m*/ */ public static void main(String[] args) { String appName = args[0]; System.out.println("Starting dumper ..."); Statement stmt = null; try { System.out.println("Getting database connection ..."); Connection conn = getJtdsConnection(); List<RequestDefinitionBean> requests = RequestDefinitionParser.getRequests(appName); assetFolder = RequestDefinitionParser.getAppBaseDir(appName) + "assets/"; for (RequestDefinitionBean request : requests) { int currentFileSize = 0, cpt = 1; stmt = conn.createStatement(); System.out.println("Dumping " + request.name + "..."); ResultSet rs = stmt.executeQuery(request.sql); BufferedWriter bw = getWriter(request.name, cpt); bw.append(COPYRIGHTS); currentFileSize += COPYRIGHTS.length(); bw.append("--" + request.name + "\n"); while (rs.next()) { StringBuilder sb = new StringBuilder(); sb.append("INSERT INTO "); sb.append(request.table).append(" VALUES ("); int pos = 0; for (String fieldName : request.fields) { String str = getFieldValue(request, pos, rs, fieldName); sb.append(str); pos++; if (pos < request.fields.size()) { sb.append(","); } } sb.append(");\n"); currentFileSize += sb.length(); bw.append(sb.toString()); bw.flush(); if (currentFileSize > MAX_FILE_SIZE) { bw.close(); bw = getWriter(request.name, ++cpt); bw.append(COPYRIGHTS); bw.append("--" + request.name + "\n"); currentFileSize = COPYRIGHTS.length(); } } bw.flush(); bw.close(); } System.out.println("done."); } catch (SQLException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (stmt != null) { try { stmt.close(); } catch (SQLException e) { e.printStackTrace(); } } } DumpFilesDescriptor descriptor = new DumpFilesDescriptor(); descriptor.lineNumbers = getDumpLinesNumber(); descriptor.dumpFileNames = dumpFileNames; writeDescriptorFile(descriptor); System.out.println("nb :" + descriptor.lineNumbers); }
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);//w w w.j av a 2 s . c om 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(); }