Java

Networking Application using Jdbc Mysql

This tutorial will teach you Networking Application using TCP/IP in Java step by step. the example to create a server is create a serversocket object that listens at a paticulat port for client requests.when the server gets a vaild request.the server socket obtains the Socket object created by the client.the communication between the server and the client occurs using the socket.

Functional requirements

The server shall be able to capture of receiving message from a client.

The server shall be able to connect to the database and retrieve the student information.

The server shall run on port 4000.

The client shall be a JFrame.

The client shall be capable of receiving information from the server application.

Creating  the  StudentInfo Class

class studentinfo implements Serializable
{
   public String sname;
   public String mno;
}

The preceding code is saved as StudentInfo.java

Creating  the  Server Class

The Server class performs the following functions:

Create a server socket using the ServerSocket object.

Listens for the client request

The following code is used to create the Server class that receives requests from the clients.

import java.net.*;
import java.io.*;
import java.sql.*;

public class server 
{
   ServerSocket socket1;

   public server( )
   {
 try{
    socket1=new ServerSocket( 4000);
    System.out.println("server is now ready....");

      while(true)
      {
          Socket clientconnection=socket1.accept(); /*waiting for clients to connect*/           Handler handler1= new Handler( clientconnection);
           handler1.start();
     }
    }
                    catch(Exception ioe)
    {
       System.out.println("error");
    }
 }/*end of constructor*/

    class Handler extends Thread
    {
 ObjectInputStream in=null;
 ObjectOutputStream out=null;

 public Handler( Socket request)
 {
  try{
     in=new ObjectInputStream (request.getInputStream( ));
     out=new ObjectOutputStream( request.getOutputStream());
  }catch(IOException io)
    {
   System.out.println("can't take i/o stream");
   }
    }
 
   public void run( )
   {
  try{
       while(true)
       {
      studentinfo  p1=(studentinfo)in.readObject( );
   Class.forName("com.mysql.jdbc.Driver");
                        Connection  con1 = DriverManager.getConnection("jdbc:mysql://localhost/network", "root","");

     PreparedStatement stat1=con1.prepareStatement("insert into student(stname,mobile) values(?,?)");
   
   stat1.setString(1,p1.sname);
   stat1.setString(2,p1.mno);
     int k=stat1.executeUpdate();
   if(k==1)out.writeObject("success");
   else
      out.writeObject("failed");
   }/*end of while*/
    }catch(Exception ee)
     {
         try{ 
      out.writeObject("failed");
    }catch(Exception eee){ }
      System.out.println("errrrrrrror");
     }
  }
 }/*end of Handler*/ 
    public static void main(String[ ] arg)
   {
 server server1=new server( );
      }
}

Creating  the  Client Application using jFrame

import java.io.Serializable;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.io.*;

public class client extends javax.swing.JFrame 
     {

        Socket socket1=null;
 ObjectInputStream in=null;
 ObjectOutputStream out=null;

    public client() {
        initComponents();
    }

   
    private void btn1ActionPerformed(java.awt.event.ActionEvent evt) {                                     
       
       
       try{
         socket1=new Socket("localhost", 4000); /*making connection*/         out= new ObjectOutputStream( socket1.getOutputStream());
         in= new ObjectInputStream (socket1.getInputStream( ));

         btn1.setEnabled(false);
         btn2.setEnabled(true);
         labelmesg.setText("connected..");
       }catch(IOException ioe)
    {
    System.out.println("can't connect");
  }
        
        
        
        
    }                                    

    private void btn2ActionPerformed(java.awt.event.ActionEvent evt) {                                     
   
        studentinfo p1=new studentinfo( );
 p1.sname=txtname.getText().trim( );
 p1.mno=txtmobile.getText().trim( );

 try
        {
 out.writeObject(p1);
 String mesg=(String)in.readObject();
 labelmesg.setText(mesg);

 txtname.setText("");
 txtmobile.setText("");
 txtname.requestFocus( );
 }
        catch(Exception ee)  
 {
 System.out.println("can't send data" + ee);
 }
    }                                    

   

I have attached the video tutorial below it will help you  to do this  step by step.

 

 

 

 

 

 

 

 

 

 

admin

Recent Posts

Laravel 11 CRUD Mastering RESTful API MVC with Repository Pattern

In this tutorial will teach Laravel 11 Api MVC with Repository Pattern Crud Application step…

3 weeks ago

Laravel 11 CRUD Application

In this tutorial will teach Laravel 11 CRUD Application step by step. Laravel  11 CRUD…

1 month ago

How to make Times Table in React

in this tutorials we will be talk about how to make a times table in…

2 months ago

Laravel Tutorial: How to Add Numbers Easily Laravel 10

In this tutorials will teach How to add two numbers in Laravel 10. (more…)

2 months ago

Build Full-Stack Node.js MongoDB CRUD App with JWT Authentication

In this tutorial, we will teach the process of building a full-stack application using Node.js,…

3 months ago

Hospital Management System using OOP Java and MySQL

In this tutorial, we will explin you through the process of building a Hospital Management…

3 months ago