Example usage for java.lang Class forName

List of usage examples for java.lang Class forName

Introduction

In this page you can find the example usage for java.lang Class forName.

Prototype

@CallerSensitive
public static Class<?> forName(String className) throws ClassNotFoundException 

Source Link

Document

Returns the Class object associated with the class or interface with the given string name.

Usage

From source file:MainClass.java

public static void main(String[] unused) {
    try {/*w  w w. ja  v  a 2s.c o  m*/
        String n = "java.util.List";
        Class c = Class.forName(n);

        if (c.isInterface()) {
            System.out.println(n + " is an interface");
        } else {
            System.out.println(n + " is not an interface");
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:Main.java

public static void main(String[] args) throws ClassNotFoundException {
    Class c = Class.forName("java.lang.String");

    Constructor[] constructors = c.getDeclaredConstructors();
    for (int i = 0; i < constructors.length; i++) {
        print_method_or_constructor(constructors[i]);
    }/*  ww  w. j a  v a  2s. c  o m*/

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Class<?> class1 = Class.forName("Rate");
    Object obj = class1.newInstance();
    for (Method m : class1.getMethods()) {
        if (m.getName().equals("myMethod")) {
            Class<?>[] parameterTypes = m.getParameterTypes();
            System.out.println(Arrays.toString(m.getParameterTypes()));
            Object methodArgs[] = new Object[parameterTypes.length];
            for (Class<?> parameterType : parameterTypes) {
                if (parameterType == Double.TYPE) {
                    double value = 0.5;
                    methodArgs[0] = value;
                }//from ww w . ja v a 2s  .c  o  m
            }
            Rate rate = (Rate) m.invoke(obj, methodArgs);
            System.out.println(rate.getValue());
        }
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

    ResultSet rs = DriverManager.getConnection("jdbc:odbc:employee_xls").createStatement()
            .executeQuery("Select * from [Sheet1$]");

    ResultSetMetaData rsmd = rs.getMetaData();
    int numberOfColumns = rsmd.getColumnCount();

    System.out.println("No of cols " + numberOfColumns);
    while (rs.next()) {
        for (int i = 1; i <= numberOfColumns; i++) {
            String columnValue = rs.getString(i);
            System.out.println(columnValue);
        }//  w  ww.  j a v a  2s.co m
    }
    rs.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String url = "jdbc:mysql://localhost:3306/mydb";
    Class.forName("com.mysql.jdbc.Driver");
    Connection conn = DriverManager.getConnection(url, "root", " ");
    System.out.println("Database connection established");
    conn.close();/*from   w  ww. java  2s.  c o m*/
    System.out.println("Database connection terminated");
}

From source file:ExecuteExample.java

public static void main(String args[]) {
    try {//  www.  jav  a2  s.  c  om
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    } catch (Exception e) {
        System.out.println("JDBC-ODBC driver failed to load.");
        return;
    }

    try {
        Connection con = DriverManager.getConnection("jdbc:odbc:Inventory", "", "");
        Statement stmt = con.createStatement();
        stmt.execute("CREATE TABLE SalesHistory(ProductID NUMBER,Price CURRENCY, TrnsDate DATE)");
        stmt.close();
        con.close();
    } catch (Exception e) {
        System.out.println(e);
    }
}

From source file:Invoke.java

public static void main(String[] args) {
    try {/*from  ww w .jav  a  2  s .  c  om*/
        Class c = Class.forName(args[0]);
        Method m = c.getMethod(args[1]);
        Object ret = m.invoke(null);
        System.out.println("Invoked static method: " + args[1] + " of class: " + args[0]
                + " with no args\nResults: " + ret);
    } catch (ClassNotFoundException e) {
        System.out.println(e);
    } catch (NoSuchMethodException e2) {
        System.out.println(e2);
    } catch (IllegalAccessException e3) {
        System.out.println(e3);
    } catch (InvocationTargetException e) {
        System.out.println(e);
        System.out.println("Method threw an: " + e.getTargetException());
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Class.forName("com.mysql.jdbc.Driver");
    Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/testdb", "root", "");

    Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
            ResultSet.CONCUR_READ_ONLY);

    ResultSet resultSet = statement.executeQuery("SELECT * FROM products");
    connection.close();//from   w  ww . j  a va  2s. com
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String driverName = "org.gjt.mm.mysql.Driver";
    Class.forName(driverName);

    String serverName = "localhost";
    String mydatabase = "mydatabase";
    String url = "jdbc:mysql :// " + serverName + "/" + mydatabase;

    String username = "username";
    String password = "password";
    Connection connection = DriverManager.getConnection(url, username, password);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String driverName = "org.gjt.mm.mysql.Driver";
    Class.forName(driverName);

    String serverName = "localhost";
    String mydatabase = "mydatabase";
    String url = "jdbc:mysql://" + serverName + "/" + mydatabase;

    String username = "username";
    String password = "password";
    Connection connection = DriverManager.getConnection(url, username, password);
}