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:Main.java

public static void main(String... args) {
    try {//from  w w  w  . j a v  a  2 s. c o  m
        Class<?> c = Class.forName(args[0]);
        Class[] argTypes = new Class[] { String[].class };
        Method main = c.getDeclaredMethod("main", argTypes);
        String[] mainArgs = Arrays.copyOfRange(args, 1, args.length);
        System.out.format("invoking %s.main()%n", c.getName());
        main.invoke(null, (Object) mainArgs);

        // production code should handle these exceptions more gracefully
    } catch (ClassNotFoundException x) {
        x.printStackTrace();
    } catch (NoSuchMethodException x) {
        x.printStackTrace();
    } catch (IllegalAccessException x) {
        x.printStackTrace();
    } catch (InvocationTargetException x) {
        x.printStackTrace();
    }
}

From source file:Main.java

public static void main(String... args) throws Exception {
    Class<?> c = Class.forName(args[0]);
    Constructor[] allConstructors = c.getDeclaredConstructors();
    for (Constructor ctor : allConstructors) {
        int searchMod = modifierFromString(args[1]);
        int mods = accessModifiers(ctor.getModifiers());
        if (searchMod == mods) {
            out.format("%s%n", ctor.toGenericString());
            out.format("  [ synthetic=%-5b var_args=%-5b ]%n", ctor.isSynthetic(), ctor.isVarArgs());
        }/*from ww w.jav  a 2s  .c  o m*/
    }
}

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");
    while (resultSet.next()) {
        String productCode = resultSet.getString("product_code");
        String productName = resultSet.getString("product_name");
        int quantity = resultSet.getInt("quantity");
        double price = resultSet.getDouble("price");

        System.out.println(productCode + "\t" + productName + "\t" + quantity + "\t" + price);
    }//from  ww  w.  java2s.  co m

    while (resultSet.previous()) {
        String productCode = resultSet.getString("product_code");
        String productName = resultSet.getString("product_name");
        int quantity = resultSet.getInt("quantity");
        double price = resultSet.getDouble("price");

        System.out.println(productCode + "\t" + productName + "\t" + quantity + "\t" + price);
    }
    connection.close();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {

    String driverName = "com.jnetdirect.jsql.JSQLDriver";
    Class.forName(driverName);

    String serverName = "127.0.0.1";
    String portNumber = "1433";
    String mydatabase = serverName + ":" + portNumber;
    String url = "jdbc:JSQLConnect://" + mydatabase;
    String username = "username";
    String password = "password";

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

    // Disable auto commit
    connection.setAutoCommit(false);//from w w w  .  j  a  va 2s . co  m

    // Do SQL updates...

    // Commit updates
    connection.commit();

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Class[] classParm = null;// w w w .java2  s  . c o m
    Object[] objectParm = null;

    try {
        String name = "java.lang.String";
        Class cl = Class.forName(name);
        java.lang.reflect.Constructor co = cl.getConstructor(classParm);
        System.out.println(co.newInstance(objectParm));
    } catch (Exception e) {
        e.printStackTrace();

    }

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Date date = new Date(0);
    Class.forName("com.mysql.jdbc.Driver");
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctutorial", "root", "root");

    PreparedStatement prest = con.prepareStatement("INSERT Records VALUES(?,?,?)");
    prest.setInt(1, 1);/*  w  w  w  .j a  v  a2  s  .c o m*/
    prest.setString(2, "R");
    prest.setDate(3, date.valueOf("1998-1-17"));
    int row = prest.executeUpdate();
}

From source file:ReflectionDemo1.java

public static void main(String args[]) {
    try {/*  w  w  w. ja  va2  s  .  c o m*/
        Class c = Class.forName("java.awt.Dimension");
        System.out.println("Constructors:");
        Constructor constructors[] = c.getConstructors();
        for (int i = 0; i < constructors.length; i++) {
            System.out.println(" " + constructors[i]);
        }

        System.out.println("Fields:");
        Field fields[] = c.getFields();
        for (int i = 0; i < fields.length; i++) {
            System.out.println(" " + fields[i]);
        }

        System.out.println("Methods:");
        Method methods[] = c.getMethods();
        for (int i = 0; i < methods.length; i++) {
            System.out.println(" " + methods[i]);
        }
    } catch (Exception e) {
        System.out.println("Exception: " + e);
    }
}

From source file:ResultSetExample.java

public static void main(String args[]) {
    try {//www  . j a  va  2  s. co m
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

        Connection con = DriverManager.getConnection("jdbc:odbc:Inventory", "", "");

        Statement stmt = con.createStatement();

        ResultSet rs = stmt.executeQuery("SELECT SupplierName,ProductName, Price "
                + "FROM ProductSuppliersView WHERE CategoryName LIKE '%BEVERAGES%' ");
        while (rs.next()) {
            String supplier = rs.getString("SupplierName");
            String product = rs.getString("ProductName");
            int price = rs.getInt("Price");

            System.out.println(supplier + " sells " + product + " for $" + price);
        }

        stmt.close();
        con.close();
    } catch (Exception e) {
        System.out.println(e);
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Time time = new Time(0);

    Class.forName("com.mysql.jdbc.Driver");
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctutorial", "root", "root");

    String sql = "INSERT child VALUES(?,?)";
    PreparedStatement prest = con.prepareStatement(sql);
    prest.setString(1, "vinod");
    prest.setTime(2, time.valueOf("1:60:60"));
    int row = prest.executeUpdate();
    System.out.println(row + " row(s) affectec)");
}

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);

    Statement stmt = connection.createStatement();

    String filename = "c:/outfile.txt";
    String tablename = "mysql_2_table";
    stmt.executeUpdate("SELECT * INTO OUTFILE \"" + filename + "\" FROM " + tablename);

}