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;
    Statement statement = null;//from ww  w. j  a  v  a  2 s .  com
    try {
        Class.forName("org.hsqldb.jdbcDriver").newInstance();
        String url = "jdbc:hsqldb:hsqldb\\demoDatabase";
        connection = DriverManager.getConnection(url, "username", "password");
        connection.setAutoCommit(false);

        statement = connection.createStatement();

        String update1 = "UPDATE employees SET email = 'a@b.com' WHERE email = 'a@a.com'";
        statement.executeUpdate(update1);
        Savepoint savepoint1 = connection.setSavepoint("savepoint1");

        String update2 = "UPDATE employees SET email = 'b@b.com' WHERE email = 'b@c.com'";
        statement.executeUpdate(update2);
        Savepoint savepoint2 = connection.setSavepoint("savepoint2");

        String update3 = "UPDATE employees SET email = 'c@c.com' WHERE email = 'c@d.com'";
        statement.executeUpdate(update3);
        Savepoint savepoint3 = connection.setSavepoint("savepoint3");

        String update4 = "UPDATE employees SET email = 'd@d.com' WHERE email = 'd@e.com'";
        statement.executeUpdate(update4);
        Savepoint savepoint4 = connection.setSavepoint("savepoint4");

        String update5 = "UPDATE employees SET email = 'e@e.com' WHERE email = 'e@f.com'";
        statement.executeUpdate(update5);
        Savepoint savepoint5 = connection.setSavepoint("savepoint5");

        connection.rollback(savepoint3);
        connection.commit();
    } 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[] args) throws Exception {
    Class.forName("com.mysql.jdbc.Driver");
    Connection conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);

    String insert = "INSERT INTO orders (username, order_date) VALUES ('foobar', '2007-12-13')";
    Statement stmt = conn.createStatement();

    stmt.executeUpdate(insert, Statement.RETURN_GENERATED_KEYS);

    ResultSet keys = stmt.getGeneratedKeys();
    int lastKey = 1;
    while (keys.next()) {
        lastKey = keys.getInt(1);/*from  w  w  w .j a va 2  s  .  co  m*/
    }
    System.out.println("Last Key: " + lastKey);
    conn.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);
    DatabaseMetaData dbmd = connection.getMetaData();
    ResultSet resultSet = dbmd.getTypeInfo();

    while (resultSet.next()) {
        String typeName = resultSet.getString("TYPE_NAME");

        short dataType = resultSet.getShort("DATA_TYPE");
        getJdbcTypeName(dataType);//from w  w w . java2 s  . c  o  m
    }
}

From source file:TestOCIApp.java

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

    Class.forName("oracle.jdbc.driver.OracleDriver");
    // or you can use:
    //  DriverManager.registerDriver(
    //   new oracle.jdbc.driver.OracleDriver());
    Connection conn = DriverManager.getConnection("jdbc:oracle:oci8:@dssnt01", "scott", "tiger");

    Statement stmt = conn.createStatement();
    ResultSet rset = stmt.executeQuery("select 'Hello OCI driver tester '||USER||'!' result from dual");
    while (rset.next())
        System.out.println(rset.getString(1));
    rset.close();/*w w  w.  j  a  va2s. c  o m*/
    stmt.close();
    conn.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String url = "jdbc:mysql://localhost:3306/";
    String dbName = "jdbc4";
    String driver = "com.mysql.jdbc.Driver";
    String userName = "root";
    String password = "root";

    Class.forName(driver).newInstance();

    Connection conn = DriverManager.getConnection(url + dbName, userName, password);
    Statement st = conn.createStatement();

    int rows = st.executeUpdate("INSERT INTO Copyemployee SELECT * FROM employee");
    if (rows == 0) {
        System.out.println("Don't add any row!");
    } else {//from  w w  w .  j  a v  a2 s  . c om
        System.out.println(rows + " row(s)affected.");
        conn.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);
    DatabaseMetaData dbmd = connection.getMetaData();
    ResultSet resultSet = dbmd.getTypeInfo();

    while (resultSet.next()) {
        // Get the database-specific type name
        String typeName = resultSet.getString("TYPE_NAME");

        // Get the java.sql.Types type to which this database-specific type is
        // mapped
        short dataType = resultSet.getShort("DATA_TYPE");
        getJdbcTypeName(dataType);/*from  www.ja  v a 2  s  . c  o  m*/
    }
}

From source file:Main.java

public static void main(String[] args) {
    try {//from   www  . j a  v  a2  s .c o m
        Class.forName(driver);
        Connection conn = DriverManager.getConnection(url, username, password);
        System.out.println(conn.getMetaData().getDatabaseProductName());
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    System.out.println("Count number of rows in a specific table!");
    Connection con = null;/*from w  w w  .j  a  v a 2  s .  c  om*/
    int count = 0;
    Class.forName("com.mysql.jdbc.Driver");
    con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctutorial", "root", "root");

    Statement st = con.createStatement();
    BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
    ResultSet res = st.executeQuery("SELECT COUNT(*) FROM EMP");
    while (res.next()) {
        count = res.getInt(1);
    }
    System.out.println("Number of row:" + count);
}

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);
    conn.setAutoCommit(false);/*  w ww.j  ava2 s .  c  o  m*/

    String sql = "INSERT INTO pictures (name, description, image) VALUES (?, ?, ?)";
    PreparedStatement stmt = conn.prepareStatement(sql);
    stmt.setString(1, "java.gif");
    stmt.setString(2, "Java Official Logo");

    File image = new File("D:\\a.gif");
    FileInputStream fis = new FileInputStream(image);
    stmt.setBinaryStream(3, fis, (int) image.length());
    stmt.execute();

    conn.commit();
    fis.close();
    conn.close();
}

From source file:ShowClass.java

public static void main(String[] args) throws ClassNotFoundException {
    Class aClass = Class.forName("javax.swing.JComponent");
    if (aClass.isInterface()) {
        System.out.print(Modifier.toString(aClass.getModifiers()) + " " + typeName(aClass));
    } else if (aClass.getSuperclass() != null) {
        System.out.print(Modifier.toString(aClass.getModifiers()) + " class " + typeName(aClass) + " extends "
                + typeName(aClass.getSuperclass()));
    } else {//from w w  w  . j  a v a  2s.  c  o m
        System.out.print(Modifier.toString(aClass.getModifiers()) + " class " + typeName(aClass));
    }

    Class[] interfaces = aClass.getInterfaces();
    if ((interfaces != null) && (interfaces.length > 0)) {
        if (aClass.isInterface())
            System.out.print(" extends ");
        else
            System.out.print(" implements ");
        for (int i = 0; i < interfaces.length; i++) {
            if (i > 0)
                System.out.print(", ");
            System.out.print(typeName(interfaces[i]));
        }
    }

    System.out.println(" {");

    Constructor[] constructors = aClass.getDeclaredConstructors();
    for (int i = 0; i < constructors.length; i++)
        printMethodOrConstructor(constructors[i]);

    Field[] fields = aClass.getDeclaredFields();
    for (int i = 0; i < fields.length; i++)
        printField(fields[i]);

    Method[] methods = aClass.getDeclaredMethods();
    for (int i = 0; i < methods.length; i++)
        printMethodOrConstructor(methods[i]);

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