Sunday, January 8, 2012

Connecting a phpMyAdmin DB to Eclipse

This is a small tutorial on how to connecting a phpMyAdmin database to Eclipse.
Things you need - Eclipse , mySQLConnectorJXAMPP

Step 1
Once you install XAMPP, Open the install folder. There you will find 'xampp-control.exe' . Open it configure it to the following setting. Apache and Mysql are initiated by their start buttons.



Go to your browser, type 'localhost' to the address bar. Then click phpMyAdmin on the left side bar.

In the phpMyAdmin page, you are able to create the database of what you want and the tables necessary.

For more help -
InstallingConfiguringDevelopingWithXAMPP

Step 2
Open Eclipse. Create a project in where you want to connect to the database. Right click the project file > Build Path > Add External Archives

From the selection window, select the 'mysql-connector-java-5.1.18-bin.jar' file in the mySQLConnectorJ folder. (You may have a different version of the jar file)

Step 3
The code below is a class I made in connecting to a database in XAMPP.

//THE_CODE

package database;
import java.sql.Connection;
import java.sql.DriverManager;

public final class Connect {
private Connection conn;
public Connect(){
conn = null;
try{
String userName = "root";
String password = "";
String url = "jdbc:mysql://localhost/shop";
Class.forName ("com.mysql.jdbc.Driver").newInstance ();
conn = DriverManager.getConnection(url, userName, password);
}
catch(Exception e){
System.out.println("Exception found");
}
}
public Connection getConnection(){
return conn;
}
public void closeConnection(){
try{
conn.close ();
}catch (Exception e) {
System.out.println ("Connection close error");
}
}
}


//DESCRIPTION
The code below is the main part in connecting to the database.


String userName = "root";
String password = "";
String url = "jdbc:mysql://localhost/shop";
Class.forName ("com.mysql.jdbc.Driver").newInstance ();
conn = DriverManager.getConnection(url, userName, password);

The Strings userName and password are the username and password for the database.
"shop" mentioned there is the name of the database.
To execute SQL statements, follow the tutorial link given below -
http://www.kitebird.com/articles/jdbc.html

*Remember to close the connection after using it. The closeConnection function will execute it.

Hope all this is clear for you. :D

1 comment:

  1. "I very much enjoyed this article.Nice article thanks for given this information. i hope it useful to many pepole.php jobs in hyderabad.
    "

    ReplyDelete