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.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    String URL = "jdbc:odbc:dbName";
    Connection dbConn = DriverManager.getConnection(URL, "user", "pass");
    PrintWriter w = new PrintWriter(new OutputStreamWriter(System.out));
    DriverManager.setLogWriter(w);
    dbConn.close();//from  w  ww  .  jav a 2  s . co  m

    PreparedStatement prepstmt;
    prepstmt = dbConn.prepareStatement("SELECT id FROM employee");
    prepstmt.execute();
    prepstmt.close();
    dbConn.close();
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    String connectionURL = "jdbc:mysql://localhost:3306/test";
    Class.forName("com.mysql.jdbc.Driver");
    Connection con = DriverManager.getConnection(connectionURL, "login", "password");
    Statement stmt = con.createStatement();
    stmt.execute("CREATE TRIGGER obs_update BEFORE UPDATE ON obs " //
            + "FOR EACH ROW "//
            + "BEGIN "//
            + "IF OLD.voided = 0 AND NEW.voided = 1 THEN "//
            + "   DELETE FROM emp WHERE id = OLD.obs_id; "//
            + "ELSE "//
            + "   UPDATE emp SET emp.revision_token = NOW() "//
            + "   WHERE NEW.id = emp.id; "//
            + "END IF; "//
            + "END;");
    con.close();//from   w ww .j  av a2  s.co m
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String driverName = "oracle.jdbc.driver.OracleDriver";
    Class.forName(driverName);

    String serverName = "127.0.0.1";
    String portNumber = "1521";
    String sid = "mydatabase";
    String url = "jdbc:oracle:thin:@" + serverName + ":" + portNumber + ":" + sid;
    String username = "username";
    String password = "password";
    Connection connection = DriverManager.getConnection(url, username, password);

    Statement stmt = connection.createStatement();

    stmt.execute("CREATE TYPE object2 AS OBJECT(col_string2 VARCHAR(30), col_integer2 NUMBER)");

    stmt.execute("CREATE TYPE object1 AS OBJECT(col_string1 VARCHAR(30), col_integer2 object2)");

    stmt.execute("CREATE TABLE object1_table(col_integer NUMBER, col_object1 object1)");
}

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);
    DatabaseMetaData dbmd = connection.getMetaData();
    // Get max table name length
    int length = dbmd.getMaxTableNameLength();
    System.out.println(length);//from  w w  w  . j a  va2  s  .c  o  m

}

From source file:MainClass.java

public static void main(String[] args) {
    try {// w ww. j  a  va2 s.  c o  m
        Class c = Class.forName("java.util.ArrayList");

        Constructor constructors[] = c.getDeclaredConstructors();
        for (int i = 0; i < constructors.length; i++) {
            System.out.print(constructors[i].getName() + ": ");
            Class parameters[];
            parameters = constructors[i].getParameterTypes();
            for (int j = 0; j < parameters.length; j++) {
                String s = parameters[j].getName();
                s = s.substring(s.lastIndexOf(".") + 1, s.length());
                System.out.print(s + " ");
            }
            System.out.println("");
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:MainClass.java

public static void main(String[] args) {
    try {/*w  ww. jav a  2  s.  c  o  m*/
        Class c = Class.forName("java.util.ArrayList");

        Constructor constructors[] = c.getConstructors();
        for (int i = 0; i < constructors.length; i++) {
            System.out.print(constructors[i].getName() + ": ");
            Class parameters[];
            parameters = constructors[i].getParameterTypes();
            for (int j = 0; j < parameters.length; j++) {
                String s = parameters[j].getName();
                s = s.substring(s.lastIndexOf(".") + 1, s.length());
                System.out.print(s + " ");
            }
            System.out.println("");
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

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);
    Statement stmt = connection.createStatement();

    // Use TRUNCATE
    String sql = "TRUNCATE my_table";
    // Execute deletion
    stmt.executeUpdate(sql);/*ww  w . ja v a2  s.c o  m*/
    // Use DELETE
    sql = "DELETE FROM my_table";
    // Execute deletion
    stmt.executeUpdate(sql);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Connection con = null;//from  w  w w.j a va 2  s .com
    CallableStatement proc_stmt = null;

    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
    con = DriverManager.getConnection("jdbc:sqlserver://MYSERVER;databaseName=MYDATABASE", "USERID",
            "PASSWORD");
    proc_stmt = con.prepareCall("{ call someStoredProc() }");

    proc_stmt.executeQuery();

}

From source file:Main.java

public static void main(String args[]) throws Exception {
    Connection con = null;/*from   w w  w .ja  v a  2s  . co m*/
    Class.forName("oracle.jdbc.driver.OracleDriver");
    con = DriverManager.getConnection("jdbc:oracle:thin:@192.201.32.92:1521:psprd1", "username", "password");
    String query = null;
    ResultSet rset = null;
    query = "UPDATE t1 " + " SET id = ?";
    PreparedStatement stmt = con.prepareStatement(query);
    // stmt.setInt(paramIndex++, null);
    stmt.setNull(1, java.sql.Types.INTEGER);
    stmt.executeUpdate();
    stmt.close();
    query = "select id from t1 ";
    stmt = con.prepareStatement(query);
    rset = stmt.executeQuery();
    rset.next();
    System.out.println(rset.getString("id"));
    rset.close();
    stmt.close();
    con.close();
}

From source file:PrintResultSet.java

public static void main(String args[]) throws Exception {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection con = DriverManager.getConnection("jdbc:odbc:Inventory");
    Statement stmt = con.createStatement();
    ResultSet rs = stmt.executeQuery("select * from employee");
    ResultSetMetaData md = rs.getMetaData();

    int nColumns = md.getColumnCount();
    for (int i = 1; i <= nColumns; i++) {

        System.out.print(md.getColumnLabel(i) + ((i == nColumns) ? "\n" : "\t"));
    }/*from   w  w w .ja  v  a 2s .  co m*/
    while (rs.next()) {
        for (int i = 1; i <= nColumns; i++) {
            System.out.print(rs.getString(i) + ((i == nColumns) ? "\n" : "\t"));
        }
    }
}