Home Java Networking Application using Jdbc Mysql

Networking Application using Jdbc Mysql

6 min read
0
0
1,264

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.

 

 

 

 

 

 

 

 

 

 

Load More Related Articles
Load More By admin
Load More In Java

Leave a Reply

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

Check Also

Employee Working Hours Calculation System using C#.net

Initialize the employee number, Hourswork,and Hoursrate to calculate a grosswage use the f…