Things you need - Eclipse , mySQLConnectorJ, XAMPP
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
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
"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"