List of usage examples for java.sql DriverManager getConnection
private static Connection getConnection(String url, java.util.Properties info, Class<?> caller) throws SQLException
From source file:Main.java
public static void main(String[] args) throws Exception { try {//from ww w .j ava2 s.c om 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:Batch.java
static public void main(String[] args) { Connection conn = null;/*from w w w. ja v a 2 s. c o m*/ try { 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:Select.java
public static void main(String args[]) { String url = "jdbc:msql://carthage.imaginary.com/ora"; Connection con = null;//from w w w .j av a 2 s.c om try { String driver = "com.imaginary.sql.msql.MsqlDriver"; Class.forName(driver).newInstance(); } catch (Exception e) { System.out.println("Failed to load mSQL driver."); return; } try { con = DriverManager.getConnection(url, "borg", ""); Statement select = con.createStatement(); ResultSet result = select.executeQuery("SELECT test_id, test_val FROM test"); System.out.println("Got results:"); while (result.next()) { // process results one row at a time int key = result.getInt(1); String val = result.getString(2); System.out.println("key = " + key); System.out.println("val = " + val); } } catch (Exception e) { e.printStackTrace(); } finally { if (con != null) { try { con.close(); } catch (Exception e) { e.printStackTrace(); } } } }
From source file:Main.java
public static void main(String[] argv) throws Exception { String driverName = "com.jnetdirect.jsql.JSQLDriver"; Class.forName(driverName);/*from w w w . jav a 2s. co 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_boolean," + "col_byte," + "col_short," + "col_int," + "col_long," + "col_float," + "col_double," + "col_bigdecimal," + "col_string," + "col_date," + "col_time," + "col_timestamp," + "col_asciistream," + "col_binarystream," + "col_blob) " + "VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"; PreparedStatement pstmt = connection.prepareStatement(sql); pstmt.setBoolean(1, true); pstmt.setByte(2, (byte) 123); pstmt.setShort(3, (short) 123); pstmt.setInt(4, 123); pstmt.setLong(5, 123L); pstmt.setFloat(6, 1.23F); pstmt.setDouble(7, 1.23D); pstmt.setBigDecimal(8, new BigDecimal(1.23)); pstmt.setString(9, "a string"); pstmt.setDate(10, new java.sql.Date(System.currentTimeMillis())); pstmt.setTime(11, new Time(System.currentTimeMillis())); pstmt.setTimestamp(12, new Timestamp(System.currentTimeMillis())); File file = new File("infilename1"); FileInputStream is = new FileInputStream(file); pstmt.setAsciiStream(13, is, (int) file.length()); file = new File("infilename2"); is = new FileInputStream(file); pstmt.setBinaryStream(14, is, (int) file.length()); file = new File("infilename3"); is = new FileInputStream(file); pstmt.setBinaryStream(15, is, (int) file.length()); pstmt.executeUpdate(); }
From source file:Update.java
public static void main(String args[]) { Connection con = null;/* w ww. j a v a 2s .com*/ if (args.length != 2) { System.out.println("Syntax: <java UpdateApp [number] [string]>"); return; } try { String driver = "com.imaginary.sql.msql.MsqlDriver"; Class.forName(driver).newInstance(); String url = "jdbc:msql://carthage.imaginary.com/ora"; con = DriverManager.getConnection(url, "borg", ""); Statement s = con.createStatement(); String test_id = args[0]; String test_val = args[1]; int update_count = s.executeUpdate( "INSERT INTO test (test_id, test_val) " + "VALUES(" + test_id + ", '" + test_val + "')"); System.out.println(update_count + " rows inserted."); s.close(); } catch (Exception e) { e.printStackTrace(); } finally { if (con != null) { try { con.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = null;/*from w w w.j av a 2s. c o m*/ PreparedStatement pstmt = null; Statement stmt = null; ResultSet rs = null; Class.forName(JDBC_DRIVER); conn = DriverManager.getConnection(DB_URL, USER, PASS); stmt = conn.createStatement(); createXMLTable(stmt); File f = new File("build.xml"); long fileLength = f.length(); FileInputStream fis = new FileInputStream(f); String SQL = "INSERT INTO XML_Data VALUES (?,?)"; pstmt = conn.prepareStatement(SQL); pstmt.setInt(1, 100); pstmt.setAsciiStream(2, fis, (int) fileLength); pstmt.execute(); fis.close(); SQL = "SELECT Data FROM XML_Data WHERE id=100"; rs = stmt.executeQuery(SQL); if (rs.next()) { InputStream xmlInputStream = rs.getAsciiStream(1); int c; ByteArrayOutputStream bos = new ByteArrayOutputStream(); while ((c = xmlInputStream.read()) != -1) bos.write(c); System.out.println(bos.toString()); } rs.close(); stmt.close(); pstmt.close(); conn.close(); }
From source file:UpdateLogic.java
public static void main(String args[]) { Connection con = null;/*from w ww . j a v a 2 s .c om*/ if (args.length != 2) { System.out.println("Syntax: <java UpdateLogic [number] [string]>"); return; } try { String driver = "com.imaginary.sql.msql.MsqlDriver"; Class.forName(driver).newInstance(); String url = "jdbc:msql://carthage.imaginary.com/ora"; Statement s; con = DriverManager.getConnection(url, "borg", ""); con.setAutoCommit(false); // make sure auto commit is off! s = con.createStatement();// create the first statement s.executeUpdate("INSERT INTO test (test_id, test_val) " + "VALUES(" + args[0] + ", '" + args[1] + "')"); s.close(); // close the first statement s = con.createStatement(); // create the second statement s.executeUpdate("INSERT into test_desc (test_id, test_desc) " + "VALUES(" + args[0] + ", 'This describes the test.')"); con.commit(); // commit the two statements System.out.println("Insert succeeded."); s.close(); // close the second statement } catch (Exception e) { if (con != null) { try { con.rollback(); } // rollback on error catch (SQLException e2) { } } e.printStackTrace(); } finally { if (con != null) { try { con.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
From source file:Main.java
public static void main(String[] args) { Connection conn = null;/*from ww w . ja v a2 s. co m*/ Statement stmt = null; try { // Register JDBC driver Class.forName(JDBC_DRIVER); // Open a connection System.out.println("Connecting to database..."); conn = DriverManager.getConnection(DB_URL, USER, PASS); // Execute a query System.out.println("Creating statement..."); stmt = conn.createStatement(); String sql; sql = "SELECT id, first, last, age FROM Employees"; stmt.executeUpdate( "CREATE TABLE Employees ( id INTEGER IDENTITY, first VARCHAR(256), last VARCHAR(256),age INTEGER)"); stmt.executeUpdate("INSERT INTO Employees VALUES(1,'Jack','Smith', 100)"); ResultSet rs = stmt.executeQuery(sql); // Extract data from result set while (rs.next()) { // Retrieve by column name int id = rs.getInt("id"); int age = rs.getInt("age"); String first = rs.getString("first"); String last = rs.getString("last"); System.out.print("ID: " + id); System.out.print(", Age: " + age); System.out.print(", First: " + first); System.out.println(", Last: " + last); } // Clean-up environment rs.close(); stmt.close(); conn.close(); } catch (SQLException se) { se.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } finally { // finally block used to close resources try { if (stmt != null) stmt.close(); } catch (SQLException se2) { } try { if (conn != null) conn.close(); } catch (SQLException se) { se.printStackTrace(); } } System.out.println("Goodbye!"); }
From source file:ReverseSelect.java
public static void main(String argv[]) { Connection con = null;/*from w w w . j a va 2 s. c om*/ try { String url = "jdbc:msql://carthage.imaginary.com/ora"; String driver = "com.imaginary.sql.msql.MsqlDriver"; Properties p = new Properties(); Statement stmt; ResultSet rs; p.put("user", "borg"); Class.forName(driver).newInstance(); con = DriverManager.getConnection(url, "borg", ""); stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); rs = stmt.executeQuery("SELECT * from test ORDER BY test_id"); // as a new ResultSet, rs is currently positioned // before the first row System.out.println("Got results:"); // position rs after the last row rs.afterLast(); while (rs.previous()) { int a = rs.getInt("test_id"); String str = rs.getString("test_val"); System.out.print("\ttest_id= " + a); System.out.println("/str= '" + str + "'"); } System.out.println("Done."); } catch (Exception e) { e.printStackTrace(); } finally { if (con != null) { try { con.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
From source file:Main.java
public static void main(String[] args) { Connection conn = null;/*from w ww.j a v a 2 s .c o m*/ Statement stmt = null; try { // STEP 2: Register JDBC driver Class.forName(JDBC_DRIVER); // STEP 3: Open a connection System.out.println("Connecting to database..."); conn = DriverManager.getConnection(DB_URL, USER, PASS); // STEP 4: Execute a query System.out.println("Creating statement..."); stmt = conn.createStatement(); String sql; sql = "SELECT id, first, last, age FROM Employees"; stmt.executeUpdate( "CREATE TABLE Employees ( id INTEGER IDENTITY, first VARCHAR(256), last VARCHAR(256),age INTEGER)"); stmt.executeUpdate("INSERT INTO Employees VALUES(1,'Jack','Smith', 100)"); ResultSet rs = stmt.executeQuery(sql); // STEP 5: Extract data from result set while (rs.next()) { // Retrieve by column name int id = rs.getInt("id"); int age = rs.getInt("age"); String first = rs.getString("first"); String last = rs.getString("last"); System.out.print("ID: " + id); System.out.print(", Age: " + age); System.out.print(", First: " + first); System.out.println(", Last: " + last); } // STEP 6: Clean-up environment rs.close(); stmt.close(); conn.close(); } catch (SQLException se) { se.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } finally { // finally block used to close resources try { if (stmt != null) stmt.close(); } catch (SQLException se2) { } try { if (conn != null) conn.close(); } catch (SQLException se) { se.printStackTrace(); } } System.out.println("Goodbye!"); }