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:com.mum.edu.cs472.DbConnection.java

public static ResultSet getWords(String word) throws SQLException {
    try {//from  w ww. j  a v  a2  s.  c  o  m
        Class.forName("com.mysql.jdbc.Driver");
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/entries", "root", "root");

        //here sonoo is database name, root is username and password  
        Statement stmt = con.createStatement();

        ResultSet rs = stmt.executeQuery("SELECT * FROM entries where lower(word)=Lower('" + word + "');");

        return rs;

    } catch (Exception e) {
        System.out.println(e);
    }

    return null;
}

From source file:Main.java

private static Connection getConnection() throws Exception {
    String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
    String url = "jdbc:odbc:northwind";
    String username = "";
    String password = "";
    Class.forName(driver);/* w ww.ja va  2 s.c  o  m*/
    return DriverManager.getConnection(url, username, password);
}

From source file:Student.java

  public static void checkData() throws Exception {
  Class.forName("org.hsqldb.jdbcDriver");
  Connection conn = DriverManager.getConnection("jdbc:hsqldb:data/tutorial", "sa", "");
  Statement st = conn.createStatement();

  ResultSet mrs = conn.getMetaData().getTables(null, null, null, new String[] { "TABLE" });
  while (mrs.next()) {
    String tableName = mrs.getString(3);
    System.out.println("\n\n\n\nTable Name: "+ tableName);

    ResultSet rs = st.executeQuery("select * from " + tableName);
    ResultSetMetaData metadata = rs.getMetaData();
    while (rs.next()) {
      System.out.println(" Row:");
      for (int i = 0; i < metadata.getColumnCount(); i++) {
        System.out.println("    Column Name: "+ metadata.getColumnLabel(i + 1)+ ",  ");
        System.out.println("    Column Type: "+ metadata.getColumnTypeName(i + 1)+ ":  ");
        Object value = rs.getObject(i + 1);
        System.out.println("    Column Value: "+value+"\n");
      }/*  ww  w.j  a v  a 2  s  .  c  o  m*/
    }
  }
}

From source file:DeleteClobFromOracleServlet.java

public static Connection getConnection() throws Exception {
    String driver = "oracle.jdbc.driver.OracleDriver";
    String url = "jdbc:oracle:thin:@localhost:1521:databaseName";
    String username = "userName";
    String password = "password";
    Class.forName(driver);/*www.  j av  a2s . c  o  m*/
    Connection conn = DriverManager.getConnection(url, username, password);
    return conn;
}

From source file:MainClass.java

public static void readDatabase() {
    String data = "jdbc:derby:presidents";
    try {/*  ww  w .  ja  va2s .  c  o m*/
        Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
        Connection conn = DriverManager.getConnection(data, "", "");
        Statement st = conn.createStatement();
        ResultSet rec = st.executeQuery("SELECT * FROM contacts ORDER BY name");
        while (rec.next()) {
            System.out.println(
                    rec.getString("name") + "\n" + rec.getString("address1") + "\n" + rec.getString("address2")
                            + "\n" + rec.getString("phone") + "\n" + rec.getString("email") + "\n");
        }
        st.close();
    } catch (Exception e) {
        System.out.println("Error - " + e.toString());
    }
}

From source file:Main.java

private static Connection getHSQLConnection() throws Exception {
    Class.forName("org.hsqldb.jdbcDriver");
    System.out.println("Driver Loaded.");
    String url = "jdbc:hsqldb:data/tutorial";
    return DriverManager.getConnection(url, "sa", "");
}

From source file:com.quangphuong.crawler.dbutil.DBHandler.java

public static Connection openConnection() {
    try {/*from   w  w w .  jav a 2s. c om*/
        Class.forName("com.mysql.jdbc.Driver");
        boolean isDebug = System.getenv("OPENSHIFT_APP_NAME") == null;
        DBConfig dBConfig;
        if (isDebug) {
            dBConfig = new DBConfig(DBConstant.driver, DBConstant.host, DBConstant.port, DBConstant.schema,
                    DBConstant.user, DBConstant.password);
        } else {
            dBConfig = new DBConfig(DBConstant.driver, DBConstant.OPENSHIFT_HOST, DBConstant.OPENSHIFT_PORT,
                    DBConstant.schema, DBConstant.OPENSHIFT_USER, DBConstant.OPENSHIFT_PASSWORD);
        }
        System.out.println("COnnection str: " + dBConfig.toString());
        Connection connection = DriverManager.getConnection(dBConfig.toString(), dBConfig.getUser(),
                dBConfig.getPassword());

        return connection;
    } catch (Exception ex) {
        Logger.getLogger(DBConfig.class.getName()).log(Level.SEVERE, null, ex);
    }
    return null;
}

From source file:DemoDataTruncation.java

public static Connection getMySQLConnection() throws Exception {
    String driver = "org.gjt.mm.mysql.Driver";
    String url = "jdbc:mysql://localhost/databaseName";
    String username = "root";
    String password = "root";
    Class.forName(driver);//  ww  w  .  ja va2 s  .  c o  m
    return DriverManager.getConnection(url, username, password);
}

From source file:com.app.util.DatabaseUtil.java

public static Connection getDatabaseConnection() throws DatabaseConnectionException {

    if (!_isPropertiesSet) {
        _log.error("The database properties are not set");

        throw new DatabaseConnectionException();
    }//from   w  ww.  ja  v a  2 s . co m

    try {
        return DriverManager.getConnection(_databaseURL, _databaseUsername, _databasePassword);
    } catch (SQLException sqle) {
        _log.error(sqle.getMessage(), sqle);

        throw new DatabaseConnectionException(sqle);
    }
}

From source file:aaa.DBOperations.java

protected static void openConnection() {
    try {//  w w w .j av  a2 s  . c o  m
        try {
            Class.forName(Initialize.CLASS_NAME).newInstance();
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException ex) {
            Logger.getLogger(DBOperations.class.getName()).log(Level.SEVERE, null, ex);
        }
        connection = (Connection) DriverManager.getConnection(Initialize.DB_URL, Initialize.USERNAME,
                Initialize.PASSWORD);
    } catch (SQLException ex) {
        Logger.getLogger(DBOperations.class.getName()).log(Level.SEVERE, null, ex);
    }
}