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("sun.jdbc.odbc.JdbcOdbcDriver"); Connection c = DriverManager .getConnection("jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=c:/access.mdb"); ResultSet rs = c.createStatement().executeQuery("select objet from Employee"); while (rs.next()) { System.out.println(rs.getString("objet")); }/*from w w w . j a v a2s .c om*/ rs.close(); c.close(); }
From source file:MyClass.java
public static void main(String[] args) throws Exception { Class<?> clazz = Class.forName("MyClass"); MyClass x = (MyClass) clazz.newInstance(); Field f = clazz.getField("i"); System.out.println(f.getByte(x)); // Output: 10 f.setByte(x, (byte) 20); System.out.println(f.getByte(x)); // Output: 20 }
From source file:Main.java
public static void main(String args[]) throws Exception { Class.forName("oracle.jdbc.driver.OracleDriver"); String url = "jdbc:oracle:thin:@localhost:1521:XE"; String userName = "suru"; String password = "password"; Connection con = DriverManager.getConnection(url, userName, password); System.out.println("Connection success!"); Statement stmt = con.createStatement(); String sql = "CREATE TABLE EMP ( ID NUMBER(5) PRIMARY KEY, NAME VARCHAR2(50))"; stmt.execute(sql);/*ww w. j av a2 s. c o m*/ System.out.println("Table created successfully!"); stmt.close(); con.close(); }
From source file:Main.java
public static void main(String[] argv) throws Exception { String url = "jdbc:odbc:datasource"; Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection connection = DriverManager.getConnection(url); System.out.println("Connect to " + connection.getCatalog() + " a success!"); }
From source file:Main.java
public static void main(String[] args) throws Exception { Class.forName("oracle.jdbc.driver.OracleDriver"); String url = "jdbc:oracle:thin:@localhost:1521:javaDemo"; String username = "username"; String password = "welcome"; String sql = "CREATE TABLE books (id NUMBER(11), title VARCHAR2(64))"; Connection connection = DriverManager.getConnection(url, username, password); Statement statement = connection.createStatement(); statement.execute(sql);//from ww w. j av a2 s . c o m connection.close(); }
From source file:MyClass.java
public static void main(String[] args) throws Exception { Class<?> clazz = Class.forName("MyClass"); MyClass x = (MyClass) clazz.newInstance(); Field f = clazz.getField("i"); System.out.println(f.getShort(x)); f.setShort(x, (short) 9); System.out.println(f.getShort(x)); }
From source file:Main.java
public static void main(String args[]) throws Exception { String name = "java.lang.String"; String methodName = "toLowerCase"; Class cl = Class.forName(name); java.lang.reflect.Constructor constructor = cl.getConstructor(new Class[] { String.class }); Object invoker = constructor.newInstance(new Object[] { "AAA" }); Class arguments[] = new Class[] {}; java.lang.reflect.Method objMethod = cl.getMethod(methodName, arguments); Object result = objMethod.invoke(invoker, (Object[]) arguments); System.out.println(result);/*from ww w . j a va2 s . co m*/ }
From source file:MyClass.java
public static void main(String[] args) throws Exception { Class<?> clazz = Class.forName("MyClass"); MyClass x = (MyClass) clazz.newInstance(); Field f = clazz.getField("i"); System.out.println(f.getChar(x)); f.setChar(x, 'a'); System.out.println(f.getBoolean(x)); }
From source file:Main.java
public static void main(String[] args) { String name = "java.util.Collections"; try {// w w w . j a v a2s. c om Class cl = Class.forName(name); printMethods(cl); } catch (ClassNotFoundException e) { System.out.println("Class not found."); } }
From source file:Main.java
public static void main(String[] args) throws Exception { Class.forName("com.mysql.jdbc.Driver").newInstance(); Connection conn = null;//from w w w . j av a 2s . c o m conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "password"); byte[] bkey = "This is some binary stuff".getBytes(); String query = "INSERT INTO keytable (name, `key`) VALUES (?,?)"; PreparedStatement pstmt = conn.prepareStatement(query); pstmt.setString(1, "test"); pstmt.setBytes(2, bkey); pstmt.execute(); conn.close(); }