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 { Connection conn = null;//from www. j a v a 2s . co 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 = "SELECT id, first, last, age FROM Person"; ResultSet rs = stmt.executeQuery(sql); while (rs.next()) { // Retrieve by column name int id = rs.getInt("id"); int age = rs.getInt("age"); String first = rs.getString("firstName"); String last = rs.getString("lastName"); // Display values System.out.print("ID: " + id); System.out.print(", Age: " + age); System.out.print(", First: " + first); System.out.println(", Last: " + last); } rs.close(); stmt.close(); conn.close(); }
From source file:Main.java
public static void main(String... args) { String argType = (args.length == 0 ? "" : args[0]); try {/*from w w w .j ava 2s .c om*/ Class<?> c = Class.forName("Main"); if ("".equals(argType)) { // IllegalArgumentException: wrong number of arguments Object o = c.getConstructor().newInstance("foo"); } else if ("int".equals(argType)) { // NoSuchMethodException - looking for int, have Integer Object o = c.getConstructor(int.class); } else if ("Object".equals(argType)) { // newInstance() does not perform method resolution Object o = c.getConstructor(Object.class).newInstance("foo"); } else { assert false; } // production code should handle these exceptions more gracefully } catch (ClassNotFoundException x) { x.printStackTrace(); } catch (NoSuchMethodException x) { x.printStackTrace(); } catch (InvocationTargetException x) { x.printStackTrace(); } catch (InstantiationException x) { x.printStackTrace(); } catch (IllegalAccessException x) { x.printStackTrace(); } }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = null;/*from w ww . ja va2 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 = "UPDATE Registration SET age = 30 WHERE id in (1, 2)"; stmt.executeUpdate(sql); sql = "SELECT id, first, last, age FROM Person"; ResultSet rs = stmt.executeQuery(sql); while (rs.next()) { // Retrieve by column name int id = rs.getInt("id"); int age = rs.getInt("age"); String first = rs.getString("firstName"); String last = rs.getString("lastName"); // Display values System.out.print("ID: " + id); System.out.print(", Age: " + age); System.out.print(", First: " + first); System.out.println(", Last: " + last); } rs.close(); stmt.close(); conn.close(); }
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 * FROM users"); ResultSetMetaData metadata = resultSet.getMetaData(); int columnCount = metadata.getColumnCount(); ArrayList<String> columns = new ArrayList<String>(); for (int i = 1; i < columnCount; i++) { String columnName = metadata.getColumnName(i); columns.add(columnName);/* w w w .j av a 2 s .c o m*/ } while (resultSet.next()) { for (String columnName : columns) { String value = resultSet.getString(columnName); System.out.println(columnName + " = " + value); } } }
From source file:Main.java
public static void main(String[] args) throws Exception { try {// w w w . j av a 2 s. c o m String url = "jdbc:odbc:databaseName"; String driver = "sun.jdbc.odbc.JdbcOdbcDriver"; String user = "guest"; String password = "guest"; FileInputStream fis = new FileInputStream("somefile.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, "TryInputStream"); // Set first field statement.setAsciiStream(2, fis, fis.available()); // Stream is source int rowsUpdated = statement.executeUpdate(); System.out.println("Rows affected: " + rowsUpdated); connection.close(); } catch (Exception e) { System.err.println(e); } }
From source file:JavaDBDemo.java
public static void main(String[] args) { String driver = "org.apache.derby.jdbc.EmbeddedDriver"; String connectionURL = "jdbc:derby:myDatabase;create=true"; String createString = "CREATE TABLE Employee (NAME VARCHAR(32) NOT NULL, ADDRESS VARCHAR(50) NOT NULL)"; try {//from www . j a v a2 s . co m Class.forName(driver); } catch (java.lang.ClassNotFoundException e) { e.printStackTrace(); } try { conn = DriverManager.getConnection(connectionURL); Statement stmt = conn.createStatement(); stmt.executeUpdate(createString); PreparedStatement psInsert = conn.prepareStatement("insert into Employee values (?,?)"); psInsert.setString(1, args[0]); psInsert.setString(2, args[1]); psInsert.executeUpdate(); Statement stmt2 = conn.createStatement(); ResultSet rs = stmt2.executeQuery("select * from Employee"); int num = 0; while (rs.next()) { System.out.println(++num + ": Name: " + rs.getString(1) + "\n Address" + rs.getString(2)); } rs.close(); } catch (Exception e) { e.printStackTrace(); } }
From source file:Main.java
public static void main(String[] args) { Connection conn = null;/*from w ww . ja v a 2 s. c o m*/ Statement stmt = null; try { Class.forName(JDBC_DRIVER); conn = DriverManager.getConnection(DB_URL, USER, PASS); stmt = conn.createStatement(); String sql = "SELECT id, first, last, age FROM Employees"; ResultSet rs = stmt.executeQuery(sql); while (rs.next()) { 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); } rs.close(); stmt.close(); conn.close(); } catch (SQLException se) { se.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } finally { try { if (stmt != null) stmt.close(); if (conn != null) conn.close(); } catch (SQLException se) { se.printStackTrace(); } } }
From source file:Main.java
public static void main(String[] argv) throws Exception { Connection dbConnection = null; PreparedStatement preparedStatement = null; Class.forName(DB_DRIVER); dbConnection = DriverManager.getConnection(DB_CONNECTION, DB_USER, DB_PASSWORD); String deleteSQL = "DELETE Person WHERE USER_ID = ?"; preparedStatement = dbConnection.prepareStatement(deleteSQL); preparedStatement.setInt(1, 1001);/*from www . j av a2 s. c o m*/ preparedStatement.executeUpdate(); preparedStatement.close(); dbConnection.close(); }
From source file:Select.java
public static void main(String args[]) { String url = "jdbc:msql://carthage.imaginary.com/ora"; Connection con = null;/*from ww w . j ava 2 s . c o m*/ 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 { Connection dbConnection = null; PreparedStatement preparedStatement = null; Class.forName(DB_DRIVER); dbConnection = DriverManager.getConnection(DB_CONNECTION, DB_USER, DB_PASSWORD); String updateTableSQL = "UPDATE Person SET USERNAME = ? " + " WHERE USER_ID = ?"; preparedStatement = dbConnection.prepareStatement(updateTableSQL); preparedStatement.setString(1, "newValue"); preparedStatement.setInt(2, 1001);/*from w w w. j av a2s . c o m*/ preparedStatement.executeUpdate(); preparedStatement.executeUpdate(); preparedStatement.close(); dbConnection.close(); }