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 = "org.gjt.mm.mysql.Driver";
    Class.forName(driverName);

    String url = "jdbc:mysql://a/b";
    Driver driver = DriverManager.getDriver(url);

    DriverPropertyInfo[] info = driver.getPropertyInfo(url, null);
    for (int i = 0; i < info.length; i++) {
        String name = info[i].name;
        boolean isRequired = info[i].required;
        String value = info[i].value;
        String desc = info[i].description;
        String[] choices = info[i].choices;
    }//from   www . j  ava  2s.  co m
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String hostname = "", dbname = "", username = "", password = "";
    Class.forName("org.postgresql.Driver");
    Connection connection = DriverManager.getConnection("jdbc:postgresql://" + hostname + "/" + dbname,
            username, password);//from  www . ja  v a2 s .co  m
    ResultSet rs = connection.createStatement().executeQuery("select version() as version");
    while (rs.next()) {
        System.out.println(rs.getString("version"));
    }
}

From source file:Main.java

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

    int records = 0;

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

    String sql = "SELECT COUNT(*) FROM mytable";
    PreparedStatement prest = con.prepareStatement(sql);
    ResultSet rs = prest.executeQuery();
    while (rs.next()) {
        records = rs.getInt(1);//from w w w  .  j av a  2 s  .co m
    }
    System.out.println("Number of records: " + records);
    con.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);
    Statement stmt = connection.createStatement();
    String sql = "UPDATE my_table SET col_string='a new string' WHERE col_string = 'a string'";
    int updateCount = stmt.executeUpdate(sql);

}

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();
    String sql = "INSERT INTO my_table (col_string) VALUES('a string')";

    stmt.executeUpdate(sql);//from  w  ww  . jav a  2  s .  co m
}

From source file:XMLDBDOM.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 ww .  j a v  a  2 s .  c om*/

    Statement s = conn.createStatement();
    s.executeUpdate("CREATE TABLE XMLData(GAMEID INT, MANUAL SERIALIZE(org.w3c.dom.Document))");
    conn.commit();

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

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

    ps.setInt(1, 1285757);

    DOMParser parser = new DOMParser();
    parser.parse("XMLData.xml");
    Document manual = parser.getDocument();
    ps.setObject(2, manual);

    ps.execute();

    conn.commit();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Class.forName("com.mysql.jdbc.Driver");
    Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/testdb", "root", "");

    Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
            ResultSet.CONCUR_UPDATABLE);

    String query = "SELECT id, code, name, quantity, price FROM products";
    ResultSet uprs = statement.executeQuery(query);

    while (uprs.next()) {
        System.out.println(uprs.getString("id") + ":" + uprs.getString("code") + ":" + uprs.getString("name")
                + ":" + uprs.getInt("quantity") + ":" + uprs.getDouble("price"));
    }/*  w w  w . j  av a 2s .co  m*/
    uprs.first();
    uprs.updateString("name", "Java");
    uprs.updateRow();
    uprs.next();
    uprs.deleteRow();

    uprs.moveToInsertRow();
    uprs.updateString("code", "1");
    uprs.updateString("name", "Data Structures");
    uprs.updateInt("quantity", 1);
    uprs.updateDouble("price", 5.99);
    uprs.insertRow();

    uprs.beforeFirst();
    while (uprs.next()) {
        System.out.println(uprs.getString("id") + "\t" + uprs.getString("code") + "\t" + uprs.getString("name")
                + "\t" + uprs.getInt("quantity") + "\t" + uprs.getDouble("price"));
    }
    connection.close();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String driverName = "oracle.jdbc.driver.OracleDriver";
    Class.forName(driverName);

    String serverName = "127.0.0.1";
    String portNumber = "1521";
    String sid = "mydatabase";
    String url = "jdbc:oracle:thin:@" + serverName + ":" + portNumber + ":" + sid;
    String username = "username";
    String password = "password";
    Connection connection = DriverManager.getConnection(url, username, password);
    Statement stmt = connection.createStatement();

    String procedure = "CREATE OR REPLACE PROCEDURE myprocout(x OUT VARCHAR) IS BEGIN "
            + "INSERT INTO oracle_table VALUES('string 2'); x := 'outvalue'; END;";
    stmt.executeUpdate(procedure);/*from  ww  w.j av  a2  s .  c o m*/
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String user = "root";
    String pass = "root";

    Class.forName("com.mysql.jdbc.Driver").newInstance();
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctutorial", user, pass);
    Statement st = con.createStatement();
    String table = "CREATE TABLE java_DataTypes2(typ_boolean BOOL, "
            + "typ_byte          TINYINT, typ_short         SMALLINT, "
            + "typ_int           INTEGER, typ_long          BIGINT, "
            + "typ_float         FLOAT,   typ_double        DOUBLE PRECISION, "
            + "typ_bigdecimal    DECIMAL(13,0), typ_string        VARCHAR(254), "
            + "typ_date          DATE,    typ_time          TIME, " + "typ_timestamp     TIMESTAMP, "
            + "typ_asciistream   TEXT,    typ_binarystream  LONGBLOB, " + "typ_blob          BLOB)";

    st.executeUpdate(table);/*from w  w  w . j  ava  2 s  .  co  m*/
    con.close();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String driverName = "oracle.jdbc.driver.OracleDriver";
    Class.forName(driverName);

    String serverName = "127.0.0.1";
    String portNumber = "1521";
    String sid = "mydatabase";
    String url = "jdbc:oracle:thin:@" + serverName + ":" + portNumber + ":" + sid;
    String username = "username";
    String password = "password";
    Connection connection = DriverManager.getConnection(url, username, password);

    Statement stmt = connection.createStatement();
    String function = "CREATE OR REPLACE FUNCTION myfunc RETURN VARCHAR IS BEGIN RETURN 'a returned string'; END;";
    stmt.executeUpdate(function);//from w  ww  . ja v a 2  s.  co m

}