diff --git a/FloydWarshall.java b/FloydWarshall.java new file mode 100644 index 0000000..ddf18da --- /dev/null +++ b/FloydWarshall.java @@ -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 stack; + + public TravellingSalesman() + { + stack = new Stack(); + } + + 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 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(); + } +} \ No newline at end of file diff --git a/jdbc-tcp/Employee.java b/jdbc-tcp/Employee.java new file mode 100644 index 0000000..18d53c8 --- /dev/null +++ b/jdbc-tcp/Employee.java @@ -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; + } +} diff --git a/jdbc-tcp/TCPClient.java b/jdbc-tcp/TCPClient.java new file mode 100644 index 0000000..cae98b1 --- /dev/null +++ b/jdbc-tcp/TCPClient.java @@ -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); + } + + } + +} diff --git a/jdbc-tcp/TCPServer.java b/jdbc-tcp/TCPServer.java new file mode 100644 index 0000000..58a4cbd --- /dev/null +++ b/jdbc-tcp/TCPServer.java @@ -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 details = new ArrayList(); + 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); + } + + } + +} diff --git a/jdbc-tcp/employeedb/EmployeeDB.sql b/jdbc-tcp/employeedb/EmployeeDB.sql new file mode 100644 index 0000000..b0832c4 --- /dev/null +++ b/jdbc-tcp/employeedb/EmployeeDB.sql @@ -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); \ No newline at end of file diff --git a/jdbc-tcp/employeedb/db.opt b/jdbc-tcp/employeedb/db.opt new file mode 100644 index 0000000..4ed6015 --- /dev/null +++ b/jdbc-tcp/employeedb/db.opt @@ -0,0 +1,2 @@ +default-character-set=utf8 +default-collation=utf8_general_ci diff --git a/jdbc-tcp/employeedb/employee.frm b/jdbc-tcp/employeedb/employee.frm new file mode 100644 index 0000000..692c446 Binary files /dev/null and b/jdbc-tcp/employeedb/employee.frm differ diff --git a/jdbc-tcp/employeedb/employee.ibd b/jdbc-tcp/employeedb/employee.ibd new file mode 100644 index 0000000..5866c67 Binary files /dev/null and b/jdbc-tcp/employeedb/employee.ibd differ diff --git a/jdbc-tcp/employeedb/info b/jdbc-tcp/employeedb/info new file mode 100644 index 0000000..35c98b5 --- /dev/null +++ b/jdbc-tcp/employeedb/info @@ -0,0 +1 @@ +Employee database files. diff --git a/jdbc-tcp/info b/jdbc-tcp/info new file mode 100644 index 0000000..9bfc4de --- /dev/null +++ b/jdbc-tcp/info @@ -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!