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[] argv) throws Exception {
    String url = "jdbc:mysql://localhost:3306/";
    String dbName = "javatutorial";
    String userName = "root";
    String password = "root";

    Class.forName("com.mysql.jdbc.Driver");
    Connection con = DriverManager.getConnection(url + dbName, userName, password);
    File imgfile = new File("images.jpg");
    FileInputStream fin = new FileInputStream(imgfile);
    PreparedStatement pre = con.prepareStatement("insert into Image values(?,?,?)");
    pre.setInt(1, 5);//from  ww w  . j ava 2 s .co m
    pre.setString(2, "A");
    pre.setBinaryStream(3, fin, (int) imgfile.length());
    pre.executeUpdate();
    pre.close();
    con.close();
}

From source file:Main.java

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

    Connection con;// w  w w .ja v  a  2 s.  c  o  m
    Statement stmt;
    ResultSet rs;

    try {
        Class.forName(driver);
        con = DriverManager.getConnection("jdbc:odbc:databaseName", "student", "student");
        // Start a transaction
        con.setAutoCommit(false);

        stmt = con.createStatement();
        stmt.addBatch("UPDATE EMP SET JOB = 1");

        // Submit the batch of commands for this statement to the database
        stmt.executeBatch();

        // Commit the transaction
        con.commit();

        // Close the existing to be safe before opening a new one
        stmt.close();

        // Print out the Employees
        stmt = con.createStatement();
        rs = stmt.executeQuery("SELECT * FROM EMP");
        // Loop through and print the employee number, job, and hiredate
        while (rs.next()) {
            int id = rs.getInt("EMPNO");
            int job = rs.getInt("JOB");
            String hireDate = rs.getString("HIREDATE");

            System.out.println(id + ":" + job + ":" + hireDate);
        }
        con.close();
    } catch (SQLException ex) {
        ex.printStackTrace();
    }
}

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  www. j  av  a  2  s.  c om

    String sql = "INSERT INTO pictures (name, description, image) VALUES (?, ?, ?)";
    PreparedStatement stmt = conn.prepareStatement(sql);
    stmt.setString(1, "java.gif");
    stmt.setString(2, "Java Official Logo");

    File image = new File("D:\\a.gif");
    FileInputStream fis = new FileInputStream(image);
    stmt.setBinaryStream(3, fis, (int) image.length());
    stmt.execute();

    conn.commit();
    fis.close();
    conn.close();
}

From source file:Main.java

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

    Class.forName("oracle.jdbc.driver.OracleDriver");

    String url = "jdbc:oracle:thin:@localhost:1521:javaDemo";
    String username = "username";
    String password = "welcome";

    String sql = "CREATE TABLE books (id NUMBER(11), title VARCHAR2(64))";
    Connection connection = DriverManager.getConnection(url, username, password);
    Statement statement = connection.createStatement();
    statement.execute(sql);/*from  w ww.  ja  v  a2s . co  m*/
    connection.close();
}

From source file:Main.java

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

    boolean isBatchingSupported = metadata.supportsBatchUpdates();
    System.out.println("Batching Supported = " + isBatchingSupported);
    connection.close();
}

From source file:Main.java

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

    boolean isSupportTransaction = metadata.supportsTransactions();
    System.out.println("Support Transaction = " + isSupportTransaction);
    connection.close();
}

From source file:Main.java

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

    Class.forName(DRIVER);//from w w  w.  j  a v  a2  s.  co  m
    Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
    DatabaseMetaData metadata = connection.getMetaData();

    int maxLength = metadata.getMaxTableNameLength();
    System.out.println("Max Table Name Length = " + maxLength);
    connection.close();
}

From source file:Main.java

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

    Class.forName(DRIVER);//  w  ww. j  a v  a2  s  .  c om
    Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
    DatabaseMetaData metadata = connection.getMetaData();

    int maxConnection = metadata.getMaxConnections();
    System.out.println("Maximum Connection = " + maxConnection);
    connection.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Class.forName(DRIVER);/*from ww  w. j  a v a2 s .  c om*/
    Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
    DatabaseMetaData metadata = connection.getMetaData();
    String[] functions = metadata.getTimeDateFunctions().split(",\\s*");

    for (int i = 0; i < functions.length; i++) {
        String function = functions[i];
        System.out.println("Function = " + function);
    }
    connection.close();
}

From source file:Main.java

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

    String[] functions = metadata.getSystemFunctions().split(",\\s*");

    for (int i = 0; i < functions.length; i++) {
        String function = functions[i];
        System.out.println("Function = " + function);
    }
}