How create WebServices API in JAVA Using Servlet and MySQL?

Open Eclipse indigo or Netbeans and create dynamic app

This tutorial series demonstrates a way to produce a complete internet services server and consumer application that you simply will simply run from the command with Java SE half-dozen instead of from inside internet application server containers. employing a straightforward howdy World example, you may leverage the Eclipse IDE, Java SE 6, and Apache hymenopteran to simply produce totally functioning internet services server and consumer applications. you may conjointly use the TCP/IP Monitor to look at the communication traffic between the server and consumer, and use the Eclipse internet Services adventurer tool to check the net service.

Prerequisites :
This tutorial includes easy steps written for beginning- to intermediate-level Java programmers with some operating data of the Java language and hymenopteran builds. Novice to a lot of advanced Java developers can gain some data of the way to build, deploy, and run complete internet services servers and distributed shoppers to supply firewall-friendly remote communications and applications process.

⇒Create dynamic app>>StudentApp
⇒Create JAVA class Name is DBConnection and put below data (below code is connection between database and java).

Youtube Channel :


DBConnection

 import java.sql.Connection;  
 import java.sql.DriverManager;  
 /**  
  *  
  * @author Aaru     
  */  
 public class DBConnectiion {  
     String Driver="com.mysql.jdbc.Driver";//this is mysql driver class path  
     String SQL="select Name,Address,Phone from college.Student";//this my sql query for get data from database  
     String URL="jdbc:mysql://localhost:3306";//this my sql url for database connection..  
     String USER="root"; //this is my database connection username and password  
     String PASS="root";  
 }  


Create a JAVA class name is   StudentBeam   the class is use for store multiple data in array.

StudentBeam


 /*  
  * To change this license header, choose License Headers in Project Properties.  
  * To change this template file, choose Tools | Templates  
  * and open the template in the editor.  
  */  
 /**  
  *  
  * @author Aaru  
  */  
 public class Studentbeam {  
   private String Name;  
   private String Address;  
   private String phone;  
   public String getName() {  
     return Name;  
   }  
   public void setName(String Name) {  
     this.Name = Name;  
   }  
   public String getAddress() {  
     return Address;  
   }  
   public void setAddress(String Address) {  
     this.Address = Address;  
   }  
   public String getPhone() {  
     return phone;  
   }  
   public void setPhone(String phone) {  
     this.phone = phone;  
   }  
 }  



Create a JavaServlet name is StudentServlet and create a java Class name is StudentImp
put below code in StudentImp.java



StudentImp


 import java.sql.Connection;  
 import java.sql.DriverManager;  
 import java.sql.ResultSet;  
 import java.sql.SQLException;  
 import java.sql.Statement;  
 import java.util.ArrayList;  
 import java.util.logging.Level;  
 import java.util.logging.Logger;  
 /*  
  * To change this license header, choose License Headers in Project Properties.  
  * To change this template file, choose Tools | Templates  
  * and open the template in the editor.  
  */  
 /**  
  *  
  * @author Aaru  
  */  
 public class StudentImp {  
   DBConnectiion DB=new DBConnectiion();  
   Connection con;  
   Statement stmt;  
   ArrayList<Studentbeam>alStd=new ArrayList<>();  
   public ArrayList<Studentbeam> getData(){  
     try {  
       Class.forName(DB.Driver);  
       con=DriverManager.getConnection(DB.URL, DB.USER, DB.PASS);  
      stmt= con.createStatement();  
       ResultSet rs= stmt.executeQuery(DB.SQL);  
       while (rs.next()) {  
         Studentbeam s=new Studentbeam();  
         s.setName(rs.getString("Name"));  
         s.setAddress(rs.getString("Address"));  
         s.setPhone(rs.getString("Phone"));  
         alStd.add(s);  
         System.out.println("add data success in array");  
       }  
     } catch (ClassNotFoundException ex) {  
       Logger.getLogger(StudentImp.class.getName()).log(Level.SEVERE, null, ex);  
     } catch (SQLException ex) {  
       Logger.getLogger(StudentImp.class.getName()).log(Level.SEVERE, null, ex);  
     }  
     return alStd;  
     //Now App run success  
     //lets Connect with android app using ip address  
   }  
 }  




put below code in StudentServlet

StudentServlet


 /*  
  * To change this license header, choose License Headers in Project Properties.  
  * To change this template file, choose Tools | Templates  
  * and open the template in the editor.  
  */  
 import java.io.IOException;  
 import java.io.PrintWriter;  
 import java.util.ArrayList;  
 import javax.servlet.ServletException;  
 import javax.servlet.http.HttpServlet;  
 import javax.servlet.http.HttpServletRequest;  
 import javax.servlet.http.HttpServletResponse;  
 import org.json.JSONArray;  
 /**  
  *  
  * @author Aaru  
  */  
 public class StudentServlet extends HttpServlet {  
   @Override  
   protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {  
     //b'coz we use get method android side..  
     StudentImp imp=new StudentImp();  
     ArrayList<Studentbeam>alstd= imp.getData();  
     if (alstd!=null) {  
       JSONArray array=new JSONArray(alstd);  
       PrintWriter pw= resp.getWriter();  
       pw.write(array.toString());  
       pw.print(array.toString());  
       //everting is done let' run  
     }  
   }  
 }  


⇓find web.xml in project directory  and put below code)XML


 <?xml version="1.0" encoding="UTF-8"?>  
 <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">  
   <servlet>  
     <servlet-name>StudentServlet</servlet-name>  
     <servlet-class>StudentServlet</servlet-class>  
   </servlet>  
   <servlet-mapping>  
     <servlet-name>StudentServlet</servlet-name>  
     <url-pattern>/StudentServlet</url-pattern>  
   </servlet-mapping>  
 </web-app>  


Run App as Server(Apache Tomcat or Other)



click below for retrieve data in android app.

Comments