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[] argv) throws Exception {
    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

    Connection con = DriverManager.getConnection("jdbc:sqlserver://MYSERVER;databaseName=MYDATABASE", "USERID",
            "PASSWORD");

    CallableStatement proc_stmt = con.prepareCall("{ call generateID(?) }");

    proc_stmt.setString(1, "employee");
    ResultSet rs = proc_stmt.executeQuery();

    if (rs.next()) {
        int employeeId = rs.getInt(1);
        System.out.println("Generated employeeId: " + employeeId);
    } else {//  w w  w  .  j  ava  2 s.c o  m
        System.out.println("Stored procedure couldn't generate new Id");
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    int sum = 0;//from w  w  w  .ja  v a 2  s .c o  m
    Class.forName("com.mysql.jdbc.Driver");
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctutorial", "root", "root");
    Statement st = con.createStatement();
    ResultSet res = st.executeQuery("SELECT SUM(col) FROM mytable");
    while (res.next()) {
        int c = res.getInt(1);
        sum = sum + c;
    }
    System.out.println("Sum of column = " + sum);
}

From source file:Main.java

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

    Constructor[] constructors = c.getDeclaredConstructors();
    for (int i = 0; i < constructors.length; i++) {
        print_method_or_constructor(constructors[i]);
    }/* w w  w.j  av  a2s  .  com*/

}

From source file:MainClass.java

public static void main(String[] args) {
    String name = "java.util.Date";
    try {/*from w  w  w.ja va2  s.c  o  m*/
        Class cl = Class.forName(name);
        Class supercl = cl.getSuperclass();
        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:Spy.java

public static void main(String... args) throws Exception {
    Class<?> c = Class.forName("Spy");
    Field[] flds = c.getFields();
    for (Field f : flds) {
        Annotation[] annos = f.getDeclaredAnnotations();
        for (Annotation anno : annos) {
            System.out.println(anno.toString());
        }/* w  ww  .j a v a2 s .co m*/

    }
}

From source file:Main.java

public static void main(String[] args) {
    String name = "java.util.Date";
    try {//www  .  j a va  2 s . co  m
        Class cl = Class.forName(name);
        System.out.println("class " + name);
        System.out.println("Its methods:");
        printMethods(cl);
        System.out.println();
    } catch (ClassNotFoundException e) {
        System.out.println("Class not found.");
    }
}

From source file:DataUpdater.java

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

    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    DriverManager.registerDriver(new JdbcOdbcDriver());
    String url = "jdbc:odbc:Contacts";

    Connection con = DriverManager.getConnection(url);
    Statement stmt = con.createStatement();
    String SQLCommand = "UPDATE CONTACT_INFO " + "SET STREET = '58 Broadway', ZIP = '10008' "
            + "WHERE First_Name = 'Michael' AND " + "Last_Name ='Corleone';";

    stmt.execute(SQLCommand);/*  w  w w .  j  ava  2 s .c o m*/
    con.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection conn = DriverManager
            .getConnection("jdbc:odbc:Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};"
                    + "Dbq=C://Book1.xlsx;");

    PreparedStatement s = conn.prepareStatement("SELECT * FROM [Sheet1$] WHERE [MetricMonth] = ?");
    s.setString(1, "Jul-2013");
    s.execute();/*from w  ww  .  java2s  .co m*/
    ResultSet rs = s.getResultSet();
    if (rs != null) {
        while (rs.next()) {
            System.out.println(rs.getInt("All"));
        }
    }
    s.close();

    conn.close();
}

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

    // Move to the second row
    resultSet.absolute(2);//from   w  w w  . jav a2s .  c  o  m
    System.out.println("You are now in: " + resultSet.getRow());

    // Move 2 records forward from the current position (fourth row)
    resultSet.relative(2);
    System.out.println("You are now in: " + resultSet.getRow());

    // Move to the last row in the result set
    resultSet.absolute(-1);
    System.out.println("You are now in: " + resultSet.getRow());

    // Move 3 records backward from the current position (second row)
    resultSet.relative(-3);
    System.out.println("You are now in: " + resultSet.getRow());
    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 = "SELECT * FROM product WHERE year_made = ?";
    PreparedStatement prest = con.prepareStatement(sql);
    prest.setInt(1, 2002);//  w  w w . java  2 s.  c o m
    ResultSet rs1 = prest.executeQuery();
    while (rs1.next()) {
        String mov_name = rs1.getString(1);
        int mad_year = rs1.getInt(2);
        System.out.println(mov_name + "\t- " + mad_year);
    }
    prest.setInt(1, 2003);
    ResultSet rs2 = prest.executeQuery();
    while (rs2.next()) {
        String mov_name = rs2.getString(1);
        int mad_year = rs2.getInt(2);
        System.out.println(mov_name + "\t- " + mad_year);
    }
}