0% found this document useful (0 votes)
59 views

Java Ex10 Shelva

The document contains code for a Java server program that creates a socket to listen for clients on port 3333, accepts a client connection, and uses input/output streams to send and receive data from the client in a continuous loop until the client sends the message "Over". It also includes code for a Java client program that connects to the server, allows user input to be sent to and received from the server until the user enters "Over", and then closes the connection. The code connects to a MySQL database named "ChennaiSuperKings" containing player data and uses JDBC to execute a query selecting all records from the table and outputting them
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views

Java Ex10 Shelva

The document contains code for a Java server program that creates a socket to listen for clients on port 3333, accepts a client connection, and uses input/output streams to send and receive data from the client in a continuous loop until the client sends the message "Over". It also includes code for a Java client program that connects to the server, allows user input to be sent to and received from the server until the user enters "Over", and then closes the connection. The code connects to a MySQL database named "ChennaiSuperKings" containing player data and uses JDBC to execute a query selecting all records from the table and outputting them
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

PROGRAM[SREVER]:

import java.net.*;
import java.io.*;
public class Server{
public static void main(String args[]) throws IOException{
ServerSocket server = new ServerSocket(3333);
System.out.println("Server Started");
System.out.println("Waiting for client.....");
Socket k = server.accept();
System.out.println("Client connected");
DataInputStream in = new DataInputStream(k.getInputStream ());
DataOutputStream out = new DataOutputStream(k.getOutputStream());
String a="";
while(true){
a=in.readUTF();
System.out.println("Client:"+a);
if(a.equals("Over")){
break;
}
out.writeUTF(a);
}
System.out.println("Disconnected");
k.close();
server.close();
System.out.println("Server stopped");
}
}
PROGRAM[CLIENT]:
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
public class Client {
public static void main(String[] args) {
try {
Socket s = new Socket("localhost", 3333);
System.out.println("Connected");
DataInputStream in = new DataInputStream(s.getInputStream());
DataOutputStream out = new DataOutputStream(s.getOutputStream());
BufferedReader b = new BufferedReader(new InputStreamReader(System.in));
String a = "";
while (!a.equals("Over")) {
a = b.readLine();
out.writeUTF(a);
if (a.equals("Over")) {
break;
}
String c = in.readUTF();
System.out.println("Server: " + c);
}
s.close();
System.out.println("Disconnected");
} catch (IOException e) {
e.printStackTrace();
}
}
}
OUTPUT:
PROGRAM[DATABASE]:

create database ChennaiSuperKings;


use ChennaiSuperKings;
create table csk ( Jersey_no int,Name varchar(200),Strike_rate int);
insert into csk values(7,"Dhoni",132);
insert into csk values(31,"Ruturaj",141);
insert into csk values(18,"Moeen Ali",127);
insert into csk values(8,"Ravindra Jadeja",124);
insert into csk values(27,"Ben Stokes",121);
select * from csk;

PROGRAM:

import java.sql.*;
public class Main {
public static void main(String[] args) throws Exception{
try{
Class.forName("com.mysql.jdbc.Driver");
("com.mysql.jdbc.Driver");
Connection con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/ChennaiSup
erKings ","root","students ");
Statement st = con.createStatement();
ResultSet resultset = st.executeQuery("select * from csk");
while (resultset.next()){
System.out.println(resultset.getString(1) + " " + resultset.getString(2) + "
"+ resultset.getString(3));
}
con.close();
} catch (Exception ex){
System.out.println(ex);
}
}
}
OUTPUT:

You might also like