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 {

    Class.forName("com.mysql.jdbc.Driver");
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctutorial", "root", "root");

    Statement st = con.createStatement();
    int n = st.executeUpdate("ALTER TABLE mytable ADD UNIQUE( colName)");
    System.out.println("Query OK, " + n + " rows affected.");
}

From source file:Test.java

public static void main(String[] args) throws Exception {
    Connection conn = DriverManager.getConnection("...", "username", "password");
    String query = "{CALL GETDATE(?,?)}";
    CallableStatement callableStatement = (CallableStatement) conn.prepareCall(query);

    callableStatement.setInt(1, 123);/*w ww  .j  a  va2 s  .c  o m*/
    callableStatement.registerOutParameter(1, Types.DATE);
    callableStatement.executeQuery();

    Date date = callableStatement.getObject(2, Date.class);

}

From source file:Main.java

License:asdf

public static void main(String[] argv) throws Exception {
    Class.forName("com.mysql.jdbc.Driver");
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctutorial", "root", "root");

    String sql = "INSERT product VALUES(?,?)";
    PreparedStatement prest = con.prepareStatement(sql);
    prest.setString(1, "asdf");
    prest.setInt(2, 2009);//from w  ww.j  a va 2s  .  c  o  m
    int count = prest.executeUpdate();
    System.out.println(count + "row(s) affected");
    con.close();
}

From source file:Main.java

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

    Class.forName("com.mysql.jdbc.Driver");
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctutorial", "root", "root");

    String sql = "DELETE FROM product where year_made = ?";
    PreparedStatement prest = con.prepareStatement(sql);
    prest.setInt(1, 2008);//from  w w  w. j  a  va  2s. c  om
    int del = prest.executeUpdate();
    System.out.println("Number of deleted records: " + del);
    con.close();
}

From source file:Main.java

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

    Class.forName("com.mysql.jdbc.Driver");
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctutorial", "root", "root");

    con.setAutoCommit(false);/*from w w w. j  a v  a  2 s  .c o m*/
    String table1 = "INSERT emp_sal VALUES('v',1200)";
    String table2 = "DELETE FROM movies WHERE title = 'r'";
    Statement st = con.createStatement();
    st.addBatch(table1);
    st.addBatch(table2);
    int count[] = st.executeBatch();
    con.commit();
    con.close();
    System.out.println("Successfully!");
}

From source file:Main.java

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

    Class.forName("com.mysql.jdbc.Driver");
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctutorial", "root", "root");

    String sql = "INSERT myTable VALUES(?,?,?,?)";
    PreparedStatement prest = con.prepareStatement(sql);
    prest.setString(1, "A");
    prest.setInt(2, 5);/*from  w ww  .  jav  a2 s .  c  om*/
    prest.setDouble(3, 2.0);
    prest.setFloat(4, 4.2f);
    int row = prest.executeUpdate();
    System.out.println(row + " row(s) affected)");
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Class.forName("com.mysql.jdbc.Driver");
    Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/book", "root", "");

    PreparedStatement ps = connection.prepareStatement("UPDATE books SET title = ? WHERE id = ?");
    ps.setString(1, "Java");
    ps.setInt(2, 1);//from www .java2s  . c om
    int rows = ps.executeUpdate();

    System.out.printf("%d row(s) updated!", rows);
    connection.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String url = "jdbc:mysql://localhost:3306/mydb";
    Class.forName("com.mysql.jdbc.Driver");
    Connection conn = DriverManager.getConnection(url, "root", " ");
    System.out.println("Database connection established");
    conn.close();/*from ww w  .j a v a2s.  c  o m*/
    System.out.println("Database connection terminated");
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Class.forName("com.mysql.jdbc.Driver");
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctutorial", "root", "root");
    con.setAutoCommit(false);/*from  w  w w .j a v a  2 s . c  o  m*/
    String sql = "INSERT product VALUES(?,?)";
    PreparedStatement prest = con.prepareStatement(sql);
    prest.setString(1, "A");
    prest.setInt(2, 2002);
    prest.addBatch();
    prest.setString(1, "B");
    prest.setInt(2, 1998);
    prest.addBatch();
    prest.setString(1, "C");
    prest.setInt(2, 1980);
    prest.addBatch();
    prest.setString(1, "D");
    prest.setInt(2, 1975);
    prest.addBatch();
    int count[] = prest.executeBatch();
    con.commit();
    con.close();
}

From source file:Main.java

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

    Class.forName("com.mysql.jdbc.Driver");
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctutorial", "root", "root");

    Statement st = con.createStatement();
    ResultSet res = st.executeQuery("SELECT col_name  FROM mytable  ORDER BY col_name ASC");
    while (res.next()) {
        int col = res.getInt(1);
        System.out.println(col);/*from  w  w w .  j  av  a2  s .c  o  m*/
    }
}