List of usage examples for java.lang Class forName
@CallerSensitive public static Class<?> forName(String className) throws ClassNotFoundException
From source file:X.java
public static void main(String[] args) throws Exception { Class<?> clazz = Class.forName("X"); X x = (X) clazz.newInstance();//from w w w. j av a 2 s.c om Field f = clazz.getField("i"); System.out.println(f.getInt(x)); // Output: 10 f.setInt(x, 20); System.out.println(f.getInt(x)); // Output: 20 f = clazz.getField("PI"); System.out.println(f.getDouble(null)); // Output: 3.14 f.setDouble(x, 20); }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection connection = null; Class.forName("com.mysql.jdbc.Driver"); String url = "jdbc:mysql://localhost/testdb"; String user = "root"; String password = ""; connection = DriverManager.getConnection(url, user, password); Statement stmt = connection.createStatement(); String sql = "INSERT INTO users (name, address) VALUES ('Foo', 'Street')"; stmt.execute(sql);// ww w . j a v a2 s .com connection.close(); }
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 conn = DriverManager.getConnection(URL, "user", "passw"); DatabaseMetaData dmd = conn.getMetaData(); ResultSet rs1 = dmd.getSchemas(); while (rs1.next()) { String ss = rs1.getString(1); ResultSet rs2 = dmd.getTables(null, ss, "%", null); while (rs2.next()) System.out.println(rs2.getString(3) + " " + rs2.getString(4)); }/* w w w . j a va 2s .co m*/ conn.close(); }
From source file:MainClass.java
public static void main(String args[]) throws Exception { try {/*w ww. j ava2s .c o m*/ Class c = Class.forName("java.text.NumberFormat"); Method m = c.getMethod("getInstance"); Object ret = m.invoke(null); System.out.println("Results: " + ret); } catch (ClassNotFoundException e) { System.out.println("Class.forName( ) can't find the class"); } catch (NoSuchMethodException e2) { System.out.println("method doesn't exist"); } catch (IllegalAccessException e3) { System.out.println("no permission to invoke that method"); } catch (InvocationTargetException e) { System.out.println("an exception ocurred while invoking that method"); System.out.println("Method threw an: " + e.getTargetException()); } }
From source file:Main.java
public static void main(String[] args) { String name = "java.util.Date"; try {/*from w w w .j a v a2 s . c o m*/ Class cl = Class.forName(name); System.out.println("class " + name); System.out.println("Its methods:"); printFields(cl); System.out.println(); } catch (ClassNotFoundException e) { System.out.println("Class not found."); } }
From source file:Main.java
public static void main(String[] argv) throws Exception { Connection con = null;//ww w .j ava2 s.c o m Class.forName("com.mysql.jdbc.Driver"); con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctutorial", "root", "root"); String sql = "INSERT datatypes VALUES(?,?,?)"; PreparedStatement prest = con.prepareStatement(sql); prest.setByte(1, (byte) 5); prest.setShort(2, (short) 65); prest.setLong(3, (long) 254); int row = prest.executeUpdate(); System.out.println(row + " row(s) affected)"); }
From source file:Main.java
public static void main(String[] args) throws Exception { Class.forName("org.apache.derby.jdbc.ClientDriver"); Connection con = DriverManager.getConnection("jdbc:derby://localhost:1527/testDb", "name", "pass"); Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); String insertEmp1 = "insert into emp values(10,'A','trainee')"; String insertEmp2 = "insert into emp values(11,'B','trainee')"; String insertEmp3 = "insert into emp values(12,'C','trainee')"; con.setAutoCommit(false);//w ww . j a v a 2 s . c om stmt.addBatch(insertEmp1); stmt.addBatch(insertEmp2); stmt.addBatch(insertEmp3); ResultSet rs = stmt.executeQuery("select * from emp"); rs.last(); System.out.println("rows before batch execution= " + rs.getRow()); stmt.executeBatch(); con.commit(); System.out.println("Batch executed"); rs = stmt.executeQuery("select * from emp"); rs.last(); System.out.println("rows after batch execution= " + rs.getRow()); }
From source file:RectangleFields.java
public static void main(String args[]) { Class rectClass = null;//from w w w .j av a2 s .c om Field rectField[] = null; try { rectClass = Class.forName("java.awt.Rectangle"); rectField = rectClass.getDeclaredFields(); } catch (SecurityException se) { System.out.println("Access to Rectangle fields denied"); } catch (ClassNotFoundException cnfe) { System.out.println("Didn't find the Rectangle class"); } System.out.println("Fields in Rectangle are:"); for (int i = 0; i < rectField.length; i++) { System.out.println(rectField[i].toString()); } }
From source file:DataInserter.java
public static void main(String args[]) throws Exception { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); DriverManager.registerDriver(new JdbcOdbcDriver()); String SQLCommand = "INSERT INTO CONTACT_INFO " + "(First_Name,MI,Last_Name,Street,City,State,Zip) " + "VALUES " + "('Michael','J','Corleone','86 Horsehead Blvd','NY','NY','12345');"; String url = "jdbc:odbc:Contacts"; Connection con = DriverManager.getConnection(url); Statement stmt = con.createStatement(); stmt.execute(SQLCommand);//from w w w.j a va 2 s.c o m con.close(); }
From source file:Main.java
public static void main(String[] argv) throws Exception { String driver = "com.mysql.jdbc.Driver"; Class.forName(driver); Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctutorial", "root", "root"); Statement st = con.createStatement(); int val = st.executeUpdate("INSERT employee VALUES(" + 13 + "," + "'Aman'" + ")"); System.out.println("1 row affected"); }