Example usage for java.sql DriverManager getConnection

List of usage examples for java.sql DriverManager getConnection

Introduction

In this page you can find the example usage for java.sql DriverManager getConnection.

Prototype

private static Connection getConnection(String url, java.util.Properties info, Class<?> caller)
            throws SQLException 

Source Link

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    Class.forName(DRIVER);/*from w  ww. ja v  a2s  . c  o  m*/
    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 source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = null;// w w  w  . j  a  v  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 = "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 w ww.ja v  a 2s  .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();
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
    Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL", "yourName", "mypwd");
    Statement stmt = conn.createStatement();

    String streamingDataSql = "CREATE TABLE XML_Data (id INTEGER, Data LONG)";
    try {//from   w w w . j  a v a2  s .c  o  m
        stmt.executeUpdate("DROP TABLE XML_Data");
    } catch (SQLException se) {
        if (se.getErrorCode() == 942)
            System.out.println("Error dropping XML_Data table:" + se.getMessage());
    }
    stmt.executeUpdate(streamingDataSql);

    File f = new File("employee.xml");
    long fileLength = f.length();
    FileInputStream fis = new FileInputStream(f);
    PreparedStatement pstmt = conn.prepareStatement("INSERT INTO XML_Data VALUES (?,?)");
    pstmt.setInt(1, 100);
    pstmt.setAsciiStream(2, fis, (int) fileLength);
    pstmt.execute();
    fis.close();
    ResultSet rset = stmt.executeQuery("SELECT Data FROM XML_Data WHERE id=100");
    if (rset.next()) {
        InputStream xmlInputStream = rset.getAsciiStream(1);
        int c;
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        while ((c = xmlInputStream.read()) != -1)
            bos.write(c);
        System.out.println(bos.toString());
    }
    conn.close();
}

From source file:MainClass.java

public static void main(String[] args) {
    Connection connection = null;
    Statement statement = null;/*from  w w w . j  a  v a 2 s.  co m*/
    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 {
    Connection conn = null;//from w  ww.j  a  v a2  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 = "INSERT INTO Person VALUES (1, 'A', 'B', 18)";
    stmt.executeUpdate(sql);
    sql = "INSERT INTO Person VALUES (2, 'C', 'D', 25)";
    stmt.executeUpdate(sql);
    sql = "INSERT INTO Person VALUES (3, 'E', 'F', 30)";
    stmt.executeUpdate(sql);
    sql = "INSERT INTO Person VALUES(4, 'S', 'M', 28)";
    stmt.executeUpdate(sql);
    stmt.close();
    conn.close();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String driverName = "org.gjt.mm.mysql.Driver";
    Class.forName(driverName);//  w ww .  j  av a 2 s . c om

    String serverName = "localhost";
    String mydatabase = "mydatabase";
    String url = "jdbc:mysql://" + serverName + "/" + mydatabase;
    String username = "username";
    String password = "password";
    Connection connection = DriverManager.getConnection(url, username, password);

    Statement stmt = connection.createStatement();

    String filename = "c:/outfile.txt";
    String tablename = "mysql_2_table";
    stmt.executeUpdate("SELECT * INTO OUTFILE \"" + filename + "\" FROM " + tablename);

}

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 {//from  w  w w .  j a v a2s.co  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 final void main(String[] argv) throws Exception {
    Class.forName("oracle.jdbc.OracleDriver");
    Connection conn = DriverManager.getConnection("your_connection_string", "your_user_name", "your_password");
    Date nowDate = new Date();
    Timestamp nowTimestamp = new Timestamp(nowDate.getTime());
    PreparedStatement insertStmt = conn.prepareStatement(
            "INSERT INTO MyTable" + " (os_name, ts, ts_with_tz, ts_with_local_tz)" + " VALUES (?, ?, ?, ?)");
    insertStmt.setString(1, System.getProperty("os.name"));
    insertStmt.setTimestamp(2, nowTimestamp);
    insertStmt.setTimestamp(3, nowTimestamp);
    insertStmt.setTimestamp(4, nowTimestamp);
    insertStmt.executeUpdate();//from w w w .  jav  a 2s.com
    insertStmt.close();
    System.out.println("os_name, ts, ts_with_tz, ts_with_local_tz");
    PreparedStatement selectStmt = conn
            .prepareStatement("SELECT os_name, ts, ts_with_tz, ts_with_local_tz" + " FROM MyTable");
    ResultSet result = null;
    result = selectStmt.executeQuery();
    while (result.next()) {
        System.out.println(String.format("%s,%s,%s,%s", result.getString(1), result.getTimestamp(2).toString(),
                result.getTimestamp(3).toString(), result.getTimestamp(4).toString()));
    }
    result.close();
    selectStmt.close();
    conn.close();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Class.forName(DB_DRIVER);/*www  . j  a  v  a 2  s.  c o m*/
    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();
    dbConnection.close();
}