snippetjournal

– notes, snippet codes, and my journal

Archive for June 2012

Back To Basic : JDBC CONNECTION MYSQL

leave a comment »

just snippet code for jdbc connection for mysql


   public Connection jdbcConnection(String dbname, String user, String pass) {
        System.out.println(" ... trying to Connect to jdbc mysql dbname "+dbname+" ...");
        String className = "com.mysql.jdbc.Driver";
        Connection connection = null;
        try {
            Class.forName(className);

            String hostname = "localhost";
            String port = "3306";
            String jdbconn = "jdbc:mysql://" + hostname + ":" + port + "/" + dbname;
            connection = DriverManager.getConnection(jdbconn, user, pass);

        } catch (SQLException ex) {
            Logger.getLogger(JDBCConnection.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(JDBCConnection.class.getName()).log(Level.SEVERE, null, ex);
        }

        if(connection == null)
            System.out.println(" connection failed . . . .");
        else
            System.out.println(" connection success . . . .");

        return connection;
    }

Advertisements

Written by snippetjournal

June 30, 2012 at 3:18 am

Posted in Programming

Tagged with , ,