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:Main.java

License:asdf

public static void main(String[] args) throws Exception {
    Connection conn = null;/*from  w ww.j a  va2 s.c  o  m*/
    conn = DriverManager.getConnection("jdbc:mysql://192.168.1.3/greektest?"
            + "useUnicode=yes&characterEncoding=UTF-8" + "&user=root&password=obfuscated");
    String sql = "INSERT INTO `chars` (`value`) VALUES (?)";
    PreparedStatement pst = conn.prepareStatement(sql);
    String var1 = "asdf";
    pst.setString(1, var1);
    pst.executeUpdate();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Statement stmt = DriverManager.getConnection("jdbc:odbc:employee").createStatement();
    ResultSet rs = stmt.executeQuery("select lastname, firstname, id from [Sheet1$]");
    while (rs.next()) {
        String lname = rs.getString(1);
        String fname = rs.getString(2);
        int id = rs.getInt(3);
        System.out.println(fname + " " + lname + "  id : " + id);
    }//ww  w.  j a  v  a2s.co  m
    rs.close();
    stmt.close();
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection c = DriverManager
            .getConnection("jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=c:/access.mdb");

    ResultSet rs = c.createStatement().executeQuery("select objet from Employee");

    while (rs.next()) {
        System.out.println(rs.getString("objet"));
    }//ww w.ja  v  a 2  s .  c  o  m
    rs.close();
    c.close();
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection conn = DriverManager.getConnection("jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};"
            + "Dbq=C:\\Users\\Public\\uls\\ulsTest.mdb;" + "SystemDB=C:\\Users\\Public\\uls\\Security.mdw;"
            + "Uid=Gord;" + "Pwd=obfuscated;" + "ExtendedAnsiSQL=1;");
    Statement s = conn.createStatement();
    s.execute("CREATE USER Tim pwd");
    System.out.println("User 'Tim' created.");
    s.execute("DROP USER Tim");
    System.out.println("User 'Tim' dropped.");
    s.close();//from w  w  w  .  j  a v a2  s.c  om
    conn.close();
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection conn = DriverManager.getConnection("jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};"
            + "DBQ=C:\\Users\\Public\\uls\\ulsTest.mdb;" + "SystemDB=C:\\Users\\Public\\uls\\Security.mdw;"
            + "Uid=Gord;" + "Pwd=obfuscated;" + "ExtendedAnsiSQL=1;");

    String UID = "Tim";
    String oldPWD = "oldpassword";
    String newPWD = "I like Java";

    Statement s = conn.createStatement();
    s.execute("ALTER USER " + UID + " PASSWORD \"" + newPWD + "\" \"" + oldPWD + "\"");

    s.close();/*from www  . j a  v a 2 s. c  o  m*/
    conn.close();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String url = "jdbc:odbc:datasource";
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection connection = DriverManager.getConnection(url);
    System.out.println("Connect to " + connection.getCatalog() + " a success!");
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

    ResultSet rs = DriverManager.getConnection("jdbc:odbc:employee_xls").createStatement()
            .executeQuery("Select * from [Sheet1$]");

    ResultSetMetaData rsmd = rs.getMetaData();
    int numberOfColumns = rsmd.getColumnCount();

    System.out.println("No of cols " + numberOfColumns);
    while (rs.next()) {
        for (int i = 1; i <= numberOfColumns; i++) {
            String columnValue = rs.getString(i);
            System.out.println(columnValue);
        }/*from   w ww.j  av  a2s  .c o  m*/
    }
    rs.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection con = DriverManager.getConnection("jdbc:mysql://192.168.1.3/zzzTest?" + //
            "useUnicode=yes&characterEncoding=UTF-8" + //
            "&user=root&password=whatever");
    String newName = "G";
    String newEmail = "g@example.com";
    String newMobile = "444-555-2222";
    String sql = "SELECT " + //
            "id, " + //
            "name, " + //
            "email, " + //
            "mobile " + //
            "FROM registerSmsUsers " + //
            "WHERE mobile = ? " + //
            "FOR UPDATE";
    PreparedStatement pst = con.prepareStatement(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE);
    pst.setString(1, newMobile);/*from   w  w w. ja va  2  s.c  o m*/
    ResultSet rs = pst.executeQuery();
    if (rs.next()) {
        rs.moveToCurrentRow();
        rs.updateString("name", newName);
        rs.updateString("email", newEmail);
        rs.updateRow();
        System.out.println("Existing row updated.");
    } else {
        rs.moveToInsertRow();
        rs.updateString("name", newName);
        rs.updateString("email", newEmail);
        rs.updateString("mobile", newMobile);
        rs.insertRow();
        System.out.println("New row inserted.");
    }
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection con = DriverManager.getConnection("jdbc:odbc:Contacts");
    Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
    ResultSet rs = stmt.executeQuery("select * from employee");
    rs.moveToInsertRow();/*www  .j av a  2 s.  co m*/

    rs.updateInt(1, 150);
    rs.updateString("First_Name", "Nigel");
    rs.updateString("Last_Name", "Thornebury");

    rs.insertRow();
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection con = DriverManager.getConnection("jdbc:odbc:Contacts");
    Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
    ResultSet rs = stmt.executeQuery("select * from employee");
    rs.moveToInsertRow();/* w  w w. j  av a2 s . c o m*/

    rs.updateInt("Contact_ID", 150);
    rs.updateString("First_Name", "Nigel");
    rs.updateString("Last_Name", "Thornebury");

    rs.insertRow();
}