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[]) throws Exception {
    Class c = Class.forName("MyClass");

    Constructor constructors[] = c.getDeclaredConstructors();
    Object obj = null;/*  w  w  w .  j a  va2 s .co m*/
    for (Constructor cons : constructors) {
        Class[] params = cons.getParameterTypes();
        if (params.length == 1 && params[0] == int.class) {
            obj = cons.newInstance(10);
            break;
        }
    }

    if (obj == null) {
        System.out.println("Can't Create MyClass object.");
        return;
    }
}

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

        int row = resultSet.getRow();

        System.out.println(row + ". " + productCode);
    }//from w ww .ja  v a 2s.  co  m
    connection.close();
}

From source file:Main.java

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

    String sql = "INSERT bigdecimal VALUES(?,?)";
    PreparedStatement prest = con.prepareStatement(sql);
    prest.setString(1, "D");
    BigDecimal b = new BigDecimal("111111111111111111111111111111111");
    prest.setBigDecimal(2, b);/*  w ww  .  j a  v a2  s  .co m*/
    prest.executeUpdate();
}

From source file:Main.java

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

    Class.forName("org.hsqldb.jdbcDriver");
    String url = "jdbc:hsqldb:mem:data/tutorial";

    Driver driver = DriverManager.getDriver(url);

    DriverPropertyInfo[] info = driver.getPropertyInfo(url, null);
    for (int i = 0; i < info.length; i++) {
        System.out.println(info[i].name);
        // Is property value required?
        System.out.println(info[i].required);
        // Get current value
        System.out.println(info[i].value);
        // Get description of property
        System.out.println(info[i].description);

        // Get possible choices for property;
        // if null, value can be any string
        String[] choices = info[i].choices;
        if (choices != null) {
            for (int c = 0; c < choices.length; c++) {
                System.out.println(choices[c]);
            }/*from   www  .j a  v a  2  s .c  o m*/
        }
    }

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");

    Connection m_Connection = DriverManager.getConnection(
            "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=MyDatabase", "userid", "password");

    Statement m_Statement = m_Connection.createStatement();
    String query = "SELECT * FROM MyTable";

    ResultSet m_ResultSet = m_Statement.executeQuery(query);

    while (m_ResultSet.next()) {
        System.out.println(//from   w w w.j  ava2  s  .  c  o  m
                m_ResultSet.getString(1) + ", " + m_ResultSet.getString(2) + ", " + m_ResultSet.getString(3));

    }
}

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

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");
    resultSet.afterLast();/*www  .j  av  a  2  s  .  c  o 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 args[]) throws Exception {
    Class c = Class.forName("MyClass");
    System.out.println("\nFields:");
    Field fields[] = c.getDeclaredFields();
    for (Field fld : fields)
        System.out.println(" " + fld);
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    Class c = Class.forName("MyClass");
    System.out.println("Constructors:");
    Constructor constructors[] = c.getDeclaredConstructors();
    for (Constructor cons : constructors)
        System.out.println(" " + cons);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    try {//from  w w w .ja  va 2 s.co  m
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

        // Define the data source for the driver
        String sourceURL = "jdbc:odbc:technical_library";

        // Create a connection through the DriverManager 
        Connection databaseConnection = DriverManager.getConnection(sourceURL);
        System.out.println("Connection is: " + databaseConnection);
    } catch (ClassNotFoundException cnfe) {
        System.err.println(cnfe);
    } catch (SQLException sqle) {
        System.err.println(sqle);
    }
}