Advanced Java and Web Technologies for JNTUK
Blog providing beginner tutorials on different web technologies like HTML, CSS, Javascript, PHP, MYSQL, XML, Java Beans, Servlets, JSP and AJAX
Subscribe to Startertutorials.com's YouTube channel for different tutorial and lecture videos.

Categories: JDBC. 2 Comments on Connecting to and Displaying data from MYSQL in XAMPP
0
(0)

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/


Subscribe to our monthly newsletter. Get notified about latest articles, offers and contests.


 

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.

How useful was this post?

Click on a star to rate it!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Suryateja Pericherla

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.

Note: Do you have a question on this article or have a suggestion to make this article better? You can ask or suggest us by filling in the below form. After commenting, your comment will be held for moderation and will be published in 24-48 hrs.

2 Comments

You can follow any responses to this entry through the RSS 2.0 feed.

@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.

Leave a Reply

Your email address will not be published. Required fields are marked *