Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions FloydWarshall.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
public class FloydWarshall {

public static void floydWarshall(int[][] graph){
int N = graph.length;
int dist[][] = new int[N][N];
for(int i=0;i<N;i++){
for(int j=0;j<N;j++){
dist[i][j] = graph[i][j];
}
}

for(int k=0;k<N;k++){
for(int i=0;i<N;i++){
for(int j=0;j<N;j++){
if(dist[i][j]!=-999)
dist[i][j] = Math.max(dist[i][k]+dist[k][j],dist[i][j]);
}
}
}

for(int i=0;i<N;i++){
System.out.println();
for(int j=0;j<N;j++){
System.out.print(" "+dist[i][j]+" ");;
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int graph[][] = {{-999,-999,-999,-999},
{3,4,5,-999},
{40,8,9,10},
{-999,1,2,3}
};
floydWarshall(graph);
}

}
92 changes: 92 additions & 0 deletions TravellingSalesman.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import java.util.*;

public class TravellingSalesman
{
private int numberOfNodes;
private Stack<Integer> stack;

public TravellingSalesman()
{
stack = new Stack<Integer>();
}

public void tsp(int adjacencyMatrix[][])
{
numberOfNodes = adjacencyMatrix[1].length - 1;
int[] visited = new int[numberOfNodes];
visited[0] = 1;
stack.push(0);
int element, dst = -1, i;
int min = Integer.MAX_VALUE;
boolean minFlag = false;
System.out.print(0 + "\t");

while (!stack.isEmpty())
{
element = stack.peek();
i = 1;
min = Integer.MAX_VALUE;
while (i <numberOfNodes)
{
if (adjacencyMatrix[element][i] > 1 && visited[i] == 0)
{
if (min > adjacencyMatrix[element][i])
{
min = adjacencyMatrix[element][i];
dst = i;
minFlag = true;
}
}
i++;
}
if (minFlag)
{
visited[dst] = 1;
stack.push(dst);
System.out.print(dst + "\t");
minFlag = false;
continue;
}
stack.pop();
}
}

public static void main(String... arg)
{
int number_of_nodes;
Scanner scanner = null;
try
{
System.out.println("Enter the number of nodes in the graph");
scanner = new Scanner(System.in);
number_of_nodes = scanner.nextInt();
int adjacency_matrix[][] = new int[number_of_nodes][number_of_nodes];
System.out.println("Enter the adjacency matrix");
for (int i = 0; i < number_of_nodes; i++)
{
for (int j = 0; j < number_of_nodes; j++)
{
adjacency_matrix[i][j] = scanner.nextInt();
}
}
for (int i = 0; i < number_of_nodes; i++)
{
for (int j = 0; j < number_of_nodes; j++)
{
if (adjacency_matrix[i][j] == 1 && adjacency_matrix[j][i] == 0)
{
adjacency_matrix[j][i] = 1;
}
}
}
System.out.println("the citys are visited as follows");
TravellingSalesman obj = new TravellingSalesman();
obj.tsp(adjacency_matrix);
} catch (Exception E)
{
System.out.println(E);
System.out.println("Wrong Input format");
}
scanner.close();
}
}
34 changes: 34 additions & 0 deletions jdbc-tcp/Employee.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
public class Employee implements java.io.Serializable{

private static final long serialVersionUID = 1L;

private int Eid;
private String name;
private String post;
private int Did;
private int salary;

Employee(int Eid, String name, String post, int Did, int salary){
this.Eid = Eid;
this.name = name;
this.post = post;
this.Did = Did;
this.salary = salary;
}

public int getEid(){
return this.Eid;
}
public String getName(){
return this.name;
}
public String getPost(){
return this.post;
}
public int getDid(){
return this.Did;
}
public int getSalary(){
return this.salary;
}
}
34 changes: 34 additions & 0 deletions jdbc-tcp/TCPClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import java.io.*;
import java.net.*;

public class TCPClient {

public static void main(String[] args) {
String serverName = "localhost";
int port = 6666;
try{
System.out.println("\n\nConnecting to " + serverName + " on port " + port);
Socket client = new Socket(serverName, port);
System.out.println("\n\nJust connected to " + client.getRemoteSocketAddress());

System.out.print("\n\n\nEnter Employee ID : ");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int idInput = Integer.parseInt(br.readLine());

OutputStream outToServer = client.getOutputStream();
DataOutputStream out = new DataOutputStream(outToServer);
out.writeInt(idInput);

InputStream inFromServer = client.getInputStream();
DataInputStream in = new DataInputStream(inFromServer);
System.out.print("\n\n\n\n\nBelow details were returned by the server : "+in.readUTF());

client.close();
}
catch(Exception e){
System.out.println("Client Side problem : "+e);
}

}

}
69 changes: 69 additions & 0 deletions jdbc-tcp/TCPServer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import java.util.*;
import java.io.*;
import java.net.*;
import java.sql.*;

public class TCPServer {

private ServerSocket TCPSocket;

public TCPServer(int port)throws IOException{
TCPSocket = new ServerSocket(port);
TCPSocket.setSoTimeout(2147483647);
}

public void retrieveEmployee(){
while(true)
{
try
{
System.out.println("\n\nWaiting for client on port " + TCPSocket.getLocalPort() + "...");
Socket server = TCPSocket.accept();
System.out.println("\n\nJust connected to "+ server.getRemoteSocketAddress());
DataInputStream in = new DataInputStream(server.getInputStream());
int x = in.readInt();
System.out.println("\n\nClient Requested for employee "+x);

Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/EmployeeDB","root", "pass");
PreparedStatement stmt=con.prepareStatement("SELECT *FROM EMPLOYEE WHERE Eid = ?");
stmt.setInt(1,x);
List<Employee> details = new ArrayList<Employee>();
ResultSet rs = stmt.executeQuery();
while(rs.next()){
Employee E = new Employee(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getInt(4),rs.getInt(5));
details.add(E);
}
String str = null;
for(Employee E : details){
str = "\n\n\nEMPLOYEE ID : "+Integer.toString(E.getEid())+"\nEMPLOYEE NAME : "+E.getName()+"\nPOST : "+E.getPost()+"\nDEPT ID : "+Integer.toString(E.getDid())
+"\nSALARY : "+Integer.toString(E.getSalary())+"\n\n\n";
}

DataOutputStream out = new DataOutputStream(server.getOutputStream());
out.writeUTF(str);

server.close();


}
catch(Exception e){
System.out.println("Server side Retrieval problem : ");
e.printStackTrace();
}
}
}

public static void main(String[] args) {
int port = 6666;
try{
TCPServer t = new TCPServer(port);
t.retrieveEmployee();

}catch(Exception e){
System.out.println("Server main method problem : "+e);
}

}

}
13 changes: 13 additions & 0 deletions jdbc-tcp/employeedb/EmployeeDB.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
CREATE TABLE Employee(
Eid INT NOT NULL,
name VARCHAR(20) NOT NULL,
post VARCHAR(20) NOT NULL,
Did INT NOT NULL,
salary INT NOT NULL
);

INSERT INTO Employee (Eid, name, post, Did, salary) VALUES (1, 'ABC', 'Software Engineer', 101, 40000);
INSERT INTO Employee (Eid, name, post, Did, salary) VALUES (2, 'DEF', 'Systems Engineer', 201, 25000);
INSERT INTO Employee (Eid, name, post, Did, salary) VALUES (3, 'MNO', 'Data Scientist', 302, 30000);
INSERT INTO Employee (Eid, name, post, Did, salary) VALUES (4, 'PQR', 'Technical Analyst', 401, 30000);
INSERT INTO Employee (Eid, name, post, Did, salary) VALUES (5, 'XYZ', 'Java Developer', 501, 50000);
2 changes: 2 additions & 0 deletions jdbc-tcp/employeedb/db.opt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
default-character-set=utf8
default-collation=utf8_general_ci
Binary file added jdbc-tcp/employeedb/employee.frm
Binary file not shown.
Binary file added jdbc-tcp/employeedb/employee.ibd
Binary file not shown.
1 change: 1 addition & 0 deletions jdbc-tcp/employeedb/info
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Employee database files.
7 changes: 7 additions & 0 deletions jdbc-tcp/info
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
This project shows you how to connect your java program on a client to a java application a remote server that can serve you with database information.
A java-java communication takes place via TCP Sockets (there are other methods too), and
A java-database communication takes place via JDBC API.
So, I have designed this project to give the viewer a gist of both of these.
The database that I have used contains 5 records, with the "Eid" values 1 to 5.
So, enter any one from 1 to 5 on client side, and the employee details will be fetched accordingly.
You can alter the database as per need. Happy Coding!