snippetjournal

– notes, snippet codes, and my journal

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;
    }

Written by snippetjournal

June 30, 2012 at 3:18 am

Posted in Programming

Tagged with , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: