This article explains about how to connect to a MYSQL database provided by XAMPP using Servlet and JDBC.
Following are assumed to be already installed and working on your system:
- Linux or Windows operating system
- XAMPP
- Eclipse IDE
This tutorial assumes that the user has previous experience in working with MYSQL DBMS or using phpmyadmin and knowledge of writing programs in Java using JDBC. Now let’s start with the actual process.
Open XAMPP’s homepage by typing the URL: http://localhost/
Click phpmyadmin link available in the left-hand side menu. Default username for MYSQL database is root and password is empty. By default a database will be available by the name test.
Create a table named users with two columns name and pass. Insert some dummy data into the table.
Next step is to download the MYSQL’s ConnectorJ driver (jar file) available here. Next, add the mysql-connector-java-3.1.14-bin.jar file to the lib folder available in the WEB-INF folder of the project in Eclipse.
Now, insert the following Java code in a servlet file:
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class MYSQLConnect
*/
public class MYSQLConnect extends HttpServlet
{
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
response.setContentType("text/html");
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "");
String query = "select *from users";
Statement st = con.createStatement();
ResultSet rs = st.executeQuery(query);
while(rs.next())
{
response.getWriter().write(rs.getString("name") + " - " + rs.getString("pass") + "
");
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
Run the above servlet to see the data from the users table on the webpage.
The entire project in WAR format is available for download here.
Suryateja Pericherla, at present is a Research Scholar (full-time Ph.D.) in the Dept. of Computer Science & Systems Engineering at Andhra University, Visakhapatnam. Previously worked as an Associate Professor in the Dept. of CSE at Vishnu Institute of Technology, India.
He has 11+ years of teaching experience and is an individual researcher whose research interests are Cloud Computing, Internet of Things, Computer Security, Network Security and Blockchain.
He is a member of professional societies like IEEE, ACM, CSI and ISCA. He published several research papers which are indexed by SCIE, WoS, Scopus, Springer and others.
@Aman,
At line 27 in the above code, replace “localhost” with your mysql IP address. In this case it is “192.168.0.10”
How to connect if I am running it on eclipse using tomcat and the mysql xamp is on link like 192.168.0.10.
I am using ubuntu and I have mysqlserver insatalled so, localhost get connceted to the local mysql server.