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  w w  .j  av  a2 s .c om
    Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);

    Statement statement = connection.createStatement();
    ResultSet resultSet = statement.executeQuery("SELECT id, username FROM users");

    ResultSetMetaData metadata = resultSet.getMetaData();
    if (metadata.isAutoIncrement(1)) {
        System.out.println("Column ID is an auto-increment column");
    }
}

From source file:Main.java

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

    String driverName = "com.jnetdirect.jsql.JSQLDriver";
    Class.forName(driverName);/*from  w  w  w  . j a v a  2  s. c o m*/

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

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Class.forName(DB_DRIVER);/*  www.  j  a v  a 2 s.  co m*/
    Connection dbConnection = DriverManager.getConnection(DB_CONNECTION, DB_USER, DB_PASSWORD);

    CallableStatement callableStatement = null;
    String insertStoreProc = "{call insertPERSON(?,?,?,?)}";

    java.util.Date today = new java.util.Date();
    callableStatement = dbConnection.prepareCall(insertStoreProc);
    callableStatement.setInt(1, 1000);
    callableStatement.setString(2, "name");
    callableStatement.setString(3, "system");
    callableStatement.setDate(4, new java.sql.Date(today.getTime()));

    callableStatement.executeUpdate();
    System.out.println("Record is inserted into PERSON table!");
    callableStatement.close();
    dbConnection.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);

    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 . j  a  v  a  2s . c  om*/
        }
        fos.close();
    }
    conn.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    try {//  w ww .ja v a 2 s  .c  o m
        String url = "jdbc:odbc:yourdatabasename";
        String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
        String user = "guest";
        String password = "guest";

        Class.forName(driver);
        Connection connection = DriverManager.getConnection(url, user, password);

        Statement stmt = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
                ResultSet.CONCUR_READ_ONLY);

        String sqlQuery = "SELECT EMPNO, EName, Job, MGR, HIREDATE FROM EMP";

        ResultSet rs = stmt.executeQuery(sqlQuery);

        int rowSize = 0;
        while (rs.next()) {
            rowSize++;
        }

        System.out.println("Number of Rows in ResultSet is: " + rowSize);
        if (rowSize == 0) {
            System.out.println("Since there are no rows, exiting...");
            System.exit(0);
        }

        int cursorPosition = Math.round(rowSize / 2);

        System.out.println("Moving to position: " + cursorPosition);
        rs.absolute(cursorPosition);
        System.out.println("Name: " + rs.getString(2));

        rs.relative(-1);

        cursorPosition = rs.getRow();
        System.out.println("Moving to position: " + cursorPosition);
        System.out.println("Name: " + rs.getString(2));

        System.out.println("Moving to the first row");
        while (!rs.isFirst()) {
            rs.previous();
        }
        System.out.println("Name: " + rs.getString(2));
        connection.close();
    } catch (Exception e) {
        System.err.println(e);
    }
}

From source file:Main.java

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

    Class.forName(DRIVER);/*  w ww  .  j a  va 2s . c  om*/
    Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);

    Statement statement = connection.createStatement();
    String query = "SELECT stock_id, name, price FROM stocks";
    ResultSet resultSet = statement.executeQuery(query);

    ResultSetMetaData metadata = resultSet.getMetaData();
    int precision = metadata.getPrecision(3);
    int scale = metadata.getScale(3);

    System.out.println("Precision: " + precision);
    System.out.println("Scale    : " + scale);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Class.forName(DRIVER);//from w  ww.ja  v  a  2  s .c om
    Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);

    Statement statement = connection.createStatement();
    String query = "SELECT a.id, a.username, a.country_id, b.country_name " + "FROM users a "
            + "LEFT JOIN countries b ON a.country_id = b.id";
    ResultSet resultSet = statement.executeQuery(query);

    ResultSetMetaData metadata = resultSet.getMetaData();

    String tableName = metadata.getTableName(1);
    System.out.println("Table name of column 'id' = " + tableName);

    tableName = metadata.getTableName(4);
    System.out.println("Table name of column 'country name' = " + tableName);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Class.forName(DRIVER);//from  w  ww  .j  a  v a  2 s. c  o m
    Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);

    Statement statement = connection.createStatement();
    ResultSet resultSet = statement.executeQuery("SELECT id, username FROM users");
    ResultSetMetaData metadata = resultSet.getMetaData();
    int nullability = metadata.isNullable(1);

    if (nullability == ResultSetMetaData.columnNullable) {
        System.out.println("Columns ID can have a null value");
    } else if (nullability == ResultSetMetaData.columnNoNulls) {
        System.out.println("Columns ID does not allowed to have a null value");
    } else if (nullability == ResultSetMetaData.columnNullableUnknown) {
        System.out.println("Nullability unknown");
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String url = "jdbc:mysql://localhost/testdb";
    String username = "root";
    String password = "";
    Class.forName("com.mysql.jdbc.Driver");
    Connection conn = null;/*from   w w w  .  jav a  2  s.c  o m*/
    try {
        conn = DriverManager.getConnection(url, username, password);
        conn.setAutoCommit(false);

        Statement st = conn.createStatement();
        st.execute("INSERT INTO orders (username, order_date) VALUES ('java', '2007-12-13')",
                Statement.RETURN_GENERATED_KEYS);

        ResultSet keys = st.getGeneratedKeys();
        int id = 1;
        while (keys.next()) {
            id = keys.getInt(1);
        }
        PreparedStatement pst = conn.prepareStatement(
                "INSERT INTO order_details (order_id, product_id, quantity, price) VALUES (?, ?, ?, ?)");
        pst.setInt(1, id);
        pst.setString(2, "1");
        pst.setInt(3, 10);
        pst.setDouble(4, 100);
        pst.execute();

        conn.commit();
        System.out.println("Transaction commit...");
    } catch (SQLException e) {
        if (conn != null) {
            conn.rollback();
            System.out.println("Connection rollback...");
        }
        e.printStackTrace();
    } finally {
        if (conn != null && !conn.isClosed()) {
            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);/*from  ww  w . j  av  a  2 s.c  om*/
        }
        writer.close();
    }
    conn.close();
}