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 {
    System.out.println("Inserting values in  database table!");
    Connection con = null;/* w  w w .  j  a v a  2 s .  com*/
    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);/*ww  w . ja  v  a  2 s . c  om*/
    Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);

    DatabaseMetaData metadata = connection.getMetaData();

    int majorVersion = metadata.getDatabaseMajorVersion();
    System.out.println("majorVersion = " + majorVersion);

    int minorVersion = metadata.getDatabaseMinorVersion();
    System.out.println("minorVersion = " + minorVersion);

    String productName = metadata.getDatabaseProductName();
    System.out.println("productName = " + productName);

    String productVersion = metadata.getDatabaseProductVersion();
    System.out.println("productVersion = " + productVersion);
    connection.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = null;/*from  w  ww  .ja v a2 s . com*/
    Statement stmt = null;

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

    String sql = "DROP DATABASE STUDENTS";
    stmt.executeUpdate(sql);

    stmt.close();
    conn.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = null;/*  www.ja va  2s  . c o  m*/
    Statement stmt = null;

    Class.forName(JDBC_DRIVER);
    conn = DriverManager.getConnection(DB_URL, USER, PASS);
    stmt = conn.createStatement();

    String sql = "CREATE DATABASE STUDENTS";
    stmt.executeUpdate(sql);
    System.out.println("Database created successfully...");

    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);
    conn.setAutoCommit(false);/*from w  ww.  j a  v a 2s . c om*/

    String sql = "INSERT INTO documents (name, description, data) VALUES (?, ?, ?)";
    PreparedStatement stmt = conn.prepareStatement(sql);
    stmt.setString(1, "a.txt");
    stmt.setString(2, "b");

    File data = new File("C:\\a.txt");
    FileReader reader = new FileReader(data);
    stmt.setCharacterStream(3, reader, (int) data.length());
    stmt.execute();

    conn.commit();
    reader.close();
    conn.close();

}

From source file:Main.java

public static void main(String args[]) throws Exception {
    String URL = "jdbc:microsoft:sqlserver://yourServer:1433;databasename=pubs";
    String userName = "yourUser";
    String password = "yourPassword";

    Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
    Connection con = DriverManager.getConnection(URL, userName, password);
    CallableStatement callstmt = con
            .prepareCall("INSERT INTO myIdentTable (col2) VALUES (?);SELECT @@IDENTITY");
    callstmt.setString(1, "testInputBatch");
    callstmt.execute();/*from w  w w . j a  v  a  2  s.  c om*/

    int iUpdCount = callstmt.getUpdateCount();
    boolean bMoreResults = true;
    ResultSet rs = null;
    int myIdentVal = -1; // to store the @@IDENTITY

    while (bMoreResults || iUpdCount != -1) {
        rs = callstmt.getResultSet();
        if (rs != null) {
            rs.next();
            myIdentVal = rs.getInt(1);
        }
        bMoreResults = callstmt.getMoreResults();
        iUpdCount = callstmt.getUpdateCount();
    }
    callstmt.close();
    con.close();
}

From source file:Main.java

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

    boolean updatable = metadata.supportsResultSetConcurrency(ResultSet.TYPE_FORWARD_ONLY,
            ResultSet.CONCUR_UPDATABLE);

    System.out.println("Updatable ResultSet supported = " + updatable);
    connection.close();
}

From source file:Main.java

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

    Connection con;/*from w  ww. jav a 2  s.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 {
    Class.forName(DRIVER);/*from w w w .  ja  v a2s  . co  m*/
    Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);

    DatabaseMetaData metadata = connection.getMetaData();
    ResultSet resultSet = metadata.getColumns(null, null, "users", null);
    while (resultSet.next()) {
        String name = resultSet.getString("COLUMN_NAME");
        String type = resultSet.getString("TYPE_NAME");
        int size = resultSet.getInt("COLUMN_SIZE");

        System.out.println("Column name: [" + name + "]; type: [" + type + "]; size: [" + size + "]");
    }
    connection.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = null;/* w  w w. j a va2  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 = "DELETE FROM Person WHERE id = 1";
    stmt.executeUpdate(sql);
    stmt.close();
    conn.close();
}