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) {
    Connection conn = null;/*from w  ww  .  j  a va 2 s.  c o  m*/
    Statement stmt = null;
    boolean executeResult;
    try {
        String driver = "oracle.jdbc.driver.OracleDriver";
        Class.forName(driver).newInstance();
        System.out.println("Connecting to database...");
        String jdbcUrl = "jdbc:oracle:thin:@localhost:1521:ORCL";
        conn = DriverManager.getConnection(jdbcUrl, "yourName", "mypwd");
        stmt = conn.createStatement();
        conn.setAutoCommit(false);
        if (!conn.getAutoCommit())
            System.out.println("Auto-commit is set to false");
        String sql = "INSERT INTO Location VALUES(715,'Houston')";
        stmt.executeUpdate(sql);
        sql = "INSERT INTO Employees VALUES" + "(8,'K','4351',{d '2000-02-00'},715)";
        stmt.executeUpdate(sql);
        conn.commit();
    } catch (SQLException se) {
        String msg = se.getMessage();
        msg = "SQLException occured with message: " + msg;
        System.out.println(msg);
        System.out.println("Starting rollback operations...");
        try {
            conn.rollback();
        } catch (SQLException se2) {
            se2.printStackTrace();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    try {//from  w w w. ja  va  2  s . co m
        String url = "jdbc:odbc:yourdatabasename";
        String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
        String user = "guest";
        String password = "guest";

        Class.forName(driver);
        Connection connection = DriverManager.getConnection(url, user, password);

        DatabaseMetaData meta = connection.getMetaData();

        System.out.println("We are using " + meta.getDatabaseProductName());
        System.out.println("Version is " + meta.getDatabaseProductVersion());

        connection.close();
    } catch (Exception e) {
        System.err.println(e);
    }
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    String URL = "jdbc:microsoft:sqlserver://yourServer:1433;databasename=pubs";
    String userName = "yourUser";
    String password = "yourPassword";

    Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
    Connection con = DriverManager.getConnection(URL, userName, password);
    CallableStatement callstmt = con
            .prepareCall("INSERT INTO myIdentTable (col2) VALUES (?);SELECT @@IDENTITY");
    callstmt.setString(1, "testInputBatch");
    callstmt.execute();/*from  w w w .ja v a 2  s  .c o m*/

    int iUpdCount = callstmt.getUpdateCount();
    boolean bMoreResults = true;
    ResultSet rs = null;
    int myIdentVal = -1; // to store the @@IDENTITY

    while (bMoreResults || iUpdCount != -1) {
        rs = callstmt.getResultSet();
        if (rs != null) {
            rs.next();
            myIdentVal = rs.getInt(1);
        }
        bMoreResults = callstmt.getMoreResults();
        iUpdCount = callstmt.getUpdateCount();
    }
    callstmt.close();
    con.close();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String url = "jdbc:mysql://localhost:3306/";
    String dbName = "javatutorial";
    String userName = "root";
    String password = "root";

    Class.forName("com.mysql.jdbc.Driver");
    Connection con = DriverManager.getConnection(url + dbName, userName, password);
    File imgfile = new File("images.jpg");
    FileInputStream fin = new FileInputStream(imgfile);
    PreparedStatement pre = con.prepareStatement("insert into Image values(?,?,?)");
    pre.setInt(1, 5);/*www  . j  a va  2  s .c  o  m*/
    pre.setString(2, "A");
    pre.setBinaryStream(3, fin, (int) imgfile.length());
    pre.executeUpdate();
    pre.close();
    con.close();
}

From source file:X.java

public static void main(String[] args) {
    try {/*from   www.  j a  va  2 s  .  c  o m*/
        Class<?> clazz = Class.forName("X");
        X x = (X) clazz.newInstance();
        Class[] argTypes = { String.class };
        Method method = clazz.getMethod("objectMethod", argTypes);
        Type[] exp = method.getGenericExceptionTypes();
        for (Type anno : exp) {
            System.out.println(anno);
        }
        System.out.println();

    } catch (Exception e) {
        System.err.println(e);
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Statement statement = null;//from w w w . j  a v  a 2  s  .c o  m
    String url = "jdbc:odbc:databaseName";
    String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
    String username = "guest";
    String password = "guest";
    Class.forName(driver);
    Connection connection = DriverManager.getConnection(url, username, password);
    statement = connection.createStatement();
    System.out.println("Driver          :  " + driver);

    // Put each method call in a separate try block to execute them all
    System.out.print("\nMaximum rows    :");
    int maxRows = statement.getMaxRows();
    System.out.print(maxRows == 0 ? " No limit" : " " + maxRows);

    System.out.print("\nMax field size  :");
    int maxFieldSize = statement.getMaxFieldSize();
    System.out.print(maxFieldSize == 0 ? " No limit" : " " + maxFieldSize);

    System.out.print("\nTimeout          :");
    int queryTimeout = statement.getQueryTimeout();
    System.out.print(queryTimeout == 0 ? " No limit" : " " + queryTimeout);

}

From source file:ChainedExceptionDemo.java

public static void main(String[] args) {
    String driver = "com.mysql.jdbc.Driver";
    String connectionURL = "jdbc:mysql://127.0.0.1:3306/sample";
    try {/* w ww  . ja  v a2  s .c  o m*/
        Class.forName(driver);
    } catch (java.lang.ClassNotFoundException e) {
        e.printStackTrace();
    }
    try {
        conn = DriverManager.getConnection(connectionURL);
        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery("SELECT * FROM NONEXISTINGTABLE");
        rs.next();
        rs.close();
    } catch (SQLException sx) {
        for (Throwable e : sx) {
            System.err.println("Error encountered: " + e);
        }
    }
}

From source file:SimpleConnection.java

static public void main(String args[]) {
    Connection connection = null;

    if (args.length != 4) {
        System.out.println("Syntax: java SimpleConnection " + "DRIVER URL UID PASSWORD");
        return;//from   ww  w. j  ava2  s . c  o  m
    }
    try {
        Class.forName(args[0]).newInstance();
    } catch (Exception e) {
        e.printStackTrace();
        return;
    }
    try {
        connection = DriverManager.getConnection(args[1], args[2], args[3]);
        System.out.println("Connection successful!");
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

From source file:InvokeMain.java

public static void main(String[] argv) {
    //+/*from  ww w .  j  av a 2  s. c  o m*/
    try {
        // First, find the class.
        Class c = Class.forName("InvokeMain"); // RECURSION
        System.out.println(c);

        // Create the array of Argument Types
        Class[] argTypes = { argv.getClass(), // array is Object!
        };

        // Now find the method
        Method m = c.getMethod("main", argTypes);
        System.out.println(m);

        // Create the actual argument array
        Object passedArgv[] = { argv };

        // Now invoke the method.
        m.invoke(null, passedArgv);

    } catch (Exception e) {
        System.err.println(e);
    }
    //-
}

From source file:X.java

public static void main(String[] args) {
    try {//  w w w  .  ja  v a2 s .c om
        Class<?> clazz = Class.forName("X");
        X x = (X) clazz.newInstance();
        Class[] argTypes = { String.class };
        Method method = clazz.getMethod("objectMethod", argTypes);
        Class<?>[] exp = method.getExceptionTypes();
        for (Class anno : exp) {
            System.out.println(anno);
        }
        System.out.println();

    } catch (Exception e) {
        System.err.println(e);
    }
}