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 Main() throws Exception {
    ArrayList columnNames = new ArrayList();
    ArrayList data = new ArrayList();
    String url = "jdbc:mysql://localhost:3306/yourdb";
    String userid = "root";
    String password = "sesame";
    String sql = "SELECT * FROM animals";

    Connection connection = DriverManager.getConnection(url, userid, password);
    Statement stmt = connection.createStatement();
    ResultSet rs = stmt.executeQuery(sql);
    ResultSetMetaData md = rs.getMetaData();
    int columns = md.getColumnCount();
    for (int i = 1; i <= columns; i++) {
        columnNames.add(md.getColumnName(i));
    }/*from  w w  w .  j a  va 2  s.c om*/
    while (rs.next()) {
        ArrayList row = new ArrayList(columns);
        for (int i = 1; i <= columns; i++) {
            row.add(rs.getObject(i));
        }
        data.add(row);
    }
    Vector columnNamesVector = new Vector();
    Vector dataVector = new Vector();
    for (int i = 0; i < data.size(); i++) {
        ArrayList subArray = (ArrayList) data.get(i);
        Vector subVector = new Vector();
        for (int j = 0; j < subArray.size(); j++) {
            subVector.add(subArray.get(j));
        }
        dataVector.add(subVector);
    }
    for (int i = 0; i < columnNames.size(); i++)
        columnNamesVector.add(columnNames.get(i));
    JTable table = new JTable(dataVector, columnNamesVector) {
        public Class getColumnClass(int column) {
            for (int row = 0; row < getRowCount(); row++) {
                Object o = getValueAt(row, column);
                if (o != null) {
                    return o.getClass();
                }
            }
            return Object.class;
        }
    };
    JScrollPane scrollPane = new JScrollPane(table);
    getContentPane().add(scrollPane);
    JPanel buttonPanel = new JPanel();
    getContentPane().add(buttonPanel, BorderLayout.SOUTH);
}

From source file:Main.java

public static Connection getMySqlConnection() throws Exception {
    String driver = "org.gjt.mm.mysql.Driver";
    String url = "jdbc:mysql://localhost/octopus";
    String username = "root";
    String password = "root";

    Class.forName(driver); // load MySQL driver
    Connection conn = DriverManager.getConnection(url, username, password);
    return conn;//ww  w .j a  v a  2s . c om
}

From source file:controler.DbModules.java

/**
 * This code is for getting connection to the db
 *
 * @return connection string/*  www  .  j a v a 2 s . c  o  m*/
 */
public static Connection getConnection() {
    try {
        Common one = new Common();

        DriverManager.registerDriver(new com.mysql.jdbc.Driver());//remember this
        con = DriverManager.getConnection(one.getDb_url(), one.getUser(), one.getPassword());
        System.out.println("connected");
    } catch (SQLException sql) {
        System.out.println(sql.getMessage());
    }
    return con;
}

From source file:TestApplet.java

public void init() {
    try {//from www  . j  av  a 2s. c om
        System.out.println("init(): loading OracleDriver for applet created at " + created.toString());
        Class.forName("oracle.jdbc.driver.OracleDriver");
        System.out.println("init(): getting connection");
        conn = DriverManager.getConnection("jdbc:oracle:thin:@dssw2k01:1521:orcl", "scott", "tiger");
    } catch (ClassNotFoundException e) {
        System.err.println("init(): ClassNotFoundException: " + e.getMessage());
    } catch (SQLException e) {
        System.err.println("init(): SQLException: " + e.getMessage());
    }
}

From source file:io.agi.framework.persistence.jdbc.JdbcUtil.java

/**
 * Execute an INSERT or UPDATE statement, that doesn't return any data.
 *
 * @param dbUrl/*from  w w  w  . ja  va2 s  .  co m*/
 * @param user
 * @param password
 * @param sql
 */
public static void Execute(String dbUrl, String user, String password, String sql) {
    Connection c = null;
    Statement s = null;
    try {
        c = DriverManager.getConnection(dbUrl, user, password);

        //STEP 4: Execute a query
        //_logger.info( "JDBC T: {} @1 jdbc = {}", System.currentTimeMillis(), sql );
        s = c.createStatement();
        //_logger.info( "JDBC T: {} @2 ", System.currentTimeMillis() );
        s.execute(sql);
        //_logger.info( "JDBC T: {} @3 ", System.currentTimeMillis() );

        //STEP 6: Clean-up environment
        //            s.close();
        //            c.close();
    } catch (SQLException se) {
        logger.error(se.toString(), se);
    } catch (Exception e) {
        logger.error(s.toString(), s);
    } finally {
        try {
            if (s != null)
                s.close();
        } catch (SQLException se2) {
            logger.error(se2.toString(), se2);
        } finally {
            try {
                if (c != null)
                    c.close();
            } catch (SQLException se) {
                logger.error(se.toString(), se);
            }
        }
    }
}

From source file:javaclasses.adminclass.java

public void connect() {
    try {/*from w  w w  .j  a v  a 2s .  c o m*/
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        con = DriverManager.getConnection("jdbc:sqlserver://CHARMEDALLOY\\SQLEXPRESS;databasename=mydb", "sa",
                "aval");
        System.out.println("connected");
    } catch (ClassNotFoundException e) {
        System.out.println("not connected" + e);

    } catch (SQLException e) {
        System.out.println("not connected" + e);
    }
}

From source file:TestAppletNetscape.java

public void init() {
    try {/*from  w  w w  .  j av a  2  s .c  o  m*/
        System.out.println("init(): loading OracleDriver for applet created at " + created.toString());
        Class.forName("oracle.jdbc.driver.OracleDriver");
        PrivilegeManager.enablePrivilege("UniversalConnect");
        System.out.println("init(): getting connection");
        PrivilegeManager.checkPrivilegeEnabled("UniversalConnect");
        conn = DriverManager.getConnection("jdbc:oracle:thin:@dssnt01:1521:dssora01", "scott", "tiger");
    } catch (ClassNotFoundException e) {
        System.err.println("init(): ClassNotFoundException: " + e.getMessage());
    } catch (SQLException e) {
        System.err.println("init(): SQLException: " + e.getMessage());
    }
}

From source file:au.org.ala.layers.stats.ObjectsStatsGenerator.java

private static Connection getConnection() {
    Connection conn = null;//from w ww  .  j a va 2 s  .  co m
    try {
        Class.forName("org.postgresql.Driver");
        String url = db_url;
        conn = DriverManager.getConnection(url, db_usr, db_pwd);

    } catch (Exception e) {
        logger.error("Unable to create Connection", e);
    }

    return conn;
}

From source file:Main.java

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

From source file:TestDatabaseMetaDataToolDatabaseInformation.java

public static Connection getOracleConnection() throws Exception {
    String driver = "oracle.jdbc.driver.OracleDriver";
    String url = "jdbc:oracle:thin:@localhost:1521:databaseName";
    String username = "name";
    String password = "password";
    Class.forName(driver);// www . ja  v  a2  s  .  c o  m
    Connection conn = DriverManager.getConnection(url, username, password);
    return conn;
}