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

public static void main(String[] args) {
    Connection connection = null;
    PreparedStatement statement = null;
    try {/*from  w w w .j  a  va2s  . co  m*/
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        String url = "jdbc:mysql://localhost/database";
        connection = DriverManager.getConnection(url, "username", "password");

        String sql = "UPDATE employees SET email = ? WHERE employee_id = ?";
        statement = connection.prepareStatement(sql);

        statement.setString(1, "a@a.com");
        statement.setLong(2, 1);
        statement.addBatch();

        statement.setString(1, "b@b.com");
        statement.setLong(2, 2);
        statement.addBatch();

        statement.setString(1, "c@c.com");
        statement.setLong(2, 3);
        statement.addBatch();

        statement.executeBatch();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException e) {
            } // nothing we can do
        }
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
            } // nothing we can do
        }
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String driver = "com.mysql.jdbc.Driver";
    String user = "root";
    String pass = "root";

    Class.forName(driver);
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctutorial", user, pass);

    Statement st = con.createStatement();
    ResultSet rs = st.executeQuery("SELECT * FROM employee6");
    ResultSetMetaData md = rs.getMetaData();
    int col = md.getColumnCount();
    System.out.println("Number of Column : " + col);
    for (int i = 1; i <= col; i++) {
        String col_name = md.getColumnName(i);
        System.out.println(col_name);
    }//from  w  ww. j  a  v  a 2s  .c  o m
}

From source file:Main.java

public static void main(String[] args) throws ClassNotFoundException {
    Class c = Class.forName(args[0]);
    print_class(c);
}

From source file:ReflectApp.java

public static void main(String args[]) {
    String parm = args[0];/*from  w  ww .ja va2  s  .  com*/
    Class className = void.class;
    try {
        className = Class.forName(parm);
    } catch (ClassNotFoundException ex) {
        System.out.println("Not a class or interface.");
        System.exit(0);
    }
    describeClassOrInterface(className, parm);
}

From source file:ResultSetMetaDataExample.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();

    boolean notDone = true;
    String sqlStr = null;// w w w.  ja va  2s  .co m
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    while (notDone) {
        sqlStr = br.readLine();
        if (sqlStr.startsWith("SELECT") || sqlStr.startsWith("select")) {
            ResultSet rs = stmt.executeQuery(sqlStr);
            ResultSetMetaData rsmd = rs.getMetaData();
            int columnCount = rsmd.getColumnCount();
            for (int x = 1; x <= columnCount; x++) {
                String columnName = rsmd.getColumnName(x);
                System.out.print(columnName);
            }
            while (rs.next()) {
                for (int x = 1; x <= columnCount; x++) {
                    if (rsmd.getColumnTypeName(x).compareTo("CURRENCY") == 0)
                        System.out.print("$");
                    String resultStr = rs.getString(x);
                    System.out.print(resultStr + "\t");
                }
            }
        } else if (sqlStr.startsWith("exit"))
            notDone = false;
    }
    stmt.close();
    con.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);

    try {/*from   w  ww  . j  a v a 2 s.  c o m*/
        SQLWarning warning = connection.getWarnings();
        while (warning != null) {
            String message = warning.getMessage();
            String sqlState = warning.getSQLState();
            int errorCode = warning.getErrorCode();
            warning = warning.getNextWarning();
        }
        Statement stmt = connection.createStatement();
        warning = stmt.getWarnings();
        if (warning != null) {

        }
        ResultSet resultSet = stmt.executeQuery("SELECT * FROM my_table");
        while (resultSet.next()) {
            warning = resultSet.getWarnings();
            if (warning != null) {
            }
        }
    } catch (SQLException e) {
    }

}

From source file:PrintIndexedResultSet.java

public static void main(String args[]) throws Exception {
    String query = "SELECT STATE, COUNT(STATE) FROM MEMBER_PROFILES GROUP BY STATE";
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection con = DriverManager.getConnection("jdbc:odbc:Members");
    Statement stmt = con.createStatement();
    stmt.executeUpdate("CREATE INDEX STATE_INDEX ON MEMBER_PROFILES(STATE)");

    java.util.Date startTime = new java.util.Date();

    ResultSet rs = stmt.executeQuery(query);
    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 . j a 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"));
        }
    }
    java.util.Date endTime = new java.util.Date();
    long elapsedTime = endTime.getTime() - startTime.getTime();
    System.out.println("Elapsed time: " + elapsedTime);

    stmt.executeUpdate("DROP INDEX MEMBER_PROFILES.STATE_INDEX");
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String dbURL = "jdbc:odbc:Companies";
    try {//from  w  w w  .  ja  v a 2s  .c  o  m
        // Load the jdbc-odbc bridge driver
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        // Enable logging
        DriverManager.setLogWriter(new PrintWriter((System.err)));

        System.out.println("Getting Connection");
        Connection conn = DriverManager.getConnection(dbURL, "user", "password");

        SQLWarning warn = conn.getWarnings();
        while (warn != null) {
            System.out.println("SQLState: " + warn.getSQLState());
            System.out.println("Message:  " + warn.getMessage());
            System.out.println("Vendor:   " + warn.getErrorCode());
            System.out.println("");
            warn = warn.getNextWarning();
        }

        conn.close();
    } catch (ClassNotFoundException e) {
        System.out.println("Can't load driver " + e);
    } catch (SQLException e) {
        System.out.println("Database access failed " + e);
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Class.forName("oracle.jdbc.driver.OracleDriver");
    Connection conn = DriverManager.getConnection(url, username, password);

    DatabaseMetaData metadata = conn.getMetaData();
    ResultSet result = metadata.getProcedures(null, "JAVA", "%");
    while (result.next()) {
        System.out.println(result.getString("PROCEDURE_CAT") + " - " + result.getString("PROCEDURE_SCHEM")
                + " - " + result.getString("PROCEDURE_NAME"));
    }//w  ww.j  a v a  2s .com
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    System.setProperty("oracle.net.tns_admin", "C:/app/product/11.2.0/client_1/NETWORK/ADMIN");
    String dbURL = "jdbc:oracle:thin:@ENTRY_FROM_TNSNAMES";

    Class.forName("oracle.jdbc.OracleDriver");

    Connection conn = DriverManager.getConnection(dbURL, "your_user_name", "your_password");

    System.out.println("Connection established");

    Statement stmt = conn.createStatement();

    ResultSet rs = stmt.executeQuery("SELECT dummy FROM dual");

    if (rs.next()) {
        System.out.println(rs.getString(1));
    }//from  www . j a  v a2s. com
    stmt.close();
    conn.close();
}