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 {
    System.out.println("Inserting values in  database table!");
    Connection con = null;//  w  ww  . j  ava 2  s .c  o m
    String url = "jdbc:jtds:sqlserver://127.0.0.1:1433/Example";
    String username = "sa";
    String password = "";
    String driver = "net.sourceforge.jtds.jdbc.Driver";
    Class.forName(driver);
    con = DriverManager.getConnection(url, username, password);
    Statement st = con.createStatement();
    int val = st.executeUpdate("insert into dbo.Company " + "values('abc', 2)");
    System.out.println("1 row affected");
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Class.forName(DRIVER);
    Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
    DatabaseMetaData metadata = connection.getMetaData();

    boolean supportForwardOnly = metadata.supportsResultSetType(ResultSet.TYPE_FORWARD_ONLY);
    System.out.println("supportForwardOnly = " + supportForwardOnly);

    boolean supportScrollInsensitive = metadata.supportsResultSetType(ResultSet.TYPE_SCROLL_INSENSITIVE);
    System.out.println("supportScrollInsensitive = " + supportScrollInsensitive);

    boolean supportScrollSensitive = metadata.supportsResultSetType(ResultSet.TYPE_SCROLL_SENSITIVE);
    System.out.println("supportScrollSensitive = " + supportScrollSensitive);
    connection.close();//from   w ww .j ava  2  s. com
}

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

    String sql = "SELECT name, description, image FROM pictures ";
    PreparedStatement stmt = conn.prepareStatement(sql);
    ResultSet resultSet = stmt.executeQuery();
    while (resultSet.next()) {
        String name = resultSet.getString(1);
        String description = resultSet.getString(2);
        File image = new File("D:\\java.gif");
        FileOutputStream fos = new FileOutputStream(image);

        byte[] buffer = new byte[1];
        InputStream is = resultSet.getBinaryStream(3);
        while (is.read(buffer) > 0) {
            fos.write(buffer);// w  w w.jav  a 2 s .c om
        }
        fos.close();
    }
    conn.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String url = "jdbc:odbc:technical_library";
    String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
    String theStatement = "SELECT authid, lastname, firstname, email FROM authors ORDER BY authid";

    try {//ww  w  .  ja v  a2  s  .  c  o m
        Class.forName(driver);
        Connection connection = DriverManager.getConnection(url, "guest", "guest");
        Statement queryAuthors = connection.createStatement();
        ResultSet results = queryAuthors.executeQuery(theStatement);

        String lastname, firstname, email;
        int id;
        while (results.next()) {
            id = results.getInt(1);
            lastname = results.getString(2);
            firstname = results.getString(3);
            email = results.getString(4);

            if (results.wasNull()) {
                email = "no email";
            }
            System.out.println(Integer.toString(id) + ", " + lastname.trim() + ", " + firstname.trim() + ", "
                    + email.trim());
        }
        queryAuthors.close();
    } catch (Exception e) {
        System.err.println(e);
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Class.forName(DB_DRIVER);
    Connection dbConnection = DriverManager.getConnection(DB_CONNECTION, DB_USER, DB_PASSWORD);
    PreparedStatement preparedStatement = null;
    java.util.Date today = new java.util.Date();
    String insertTableSQL = "INSERT INTO DBUSER" + "(USER_ID, USERNAME, CREATED_BY, CREATED_DATE) VALUES"
            + "(?,?,?,?)";
    preparedStatement = dbConnection.prepareStatement(insertTableSQL);
    preparedStatement.setTimestamp(4, new java.sql.Timestamp(today.getTime()));

    dbConnection.commit();/*from w ww . ja  v a  2s  .  c  o  m*/
    dbConnection.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String driver = "sun.jdbc.odbc.JdbcOdbcDriver";

    Connection con;//from   w  w w. ja  v  a2s . c o m
    Statement stmt;
    ResultSet uprs;

    try {
        Class.forName(driver);
        con = DriverManager.getConnection("jdbc:odbc:RainForestDSN", "student", "student");
        stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
        uprs = stmt.executeQuery("SELECT * FROM Records");

        // Check the column count
        ResultSetMetaData md = uprs.getMetaData();
        System.out.println("Resultset has " + md.getColumnCount() + " cols.");

        int rowNum = uprs.getRow();
        System.out.println("row1 " + rowNum);
        uprs.absolute(1);
        rowNum = uprs.getRow();
        System.out.println("row2 " + rowNum);
        uprs.next();
        uprs.moveToInsertRow();
        uprs.updateInt(1, 150);
        uprs.updateString(2, "Madonna");
        uprs.updateString(3, "Dummy");
        uprs.updateString(4, "Jazz");
        uprs.updateString(5, "Image");
        uprs.updateInt(6, 5);
        uprs.updateDouble(7, 5);
        uprs.updateInt(8, 15);
        uprs.insertRow();
        uprs.close();
        stmt.close();
        con.close();
    } catch (SQLException ex) {
        System.err.println("SQLException: " + ex.getMessage());
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = null;/*from  w  w  w  .  jav a  2 s .  com*/
    Statement stmt = null;

    Class.forName(JDBC_DRIVER);
    conn = DriverManager.getConnection(DB_URL, USER, PASS);
    System.out.println("Deleting database...");
    stmt = conn.createStatement();

    conn = DriverManager.getConnection(DB_URL, USER, PASS);

    stmt = conn.createStatement();

    String sql = "DELETE FROM Person WHERE id = 1";
    stmt.executeUpdate(sql);
    stmt.close();
    conn.close();
}

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);
    PreparedStatement stmt = conn.prepareStatement("SELECT name, description, data FROM documents ");
    ResultSet resultSet = stmt.executeQuery();
    while (resultSet.next()) {
        String name = resultSet.getString(1);
        String description = resultSet.getString(2);
        File data = new File("C:\\a.txt");
        Reader reader = resultSet.getCharacterStream(3);
        FileWriter writer = new FileWriter(data);
        char[] buffer = new char[1];
        while (reader.read(buffer) > 0) {
            writer.write(buffer);// w  w  w  .  j  a  v  a2s . c o m
        }
        writer.close();
    }
    conn.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = null;//  ww w.  jav a2  s  . c om
    Statement stmt = null;

    Class.forName(JDBC_DRIVER);
    conn = DriverManager.getConnection(DB_URL, USER, PASS);
    System.out.println("Deleting database...");
    stmt = conn.createStatement();

    conn = DriverManager.getConnection(DB_URL, USER, PASS);

    stmt = conn.createStatement();

    String sql = "DROP TABLE Person ";

    stmt.executeUpdate(sql);

    System.out.println("Created table in given database...");
    stmt.close();
    conn.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = null;//from  ww  w.  ja va 2  s  . co m
    Statement stmt = null;

    Class.forName(JDBC_DRIVER);
    conn = DriverManager.getConnection(DB_URL, USER, PASS);
    System.out.println("Deleting database...");
    stmt = conn.createStatement();

    conn = DriverManager.getConnection(DB_URL, USER, PASS);

    stmt = conn.createStatement();

    String sql = "CREATE TABLE Person" + "(id INTEGER not NULL, " + " firstName VARCHAR(50), "
            + " lastName VARCHAR(50), " + " age INTEGER, " + " PRIMARY KEY ( id ))";

    stmt.executeUpdate(sql);
    System.out.println("Created table in given database...");
    stmt.close();
    conn.close();
}