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 {
    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);
    // Create a scrollable result set
    Statement stmt = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
    ResultSet resultSet = stmt.executeQuery("SELECT * FROM my_table");

    while (resultSet.next()) {
        // Get data at cursor
        String s = resultSet.getString(1);
    }//  w  w  w .  j  a v  a  2s. co  m

    while (resultSet.previous()) {
        // Get data at cursor
        String s = resultSet.getString(1);
    }

    // Move cursor to the first row
    resultSet.first();

    // Move cursor to the last row
    resultSet.last();

    // Move cursor to the end, after the last row
    resultSet.afterLast();

}

From source file:ReflectionTest.java

public static void main(String[] args) {
    String name = "java.util.Date";
    try {//from  w ww . j a  v a2  s  .  com
        Class cl = Class.forName(name);
        Class supercl = cl.getSuperclass();
        System.out.print("class " + name);
        if (supercl != null && !supercl.equals(Object.class))
            System.out.println(" extends " + supercl.getName());
        System.out.print("Its constructors:");
        printConstructors(cl);
        System.out.println();
    } catch (ClassNotFoundException e) {
        System.out.println("Class not found.");
    }
}

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);
    // Create a scrollable result set
    Statement stmt = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
    ResultSet resultSet = stmt.executeQuery("SELECT * FROM my_table");

    // Move cursor forward
    while (resultSet.next()) {
        // Get data at cursor
        String s = resultSet.getString(1);
    }//  w w w.j a v a  2  s . com

    // Move cursor backward
    while (resultSet.previous()) {
        // Get data at cursor
        String s = resultSet.getString(1);
    }

    // Move cursor to the second row
    resultSet.absolute(2);

    // Move cursor to the last row
    resultSet.absolute(-1);

    // Move cursor to the second last row
    resultSet.absolute(-2);
}

From source file:MainClass.java

public static void main(String[] args) {
    Connection connection = null;
    Statement statement = null;//from   w  w w. j a va 2 s  .c  o  m
    try {
        Class.forName("org.gjt.mm.mysql.Driver").newInstance();
        String url = "jdbc:mysql://localhost/mysql";
        connection = DriverManager.getConnection(url, "username", "password");
        statement = connection.createStatement();
        String hrappSQL = "CREATE DATABASE hrapp";
        statement.executeUpdate(hrappSQL);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException e) {
            }
        }
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
            }
        }
    }
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    Connection conn = null;//w  w  w.  j av a  2s.  co  m
    Statement s = null;
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    conn = DriverManager.getConnection("jdbc:odbc:Driver={SQL Server};" + "Server=.\\SQLEXPRESS;"
            + "Trusted_Connection=yes;" + "Database=myDb");
    s = conn.createStatement();
    s.executeQuery("SELECT * FROM dbo.SalesSummary WHERE 0 = 1");
    ResultSet rs = s.getResultSet();
    ResultSetMetaData rsmd = rs.getMetaData();
    for (int i = 1; i <= rsmd.getColumnCount(); i++) {
        System.out.println(String.format("-- Column %d --", i));
        System.out.println(String.format("Column name: %s", rsmd.getColumnName(i)));
        System.out.println(String.format("Database-specific type name: %s", rsmd.getColumnTypeName(i)));
        System.out.println(String.format("Column size (DisplaySize): %d", rsmd.getColumnDisplaySize(i)));
        System.out.println(String.format("java.sql.Type of column: %d", rsmd.getColumnType(i)));
        System.out.println();
    }
    s.close();
    conn.close();
}

From source file:TestClassForNameApp.java

public static void main(String args[]) {

    try {/*from w  ww . j  ava2s .  co m*/
        Class.forName("oracle.jdbc.driver.OracleDriver");
    } catch (ClassNotFoundException e) {
        System.out.println("Oops! Can't find class oracle.jdbc.driver.OracleDriver");
        System.exit(1);
    }

    Connection conn = null;
    Statement stmt = null;
    ResultSet rset = null;
    try {
        conn = DriverManager.getConnection("jdbc:oracle:thin:@dssw2k01:1521:orcl", "scott", "tiger");

        stmt = conn.createStatement();
        rset = stmt.executeQuery("select 'Hello '||USER||'!' result from dual");
        while (rset.next())
            System.out.println(rset.getString(1));
        rset.close();
        rset = null;
        stmt.close();
        stmt = null;
        conn.close();
        conn = null;
    } catch (SQLException e) {
        System.out.println("Darn! A SQL error: " + e.getMessage());
    } finally {
        if (rset != null)
            try {
                rset.close();
            } catch (SQLException ignore) {
            }
        if (stmt != null)
            try {
                stmt.close();
            } catch (SQLException ignore) {
            }
        if (conn != null)
            try {
                conn.close();
            } catch (SQLException ignore) {
            }
    }
}

From source file:Main.java

public static void main(String... args) {
    try {//from w  w w.ja va  2s.co m
        Class<?> c = Class.forName("Main");
        Method[] allMethods = c.getDeclaredMethods();
        for (Method m : allMethods) {
            Type gpType = m.getGenericReturnType();
            System.out.println(gpType);

        }
    } catch (ClassNotFoundException x) {
        x.printStackTrace();
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String url = "jdbc:mysql://localhost/sampledb";
    String username = "root";
    String password = "";

    Class.forName("com.mysql.jdbc.Driver");
    Connection connection = DriverManager.getConnection(url, username, password);

    String sql = "DELETE FROM users WHERE user_id = ?";
    int userId = 2;

    PreparedStatement statement = connection.prepareStatement(sql);

    statement.setInt(1, userId);/*from  w  ww  .j ava2 s  .c  om*/

    int rows = statement.executeUpdate();

    System.out.println(rows + " record(s) deleted.");
    connection.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);
    Statement stmt = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
    ResultSet resultSet = stmt.executeQuery("SELECT * FROM my_table");

    // Get cursor position
    int pos = resultSet.getRow();
    boolean b = resultSet.isBeforeFirst();

    // Move cursor to the first row
    resultSet.next();//from   w  ww. j  a  v a2  s  .c o m

    // Get cursor position
    pos = resultSet.getRow();
    b = resultSet.isFirst();

    // Move cursor to the last row
    resultSet.last();

    // Get cursor position
    pos = resultSet.getRow();
    b = resultSet.isLast();

    // Move cursor past last row
    resultSet.afterLast();

    // Get cursor position
    pos = resultSet.getRow();
    b = resultSet.isAfterLast();

}

From source file:MainClass.java

public static void main(String[] args) {
    Connection connection = null;
    Statement statement = null;//from  w w  w  .j  a  v a2s .  c o m
    try {
        Class.forName("org.gjt.mm.mysql.Driver").newInstance();
        String url = "jdbc:mysql://localhost/hrapp";
        connection = DriverManager.getConnection(url, "username", "password");

        statement = connection.createStatement();
        String employees1SQL = "UPDATE employees SET " + "num_dependants = 4 " + "WHERE employee_id = 123456";
        statement.executeUpdate(employees1SQL);
        String employees2SQL = "UPDATE employees SET " + "num_dependants = 4 " + "WHERE employee_id = 123457";
        statement.executeUpdate(employees2SQL);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException e) {
            }
        }
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
            }
        }
    }
}