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);
    // Call a procedure with no parameters
    CallableStatement cs = connection.prepareCall("{call myprocin(?)}");

    // Set the value for the IN parameter
    cs.setString(1, "a string");

    cs.execute();/*from   w  ww  .  ja v a2s.c om*/
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    CachedRowSet rs;/*from  w  ww .  ja v  a 2s  . co m*/
    String ROWSET_IMPL_CLASS = "com.sun.rowset.CachedRowSetImpl";

    Class c = Class.forName(ROWSET_IMPL_CLASS);
    rs = (CachedRowSet) c.newInstance();

    rs.setUrl("jdbc:postgresql:dbname");
    rs.setUsername("username");
    rs.setPassword("password");

    rs.setCommand("select * from members where name like ?");
    rs.setString(1, "I%");

    rs.execute();

    while (rs.next()) {
        if (rs.getInt("id") == 42) {
            rs.setString(1, "newString");
            rs.updateRow(); // Normal JDBC

            rs.acceptChanges();
        }
    }
    rs.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
    Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL", "yourName", "mypwd");
    Statement stmt = conn.createStatement();
    stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
    ResultSet rs = stmt.executeQuery("SELECT ssn, name, salary FROM EMPLOYEES");
    printRs(rs);//from   w w  w.j  a  v  a  2 s  .co m

    rs.beforeFirst();

    while (rs.next()) {
        double newSalary = rs.getDouble(3) * 1.053;
        rs.updateDouble("salary", newSalary);
        rs.updateRow();
    }
    printRs(rs);
    conn.close();
}

From source file:CallableStmt.java

    public static void main(String[] args) throws Exception {
    String driver = "oracle.jdbc.driver.OracleDriver";
    Class.forName(driver).newInstance();
    System.out.println("Connecting to database...");
    String jdbcUrl = "jdbc:oracle:thin:@localhost:1521:ORCL";
    Connection conn = DriverManager.getConnection(jdbcUrl, "yourName", "mypwd");
    CallableStatement cstmt = conn.prepareCall("{call getEmpName (?,?)}");
    cstmt.setInt(1, 111111111);/*from   w w w .  j a v a  2 s .  c  om*/
    cstmt.registerOutParameter(2, java.sql.Types.VARCHAR);
    cstmt.execute();
    String empName = cstmt.getString(2);
    System.out.println(empName);
    conn.close();

}

From source file:Main.java

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

    Timestamp tstamp = new Timestamp(0);

    Class.forName("com.mysql.jdbc.Driver");
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctutorial", "root", "root");

    String sql = "INSERT myTable VALUES(?,?)";
    PreparedStatement prest = con.prepareStatement(sql);
    prest.setString(1, "x");
    prest.setTimestamp(2, tstamp.valueOf("2009-02-24 12:51:42.11"));
    int row = prest.executeUpdate();
    System.out.println(row + " row(s) affected)");

}

From source file:IntrospectorDemo.java

public static void main(String args[]) {
    try {//from   w w w.ja va  2  s  .co m
        Class c = Class.forName("Colors");
        BeanInfo beanInfo = Introspector.getBeanInfo(c);

        System.out.println("Properties:");
        PropertyDescriptor propertyDescriptor[] = beanInfo.getPropertyDescriptors();
        for (int i = 0; i < propertyDescriptor.length; i++) {
            System.out.println("\t" + propertyDescriptor[i].getName());
        }

        System.out.println("Events:");
        EventSetDescriptor eventSetDescriptor[] = beanInfo.getEventSetDescriptors();
        for (int i = 0; i < eventSetDescriptor.length; i++) {
            System.out.println("\t" + eventSetDescriptor[i].getName());
        }
    } catch (Exception e) {
        System.out.println("Exception caught. " + e);
    }
}

From source file:Example.java

public static void main(String args[]) {
    try {//w w w.  j  ava  2  s.com
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

        Connection con = DriverManager.getConnection("jdbc:odbc:inventory", "", "");
        Statement stmt = con.createStatement();
        ResultSet rs = stmt.executeQuery("SELECT * FROM inventory ORDER BY price");

        ResultSetMetaData rsmd = rs.getMetaData();
        int numberOfColumns = rsmd.getColumnCount();
        int rowCount = 1;
        while (rs.next()) {
            for (int i = 1; i <= numberOfColumns; i++) {
                System.out.print(rs.getString(i) + " ");
            }
            rowCount++;
        }
        stmt.close();
        con.close();
    } catch (Exception e) {
        System.out.println(e);
    }
}

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 dmd = connection.getMetaData();
    if (dmd.supportsTransactions()) {
        // Transactions are supported
    } else {//from w w  w .  java 2 s  . co m
        // Transactions are not supported
    }

}

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);
    CallableStatement cs = connection.prepareCall("{? = call myfunc}");

    // Register the type of the return value
    int intValue = 0;
    cs.registerOutParameter(1, intValue);

    // Execute and retrieve the returned value
    cs.execute();//from   w w w .  j a  v  a 2 s  . c  o m
    String retValue = cs.getString(1);

}

From source file:TestJDBCDriverInstallation_MySQL.java

public static void main(String[] args) {
    System.out.println("TestJDBCDriverInstallation_MySQL begin");
    try {/*from   w ww  .j  a v  a2s .  c o  m*/
        String className = "org.gjt.mm.mysql.Driver";
        Class driverObject = Class.forName(className);
        System.out.println("driverObject=" + driverObject);
        System.out.println("your installation of JDBC Driver OK.");
    } catch (Exception e) {
        // your installation of JDBC Driver Failed
        System.out.println("Failed: JDBC Driver Error: " + e.getMessage());
    }

    System.out.println("TestJDBCDriverInstallation_MySQL end");
}