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:TestClassForNameApp.java
public static void main(String args[]) { try {//from ww w .j a va2 s.com Class.forName("oracle.jdbc.driver.OracleDriver"); } catch (ClassNotFoundException e) { System.out.println("Oops! Can't find class oracle.jdbc.driver.OracleDriver"); System.exit(1); } Connection conn = null; Statement stmt = null; ResultSet rset = null; try { conn = DriverManager.getConnection("jdbc:oracle:thin:@dssw2k01:1521:orcl", "scott", "tiger"); stmt = conn.createStatement(); rset = stmt.executeQuery("select 'Hello '||USER||'!' result from dual"); while (rset.next()) System.out.println(rset.getString(1)); rset.close(); rset = null; stmt.close(); stmt = null; conn.close(); conn = null; } catch (SQLException e) { System.out.println("Darn! A SQL error: " + e.getMessage()); } finally { if (rset != null) try { rset.close(); } catch (SQLException ignore) { } if (stmt != null) try { stmt.close(); } catch (SQLException ignore) { } if (conn != null) try { conn.close(); } catch (SQLException ignore) { } } }
From source file:Main.java
public static void main(String[] argv) throws Exception { Connection dbConnection = null; PreparedStatement preparedStatement = null; Class.forName(DB_DRIVER);/* ww w . j a v a 2 s .co m*/ dbConnection = DriverManager.getConnection(DB_CONNECTION, DB_USER, DB_PASSWORD); String selectSQL = "SELECT USER_ID, USERNAME FROM Person WHERE USER_ID = ?"; preparedStatement = dbConnection.prepareStatement(selectSQL); preparedStatement.setInt(1, 1001); ResultSet rs = preparedStatement.executeQuery(); while (rs.next()) { String userid = rs.getString("USER_ID"); String username = rs.getString("USERNAME"); System.out.println("userid : " + userid); System.out.println("username : " + username); } preparedStatement.close(); dbConnection.close(); }
From source file:ExecuteMethod.java
public static void main(String[] args) { Connection conn = null;// w w w . jav a 2 s .co m Statement stmt = null; ResultSet rs = null; boolean executeResult; try { String driver = "oracle.jdbc.driver.OracleDriver"; Class.forName(driver).newInstance(); System.out.println("Connecting to database..."); String jdbcUrl = "jdbc:oracle:thin:@localhost:1521:ORCL"; conn = DriverManager.getConnection(jdbcUrl, "test", "mypwd"); stmt = conn.createStatement(); String sql = "INSERT INTO Employees VALUES" + "(1,'G','4351',{d '1996-12-31'},500)"; executeResult = stmt.execute(sql); processExecute(stmt, executeResult); sql = "SELECT * FROM Employees ORDER BY hiredate"; executeResult = stmt.execute(sql); processExecute(stmt, executeResult); } catch (Exception e) { e.printStackTrace(); } finally { try { if (conn != null) conn.close(); } catch (SQLException se) { se.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 ww . java2 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); Statement stmt = connection.createStatement(); ResultSet rs = stmt.executeQuery("SELECT col_blob FROM mysql_all_table"); if (rs.next()) { Blob blob = rs.getBlob("col_blob"); long blobLength = blob.length(); int pos = 1; // position is 1-based int len = 10; byte[] bytes = blob.getBytes(pos, len); InputStream is = blob.getBinaryStream(); int b = is.read(); } }
From source file:DbUtilsUseBeanMySQL.java
public static void main(String[] args) { Connection conn = null;/*from w ww .j av a 2 s. c om*/ String jdbcURL = "jdbc:mysql://localhost/octopus"; String jdbcDriver = "com.mysql.jdbc.Driver"; String user = "root"; String password = "root"; try { DbUtils.loadDriver(jdbcDriver); conn = DriverManager.getConnection(jdbcURL, user, password); QueryRunner qRunner = new QueryRunner(); List beans = (List) qRunner.query(conn, "select id, name from animals_table", new BeanListHandler(Employee.class)); for (int i = 0; i < beans.size(); i++) { Employee bean = (Employee) beans.get(i); bean.print(); } } catch (SQLException e) { // handle the exception e.printStackTrace(); } finally { DbUtils.closeQuietly(conn); } }
From source file:Main.java
public static void main(String[] argv) throws Exception { String driverName = "com.jnetdirect.jsql.JSQLDriver"; Class.forName(driverName);// ww w . j a v a 2 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); try { SQLWarning warning = connection.getWarnings(); while (warning != null) { String message = warning.getMessage(); String sqlState = warning.getSQLState(); int errorCode = warning.getErrorCode(); warning = warning.getNextWarning(); } Statement stmt = connection.createStatement(); warning = stmt.getWarnings(); if (warning != null) { } ResultSet resultSet = stmt.executeQuery("SELECT * FROM my_table"); while (resultSet.next()) { warning = resultSet.getWarnings(); if (warning != null) { } } } catch (SQLException e) { } }
From source file:DbUtilsUseMapMySQL.java
public static void main(String[] args) { Connection conn = null;// ww w . ja v a 2 s . c o m String jdbcURL = "jdbc:mysql://localhost/octopus"; String jdbcDriver = "com.mysql.jdbc.Driver"; String user = "root"; String password = "root"; try { DbUtils.loadDriver(jdbcDriver); conn = DriverManager.getConnection(jdbcURL, user, password); QueryRunner qRunner = new QueryRunner(); List mapList = (List) qRunner.query(conn, "select id, name from animals_table", new MapListHandler()); for (int i = 0; i < mapList.size(); i++) { Map map = (Map) mapList.get(i); System.out.println("id=" + map.get("id")); System.out.println("name=" + map.get("name")); System.out.println("-----------------"); } System.out.println("DbUtils_UseMap_MySQL: end."); } catch (SQLException e) { // handle the exception e.printStackTrace(); } finally { DbUtils.closeQuietly(conn); } }
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 .j av a 2 s.c om*/ 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); DatabaseMetaData dbmd = connection.getMetaData(); ResultSet resultSet = dbmd.getTypeInfo(); while (resultSet.next()) { String typeName = resultSet.getString("TYPE_NAME"); short dataType = resultSet.getShort("DATA_TYPE"); getJdbcTypeName(dataType); } }
From source file:Main.java
public static void main(String[] argv) throws Exception { String driverName = "com.jnetdirect.jsql.JSQLDriver"; Class.forName(driverName);//from ww w . ja va 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); DatabaseMetaData dbmd = connection.getMetaData(); ResultSet resultSet = dbmd.getTypeInfo(); while (resultSet.next()) { // Get the database-specific type name String typeName = resultSet.getString("TYPE_NAME"); // Get the java.sql.Types type to which this database-specific type is // mapped short dataType = resultSet.getShort("DATA_TYPE"); getJdbcTypeName(dataType); } }
From source file:Main.java
public static void main(String[] args) throws Exception { try {/* w ww . j a va2s . 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(2))); while ((input = reader.readLine()) != null) { System.out.println(input); } } connection.close(); } catch (Exception e) { System.err.println(e); } }