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 driver = "com.mysql.jdbc.Driver";
    String user = "root";
    String pass = "root";

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

    Statement st = con.createStatement();
    ResultSet res = st.executeQuery("SELECT * FROM  emp");
    while (res.next()) {
        int i = res.getInt("ID");
        String s = res.getString("name");
        System.out.println(i + "\t\t" + s);
    }/*from   w ww .ja v  a2s  .c o  m*/
    con.close();
}

From source file:CreateDatabase.java

public static void main(String[] args) {
    Connection connection = null;
    Statement statement = null;//  w  ww. jav a2s . 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) {
            } // nothing we can do
        }
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
            } // nothing we can do
        }
    }
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    Class.forName("COM.cloudscape.core.JDBCDriver").newInstance();

    Connection conn = DriverManager.getConnection("jdbc:cloudscape:GAMETRADER");

    conn.setAutoCommit(false);//from   w  w  w.  j a v  a 2 s.  com

    Statement s = conn.createStatement();
    s.executeUpdate("CREATE TABLE MANUALS(GAMEID INT, MANUAL LONG VARCHAR)");
    conn.commit();

    File file = new File("manuals.xml");
    InputStream is = new FileInputStream(file);

    PreparedStatement ps = conn.prepareStatement("INSERT INTO MANUALS VALUES(?,?)");

    ps.setInt(1, 1285757);

    ps.setAsciiStream(2, is, (int) file.length());

    ps.execute();
    conn.commit();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String driver = "com.mysql.jdbc.Driver";
    String connection = "jdbc:mysql://localhost:3306/YourDBName";
    String user = "root";
    String password = "root";
    Class.forName(driver);
    Connection con = DriverManager.getConnection(connection, user, password);
    if (!con.isClosed()) {
        con.close();// www.j  a va2 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);
    String sql = "INSERT INTO mysql_all_table (col_binarystream) VALUES(?)";
    PreparedStatement pstmt = connection.prepareStatement(sql);

    byte[] buffer = "some data".getBytes();
    pstmt.setBytes(1, buffer);//from w w  w.ja v a2 s. com
    pstmt.executeUpdate();
    pstmt.close();

    Statement stmt = connection.createStatement();
    ResultSet resultSet = stmt.executeQuery("SELECT * FROM mysql_all_table");
    while (resultSet.next()) {
        byte[] bytes = resultSet.getBytes("col_binarystream");
    }
}

From source file:MainClass.java

public static void main(String[] args) {

    try {//ww  w .  j  av a2s. c  om
        Class.forName("COM.cloudscape.core.JDBCDriver").newInstance();

        Connection conn = DriverManager.getConnection("jdbc:cloudscape:GAMETRADER");

        Statement s = conn.createStatement();

        ResultSet rs = s.executeQuery("SELECT * FROM tableName");

        while (rs.next()) {
            int id = rs.getInt("GAMEID");
            Document manual = (Document) rs.getObject("MANUAL");
            XMLSerializer serialize = new XMLSerializer(System.out, null);
            serialize.serialize(manual);
        }

    } catch (Throwable e) {
        System.out.println("exception thrown");
        System.out.println(e);
    }
}

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);

    DatabaseMetaData dbm = con.getMetaData();
    String[] types = { "TABLE" };
    ResultSet rs = dbm.getTables(null, null, "%", types);
    System.out.println("Table name:");
    while (rs.next()) {
        String table = rs.getString("TABLE_NAME");
        System.out.println(table);
        con.close();/* w ww  . j  a v a  2  s. co m*/
    }

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String url = "jdbc:odbc:yourDBName";
    System.out.println("Attempting to connect to " + url);

    try {//from   w  w w.  jav a2 s  .  co  m
        System.out.println("Loading the driver...");
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

        System.out.println("Establishing a connection...");
        Connection connection = DriverManager.getConnection(url);

        System.out.println("Connect to " + connection.getCatalog() + " a success!");
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void main(String... args) {
    try {/*  w  ww .j  av  a 2  s .  c  o m*/
        Class<?> c = Class.forName("Main");
        Method[] allMethods = c.getDeclaredMethods();
        for (Method m : allMethods) {
            Class<?>[] pType = m.getParameterTypes();
            Type[] gpType = m.getGenericParameterTypes();
            for (int i = 0; i < pType.length; i++) {
                out.format(fmt, "ParameterType", pType[i]);
                out.format(fmt, "GenericParameterType", gpType[i]);
            }
        }
    } catch (ClassNotFoundException x) {
        x.printStackTrace();
    }
}

From source file:TestClassForNameNewInstanceApp.java

public static void main(String args[]) {

    try {/*from w w  w  .j  a va2 s. c om*/
        Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
    } catch (ClassNotFoundException e) {
        System.out.println("Oops! Can't find class oracle.jdbc.driver.OracleDriver");
        System.exit(1);
    } catch (IllegalAccessException e) {
        System.out.println("Uh Oh! You can't load oracle.jdbc.driver.OracleDriver");
        System.exit(2);
    } catch (InstantiationException e) {
        System.out.println("Geez! Can't instantiate oracle.jdbc.driver.OracleDriver");
        System.exit(3);
    }

    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) {
            }
    }
}