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("oracle.jdbc.driver.OracleDriver");
    Connection conn = DriverManager.getConnection(url, username, password);

    CallableStatement stmt = conn.prepareCall("{call CREATE_USERS (?, ?, ?, ?, ?, ?)}");

    stmt.setString(1, "a");
    stmt.setString(2, "b");
    stmt.setString(3, "c");
    stmt.setString(4, "d");
    stmt.setString(5, "e");
    stmt.setString(6, "f");
    stmt.execute();//  ww  w.  ja  va 2 s  . c  om
    conn.close();
}

From source file:Main.java

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

    Class.forName("com.mysql.jdbc.Driver").newInstance();
    Connection conn = DriverManager.getConnection(url + dbName, userName, password);
    Statement st = conn.createStatement();
    int rows = st.executeUpdate("INSERT INTO myTable SELECT * FROM jdbc4.Copyemployee");
    if (rows == 0) {
        System.out.println("Don't add any row!");
    } else {//from   ww  w . ja va  2s .  c  om
        System.out.println(rows + " row(s)affected.");
        conn.close();
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    File file = new File("myimage.gif");
    FileInputStream fis = new FileInputStream(file);
    Class.forName("oracle.jdbc.driver.OracleDriver");
    Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@//server.local:1521/prod", "scott",
            "tiger");
    conn.setAutoCommit(false);//from w  w w . j  a  v a2  s.  com
    PreparedStatement ps = conn.prepareStatement("insert into images values (?,?)");
    ps.setString(1, file.getName());
    ps.setBinaryStream(2, fis, (int) file.length());
    ps.executeUpdate();
    ps.close();
    fis.close();

}

From source file:JDBCVersionApp.java

public static void main(String args[]) throws ClassNotFoundException, SQLException {

    Class.forName("oracle.jdbc.driver.OracleDriver");
    Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@dssnt01:1521:dssora01", "scott", "tiger");
    DatabaseMetaData meta = conn.getMetaData();
    System.out.println("You're using version: " + meta.getDriverVersion() + " of the Oracle JDBC driver.");
    conn.close();/*w ww.  jav a 2 s.c  o  m*/
}

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 rs = stmt.executeQuery("SELECT col_blob FROM mysql_all_table");

    if (rs.next()) {
        Blob blob = rs.getBlob("col_blob");
        long blobLength = blob.length();

        int pos = 1; // position is 1-based
        int len = 10;
        byte[] bytes = blob.getBytes(pos, len);

        InputStream is = blob.getBinaryStream();
        int b = is.read();
    }//from   w w  w.  jav a  2  s .  c om
}

From source file:JdbcConnect.java

public static void main(String[] args) throws Exception {
    Connection conn1 = null;/*  w  w  w.ja  va 2  s. c  o  m*/
    Connection conn2 = null;
    Connection conn3 = null;

    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
    String jdbcUrl = "jdbc:odbc:authors";

    String user = "yourName";
    String pwd = "mypwd";

    conn1 = DriverManager.getConnection(jdbcUrl);
    if (conn1 != null) {
        System.out.println("Connection 1 successful!");
    }
    Properties prop = new Properties();
    prop.put("user", user);
    prop.put("password", pwd);

    conn2 = DriverManager.getConnection(jdbcUrl, prop);
    if (conn2 != null) {
        System.out.println("Connection 2 successful!");
    }
    conn3 = DriverManager.getConnection(jdbcUrl, user, pwd);
    if (conn3 != null) {
        System.out.println("Connection 3 successful!");
    }
    conn1.close();
    conn2.close();
    conn3.close();
    if (conn1.isClosed()) {
        System.out.println("Connection 1 is closed");
    }

    if (conn2.isClosed()) {
        System.out.println("Connection 2 is closed");
    }
    if (conn3.isClosed()) {
        System.out.println("Connection 3 is closed");
    }

    conn1.close();
    conn2.close();
    conn3.close();

}

From source file:X.java

public static void main(String[] args) {
    try {/*  w  ww. java2 s .  c o  m*/
        Class<?> clazz = Class.forName("X");
        X x = (X) clazz.newInstance();
        Class[] argTypes = { String.class };
        Method method = clazz.getMethod("objectMethod", argTypes);

        System.out.println(method.getDeclaringClass());

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

From source file:X.java

public static void main(String[] args) {
    try {/*from  ww w.jav a  2s.c o  m*/
        Class<?> clazz = Class.forName("X");
        X x = (X) clazz.newInstance();
        Class[] argTypes = { String.class };
        Method method = clazz.getMethod("objectMethod", argTypes);

        System.out.println(method.getDefaultValue());

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

From source file:X.java

public static void main(String[] args) {
    try {/*from  w w  w .  j  av a 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);
        Object[] data = { "Hello" };
        method.invoke(x, data); // Output: Instance method: Hello
        method = clazz.getMethod("classMethod", (Class<?>[]) null);
        method.invoke(null, (Object[]) null); // Output: Class method
    } catch (Exception e) {
        System.err.println(e);
    }
}

From source file:GetIt.java

public static void main(String args[]) throws Exception {
    JMenu it = new JMenu();
    String classID = it.getUIClassID();
    System.out.println(classID);/*w  w w.  j av a 2 s  .  co m*/
    String className = (String) UIManager.get(classID);
    System.out.println(className);
    Class.forName(className);
    System.out.println(System.getProperty("swing.defaultlaf"));
    System.out.println(System.getProperty("swing.auxiliarylaf"));
    System.out.println(System.getProperty("swing.plaf.multiplexinglaf"));
    System.out.println(System.getProperty("swing.installedlafs"));
}