-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Commit initial: Adaugare proiect in git.
- Loading branch information
0 parents
commit 0302867
Showing
13 changed files
with
204 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<module type="JAVA_MODULE" version="4"> | ||
<component name="NewModuleRootManager" inherit-compiler-output="true"> | ||
<exclude-output /> | ||
<content url="file://$MODULE_DIR$"> | ||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" /> | ||
</content> | ||
<orderEntry type="inheritedJdk" /> | ||
<orderEntry type="sourceFolder" forTests="false" /> | ||
<orderEntry type="module-library" scope="TEST"> | ||
<library name="JUnit5.4"> | ||
<CLASSES> | ||
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter/5.4.2/junit-jupiter-5.4.2.jar!/" /> | ||
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-api/5.4.2/junit-jupiter-api-5.4.2.jar!/" /> | ||
<root url="jar://$MAVEN_REPOSITORY$/org/apiguardian/apiguardian-api/1.0.0/apiguardian-api-1.0.0.jar!/" /> | ||
<root url="jar://$MAVEN_REPOSITORY$/org/opentest4j/opentest4j/1.1.1/opentest4j-1.1.1.jar!/" /> | ||
<root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-commons/1.4.2/junit-platform-commons-1.4.2.jar!/" /> | ||
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-params/5.4.2/junit-jupiter-params-5.4.2.jar!/" /> | ||
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-engine/5.4.2/junit-jupiter-engine-5.4.2.jar!/" /> | ||
<root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-engine/1.4.2/junit-platform-engine-1.4.2.jar!/" /> | ||
</CLASSES> | ||
<JAVADOC /> | ||
<SOURCES /> | ||
</library> | ||
</orderEntry> | ||
<orderEntry type="module-library"> | ||
<library> | ||
<CLASSES> | ||
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter/5.4.2/junit-jupiter-5.4.2.jar!/" /> | ||
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-api/5.4.2/junit-jupiter-api-5.4.2.jar!/" /> | ||
<root url="jar://$MAVEN_REPOSITORY$/org/apiguardian/apiguardian-api/1.0.0/apiguardian-api-1.0.0.jar!/" /> | ||
<root url="jar://$MAVEN_REPOSITORY$/org/opentest4j/opentest4j/1.1.1/opentest4j-1.1.1.jar!/" /> | ||
<root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-commons/1.4.2/junit-platform-commons-1.4.2.jar!/" /> | ||
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-params/5.4.2/junit-jupiter-params-5.4.2.jar!/" /> | ||
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-engine/5.4.2/junit-jupiter-engine-5.4.2.jar!/" /> | ||
<root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-engine/1.4.2/junit-platform-engine-1.4.2.jar!/" /> | ||
</CLASSES> | ||
<JAVADOC /> | ||
<SOURCES /> | ||
</library> | ||
</orderEntry> | ||
</component> | ||
</module> |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import java.io.DataInputStream; | ||
import java.io.DataOutputStream; | ||
import java.io.IOError; | ||
import java.io.IOException; | ||
import java.net.Socket; | ||
import java.sql.*; | ||
|
||
public class ClientHandler extends Thread{ | ||
final Socket socket; | ||
final DataInputStream input; | ||
final DataOutputStream output; | ||
DBConn dbc = DBConn.getDBConn(); | ||
Connection con = dbc.getConnection(); | ||
|
||
public ClientHandler(Socket socket, DataInputStream input, DataOutputStream output) { | ||
this.socket = socket; | ||
this.input = input; | ||
this.output = output; | ||
} | ||
|
||
@Override | ||
public void run() { | ||
while(true) { | ||
try { | ||
output.writeUTF(""); | ||
String received = input.readUTF(); | ||
if (received.equals("Done") || this.con.isClosed()) { | ||
this.socket.close(); | ||
System.out.println("Connection terminated"); | ||
break; | ||
} | ||
System.out.println(received); | ||
PhoneNumberData pnd = new PhoneNumberData(received); | ||
String sql = "SELECT * FROM contacts WHERE phoneNumber='" + pnd.getPhoneNumber() + "'"; | ||
Statement stmt = con.createStatement(); | ||
ResultSet rs = stmt.executeQuery(sql); | ||
while (rs.next()) { | ||
if (rs.getString(5).equals(pnd.getPhoneNumber())) { | ||
output.writeUTF("OCUPAT"); | ||
break; | ||
} | ||
} | ||
if(!rs.isFirst()) { | ||
output.writeUTF("DISPONIBIL"); | ||
} | ||
} | ||
catch (IOException | SQLException i) { | ||
System.out.println(i); | ||
break; | ||
} | ||
} | ||
try{ | ||
this.input.close(); | ||
this.output.close(); | ||
} | ||
catch (IOException i){ | ||
System.out.println(i); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import java.sql.Connection; | ||
import java.sql.DriverManager; | ||
|
||
public class DBConn { | ||
private Connection con; | ||
private static DBConn dbc; | ||
private DBConn() { | ||
try { | ||
Class.forName("com.mysql.jdbc.Driver"); | ||
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/contactdb","root", ""); | ||
} catch (Exception e) { | ||
System.out.println(e); | ||
} | ||
} | ||
|
||
public static DBConn getDBConn() { | ||
if(dbc == null) { | ||
dbc = new DBConn(); | ||
} | ||
return dbc; | ||
} | ||
|
||
public Connection getConnection() { | ||
return con; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import java.io.IOException; | ||
|
||
public class Main { | ||
public static void main(String[] args) throws IOException { | ||
Server server = new Server(5000); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
public class PhoneNumberData { | ||
String phoneNumber; | ||
|
||
public PhoneNumberData(String phoneNumber) { | ||
this.phoneNumber = phoneNumber; | ||
} | ||
|
||
public String getPhoneNumber() { | ||
return phoneNumber; | ||
} | ||
|
||
public void setPhoneNumber(String phoneNumber) { | ||
this.phoneNumber = phoneNumber; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import org.junit.jupiter.api.Assertions; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
class PhoneNumberDataTest { | ||
|
||
@org.junit.jupiter.api.Test | ||
void getPhoneNumber() { | ||
PhoneNumberData pnd = new PhoneNumberData("0749091217"); | ||
String expected = pnd.getPhoneNumber(); | ||
String actual = "0722683155"; | ||
Assertions.assertEquals(expected, actual); | ||
} | ||
|
||
@org.junit.jupiter.api.Test | ||
void setPhoneNumber() { | ||
PhoneNumberData pnd = new PhoneNumberData("0749091217"); | ||
pnd.phoneNumber = "0771519112"; | ||
pnd.setPhoneNumber(pnd.phoneNumber); | ||
Assertions.assertEquals(pnd.getPhoneNumber(), pnd.phoneNumber); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import java.io.DataInputStream; | ||
import java.io.DataOutputStream; | ||
import java.io.IOException; | ||
import java.net.ServerSocket; | ||
import java.net.Socket; | ||
|
||
public class Server { | ||
public Server(int port) throws IOException { | ||
ServerSocket serverSocket = new ServerSocket(port); | ||
System.out.println("Contacts App"); | ||
System.out.println("Server started"); | ||
System.out.println("Waiting for a client"); | ||
while (true) { | ||
Socket socket = null; | ||
try { | ||
socket = serverSocket.accept(); | ||
System.out.println("A new client is connected: " + socket); | ||
|
||
DataInputStream inputStream = new DataInputStream(socket.getInputStream()); | ||
DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream()); | ||
System.out.println("Assigning new thread for this client"); | ||
ClientHandler clientHandler = new ClientHandler(socket, inputStream, outputStream); | ||
clientHandler.start(); | ||
} | ||
catch (IOException i) { | ||
socket.close(); | ||
System.out.println(i); | ||
} | ||
} | ||
} | ||
} |