List of usage examples for java.lang Class forName
@CallerSensitive public static Class<?> forName(String className) throws ClassNotFoundException
From source file:Main.java
public static void main(String[] args) throws Exception { Class.forName(DRIVER); Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD); Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery("SELECT id, username FROM users"); ResultSetMetaData metadata = resultSet.getMetaData(); if (metadata.isAutoIncrement(1)) { System.out.println("Column ID is an auto-increment column"); }//from w ww . j av a 2 s . c o m }
From source file:Main.java
public static void main(String[] argv) throws Exception { Class.forName(DB_DRIVER); Connection dbConnection = DriverManager.getConnection(DB_CONNECTION, DB_USER, DB_PASSWORD); CallableStatement callableStatement = null; String insertStoreProc = "{call insertPERSON(?,?,?,?)}"; java.util.Date today = new java.util.Date(); callableStatement = dbConnection.prepareCall(insertStoreProc); callableStatement.setInt(1, 1000);/*from w w w .ja va2 s .c o m*/ callableStatement.setString(2, "name"); callableStatement.setString(3, "system"); callableStatement.setDate(4, new java.sql.Date(today.getTime())); callableStatement.executeUpdate(); System.out.println("Record is inserted into PERSON table!"); callableStatement.close(); dbConnection.close(); }
From source file:ExecuteMethod.java
public static void main(String[] args) { Connection conn = null;//from ww w. j a v a 2 s . c o 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[] args) throws Exception { Connection conn = null;/*ww w . j a v a 2 s .c o m*/ Statement stmt = null; Class.forName(JDBC_DRIVER); conn = DriverManager.getConnection(DB_URL, USER, PASS); System.out.println("Deleting database..."); stmt = conn.createStatement(); conn = DriverManager.getConnection(DB_URL, USER, PASS); stmt = conn.createStatement(); String sql = "INSERT INTO Person VALUES (1, 'A', 'B', 18)"; stmt.executeUpdate(sql); sql = "INSERT INTO Person VALUES (2, 'C', 'D', 25)"; stmt.executeUpdate(sql); sql = "INSERT INTO Person VALUES (3, 'E', 'F', 30)"; stmt.executeUpdate(sql); sql = "INSERT INTO Person VALUES(4, 'S', 'M', 28)"; stmt.executeUpdate(sql); stmt.close(); conn.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { try {/* ww w . j av a2 s .c om*/ String url = "jdbc:odbc:yourdatabasename"; String driver = "sun.jdbc.odbc.JdbcOdbcDriver"; String user = "guest"; String password = "guest"; Class.forName(driver); Connection connection = DriverManager.getConnection(url, user, password); Statement stmt = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); String sqlQuery = "SELECT EMPNO, EName, Job, MGR, HIREDATE FROM EMP"; ResultSet rs = stmt.executeQuery(sqlQuery); int rowSize = 0; while (rs.next()) { rowSize++; } System.out.println("Number of Rows in ResultSet is: " + rowSize); if (rowSize == 0) { System.out.println("Since there are no rows, exiting..."); System.exit(0); } int cursorPosition = Math.round(rowSize / 2); System.out.println("Moving to position: " + cursorPosition); rs.absolute(cursorPosition); System.out.println("Name: " + rs.getString(2)); rs.relative(-1); cursorPosition = rs.getRow(); System.out.println("Moving to position: " + cursorPosition); System.out.println("Name: " + rs.getString(2)); System.out.println("Moving to the first row"); while (!rs.isFirst()) { rs.previous(); } System.out.println("Name: " + rs.getString(2)); connection.close(); } catch (Exception e) { System.err.println(e); } }
From source file:Main.java
public static void main(String[] args) throws Exception { Class.forName(DRIVER); Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD); Statement statement = connection.createStatement(); String query = "SELECT stock_id, name, price FROM stocks"; ResultSet resultSet = statement.executeQuery(query); ResultSetMetaData metadata = resultSet.getMetaData(); int precision = metadata.getPrecision(3); int scale = metadata.getScale(3); System.out.println("Precision: " + precision); System.out.println("Scale : " + scale); }
From source file:Main.java
public static void main(String[] args) throws Exception { Class.forName(DRIVER); Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD); Statement statement = connection.createStatement(); String query = "SELECT a.id, a.username, a.country_id, b.country_name " + "FROM users a " + "LEFT JOIN countries b ON a.country_id = b.id"; ResultSet resultSet = statement.executeQuery(query); ResultSetMetaData metadata = resultSet.getMetaData(); String tableName = metadata.getTableName(1); System.out.println("Table name of column 'id' = " + tableName); tableName = metadata.getTableName(4); System.out.println("Table name of column 'country name' = " + tableName); }
From source file:Main.java
public static void main(String[] args) throws Exception { Class.forName(DRIVER); Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD); Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery("SELECT id, username FROM users"); ResultSetMetaData metadata = resultSet.getMetaData(); int nullability = metadata.isNullable(1); if (nullability == ResultSetMetaData.columnNullable) { System.out.println("Columns ID can have a null value"); } else if (nullability == ResultSetMetaData.columnNoNulls) { System.out.println("Columns ID does not allowed to have a null value"); } else if (nullability == ResultSetMetaData.columnNullableUnknown) { System.out.println("Nullability unknown"); }//from w w w . j a v a2s. c o m }
From source file:Main.java
public static void main(String[] args) throws Exception { ResultSet resultSet = null;//w w w . j ava 2 s.c o m Class.forName(DRIVER); Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD); DatabaseMetaData metadata = connection.getMetaData(); resultSet = metadata.getTypeInfo(); while (resultSet.next()) { String typeName = resultSet.getString("TYPE_NAME"); System.out.println("Type Name = " + typeName); } resultSet.close(); connection.close(); }
From source file:Main.java
public static void main(String[] argv) throws Exception { Class.forName(DB_DRIVER); Connection dbConnection = DriverManager.getConnection(DB_CONNECTION, DB_USER, DB_PASSWORD); PreparedStatement preparedStatementInsert = null; PreparedStatement preparedStatementUpdate = null; String insertTableSQL = "INSERT INTO Person" + "(USER_ID, USERNAME, CREATED_BY, CREATED_DATE) VALUES" + "(?,?,?,?)"; String updateTableSQL = "UPDATE Person SET USERNAME =? " + "WHERE USER_ID = ?"; java.util.Date today = new java.util.Date(); dbConnection.setAutoCommit(false);/*w ww .j av a 2 s . c om*/ preparedStatementInsert = dbConnection.prepareStatement(insertTableSQL); preparedStatementInsert.setInt(1, 9); preparedStatementInsert.setString(2, "101"); preparedStatementInsert.setString(3, "system"); preparedStatementInsert.setTimestamp(4, new java.sql.Timestamp(today.getTime())); preparedStatementInsert.executeUpdate(); preparedStatementUpdate = dbConnection.prepareStatement(updateTableSQL); preparedStatementUpdate.setString(1, "new string"); preparedStatementUpdate.setInt(2, 999); preparedStatementUpdate.executeUpdate(); dbConnection.commit(); dbConnection.close(); }