Makistos
10/28/2013 - 7:34 AM

This little function will create a connection to a MySql database in the local machine. Database and user name and password are supplied as

This little function will create a connection to a MySql database in the local machine. Database and user name and password are supplied as parameters. #mysql #java

private void initDb(String db, String user, String pw) {
        String url = "jdbc:mysql://localhost/" + db;

        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            con = DriverManager.getConnection(url, user, pw);
            if (con.isClosed()) {
                System.err.println("Failed to connecto to " + db);
            }
        }
        catch (Exception e) {
           System.err.println("Exception: "+ e.getMessage());
        }
    }