Compare connecting MySQL database in Java and PHP

Today I want to write something different. So I thought to write how to connect with MySQL database in PHP and Java. This is not a comparison, I just want to get those two different things in to single a post. Actually in one of my earlier post I have showed how to connect with MySQL in PHP.

Lets start with Java and MySQL first. First you should check following things in your system.

  • Have you installed JDK and JRE in your computer?
  • Do you have MySQL Connector/J driver downloaded and stored in your computer?
  • Have you set the CLASSPATH Environment Variable to above mentioned MySQL Connector/j driver location? (Actually the full path as well as the full name of the .jre file is needed as Variable Value)

If all above things are ready, then you are ready to create a connection with MySQL in Java. I use NetBeans IDE just as my previous posts in J2ME. So there are some basic steps you have to follow.

  • Create a new Java Application project
    • File -> New Project
    • Select Java in Categories box
    • Select Java Application in Project box
    • Provide a Project Name and Project Location
    • Remove the tic mark on the Create Main Class
    • Click on Finish
  • Right click on the project name in the Projects pane
  • Select Properties
  • Select Libraries
  • Click on the Add JAR/Folder button
  • Select MySQL Connector jar file that you have downloaded
  • Click on OK button

Now create a .java file with in your default package and name it as Here is my file with the content.

 import java.sql.*;

   public class Connect
       public static void main (String[] args)
           Connection conn = null;

               String userName = "root"; // db username
               String password = ""; // db password
               String url = "jdbc:mysql://localhost/test"; //test = db name
               Class.forName ("com.mysql.jdbc.Driver").newInstance ();
               conn = DriverManager.getConnection (url, userName, password);
               System.out.println ("Database connection established");
           catch (Exception e)
               System.err.println ("Cannot connect to database server");
               if (conn != null)
                       conn.close ();
                       System.out.println ("Database connection terminated");
                   catch (Exception e) { /* ignore close errors */ }

If you have done every thing correctly you can see the text “Database connection establish” after you run the program. Now we will see connecting MySQL database in PHP.

As usual I am using two different PHP files for this, one for the Constants and the other for the database connection. Following is the content of constants.php


define ('DB_HOST', 'localhost');

define ('DB_USER', 'root');

define ('DB_PASSWORD', '');

define ('DB_SCHEMA', 'test');


And this is the content of the database.php



$connection = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
$db_select = mysql_select_db(DB_SCHEMA, $connection);


This is the end of this blog post. Try to remember the methods that we have used in two languages. 🙂


About AnujAroshA

Working as a Technical Lead. Specialized in iOS application development. A simple person :)
This entry was posted in Java, MySQL, PHP examples. Bookmark the permalink.

Leave a Reply

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

You are commenting using your 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 )

Connecting to %s