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

@CallerSensitive
public static Connection getConnection(String url) throws SQLException 

Source Link

Document

Attempts to establish a connection to the given database URL.

Usage

From source file:PreparedStmt.java

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

    String query = "SELECT * FROM Stock WHERE Item_Number = ?";
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection con = DriverManager.getConnection("jdbc:odbc:Inventory");
    PreparedStatement pstmt = con.prepareStatement(query);
    pstmt.setInt(1, 2);/*  w  ww .  j  ava2  s .  co m*/
    ResultSet rs = pstmt.executeQuery();
    while (rs.next()) {
        String name = rs.getString("Name");
        String desc = rs.getString("Description");
        int qty = rs.getInt("Qty");
        float cost = rs.getFloat("Cost");
        System.out.println(name + ", " + desc + "\t: " + qty + "\t@ $" + cost);
    }
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    Connection conn = null;// w w  w. j  ava 2  s . com
    Statement s = null;
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    conn = DriverManager.getConnection("jdbc:odbc:Driver={SQL Server};" + "Server=.\\SQLEXPRESS;"
            + "Trusted_Connection=yes;" + "Database=myDb");
    s = conn.createStatement();
    s.executeQuery("SELECT * FROM dbo.SalesSummary WHERE 0 = 1");
    ResultSet rs = s.getResultSet();
    ResultSetMetaData rsmd = rs.getMetaData();
    for (int i = 1; i <= rsmd.getColumnCount(); i++) {
        System.out.println(String.format("-- Column %d --", i));
        System.out.println(String.format("Column name: %s", rsmd.getColumnName(i)));
        System.out.println(String.format("Database-specific type name: %s", rsmd.getColumnTypeName(i)));
        System.out.println(String.format("Column size (DisplaySize): %d", rsmd.getColumnDisplaySize(i)));
        System.out.println(String.format("java.sql.Type of column: %d", rsmd.getColumnType(i)));
        System.out.println();
    }
    s.close();
    conn.close();
}

From source file:DataUpdater.java

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

    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    DriverManager.registerDriver(new JdbcOdbcDriver());
    String url = "jdbc:odbc:Contacts";

    Connection con = DriverManager.getConnection(url);
    Statement stmt = con.createStatement();
    String SQLCommand = "UPDATE CONTACT_INFO " + "SET STREET = '58 Broadway', ZIP = '10008' "
            + "WHERE First_Name = 'Michael' AND " + "Last_Name ='Corleone';";

    stmt.execute(SQLCommand);//ww w .j  av  a2 s  .  c  o  m
    con.close();
}

From source file:MainClass.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);//w w  w  .  jav  a 2  s.com

    Statement s = conn.createStatement();
    s.executeUpdate("CREATE TABLE MANUALS(GAMEID INT, MANUAL LONG VARCHAR)");
    conn.commit();

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

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

    ps.setInt(1, 1285757);

    ps.setAsciiStream(2, is, (int) file.length());

    ps.execute();
    conn.commit();
}

From source file:MainClass.java

public static void main(String[] args) {

    try {//from   w  ww.  ja v  a 2 s  . c  om
        Class.forName("COM.cloudscape.core.JDBCDriver").newInstance();

        Connection conn = DriverManager.getConnection("jdbc:cloudscape:GAMETRADER");

        Statement s = conn.createStatement();

        ResultSet rs = s.executeQuery("SELECT * FROM tableName");

        while (rs.next()) {
            int id = rs.getInt("GAMEID");
            Document manual = (Document) rs.getObject("MANUAL");
            XMLSerializer serialize = new XMLSerializer(System.out, null);
            serialize.serialize(manual);
        }

    } catch (Throwable e) {
        System.out.println("exception thrown");
        System.out.println(e);
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection con = DriverManager.getConnection("jdbc:h2:mem:");
    Statement s = con.createStatement();
    s.execute("CREATE TABLE Table1 (Column1 CLOB)");

    InputStream is = new FileInputStream("data.txt");
    Reader rdr = new InputStreamReader(is, StandardCharsets.ISO_8859_1);
    PreparedStatement ps = con.prepareStatement("INSERT INTO Table1 (Column1) VALUES (?)");
    ps.setCharacterStream(1, rdr);/*from ww w  .j  ava2 s  . c  o  m*/
    ps.executeUpdate();

    ResultSet rs = s.executeQuery("SELECT Column1 FROM Table1");
    int rowNumber = 0;
    while (rs.next()) {
        String str = rs.getString("Column1");
        System.out.println(String.format("Row %d: CLOB is %d character(s) long.", ++rowNumber, str.length()));
    }
    rs.close();
    con.close();
}

From source file:PrintIndexedResultSet.java

public static void main(String args[]) throws Exception {
    String query = "SELECT STATE, COUNT(STATE) FROM MEMBER_PROFILES GROUP BY STATE";
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection con = DriverManager.getConnection("jdbc:odbc:Members");
    Statement stmt = con.createStatement();
    stmt.executeUpdate("CREATE INDEX STATE_INDEX ON MEMBER_PROFILES(STATE)");

    java.util.Date startTime = new java.util.Date();

    ResultSet rs = stmt.executeQuery(query);
    ResultSetMetaData md = rs.getMetaData();

    int nColumns = md.getColumnCount();
    for (int i = 1; i <= nColumns; i++) {
        System.out.print(md.getColumnLabel(i) + ((i == nColumns) ? "\n" : "\t"));
    }//from ww w .jav  a2  s . c  om

    while (rs.next()) {
        for (int i = 1; i <= nColumns; i++) {
            System.out.print(rs.getString(i) + ((i == nColumns) ? "\n" : "\t"));
        }
    }
    java.util.Date endTime = new java.util.Date();
    long elapsedTime = endTime.getTime() - startTime.getTime();
    System.out.println("Elapsed time: " + elapsedTime);

    stmt.executeUpdate("DROP INDEX MEMBER_PROFILES.STATE_INDEX");
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    try {//  ww  w.  j a  v a  2 s. com
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

        // Define the data source for the driver
        String sourceURL = "jdbc:odbc:technical_library";

        // Create a connection through the DriverManager 
        Connection databaseConnection = DriverManager.getConnection(sourceURL);
        System.out.println("Connection is: " + databaseConnection);
    } catch (ClassNotFoundException cnfe) {
        System.err.println(cnfe);
    } catch (SQLException sqle) {
        System.err.println(sqle);
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String url = "jdbc:odbc:yourDBName";
    System.out.println("Attempting to connect to " + url);

    try {//  w  w  w .j  av  a  2 s  .  c o m
        System.out.println("Loading the driver...");
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

        System.out.println("Establishing a connection...");
        Connection connection = DriverManager.getConnection(url);

        System.out.println("Connect to " + connection.getCatalog() + " a success!");
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:JdbcConnect.java

public static void main(String[] args) throws Exception {
    Connection conn1 = null;//from w  w w . j  a  v a  2  s.com
    Connection conn2 = null;
    Connection conn3 = null;

    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
    String jdbcUrl = "jdbc:odbc:authors";

    String user = "yourName";
    String pwd = "mypwd";

    conn1 = DriverManager.getConnection(jdbcUrl);
    if (conn1 != null) {
        System.out.println("Connection 1 successful!");
    }
    Properties prop = new Properties();
    prop.put("user", user);
    prop.put("password", pwd);

    conn2 = DriverManager.getConnection(jdbcUrl, prop);
    if (conn2 != null) {
        System.out.println("Connection 2 successful!");
    }
    conn3 = DriverManager.getConnection(jdbcUrl, user, pwd);
    if (conn3 != null) {
        System.out.println("Connection 3 successful!");
    }
    conn1.close();
    conn2.close();
    conn3.close();
    if (conn1.isClosed()) {
        System.out.println("Connection 1 is closed");
    }

    if (conn2.isClosed()) {
        System.out.println("Connection 2 is closed");
    }
    if (conn3.isClosed()) {
        System.out.println("Connection 3 is closed");
    }

    conn1.close();
    conn2.close();
    conn3.close();

}