diff --git a/JDBC Tasks/DatabaseTask1/.gitignore b/JDBC Tasks/DatabaseTask1/.gitignore new file mode 100644 index 0000000..f68d109 --- /dev/null +++ b/JDBC Tasks/DatabaseTask1/.gitignore @@ -0,0 +1,29 @@ +### IntelliJ IDEA ### +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/JDBC Tasks/DatabaseTask1/.idea/.gitignore b/JDBC Tasks/DatabaseTask1/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/JDBC Tasks/DatabaseTask1/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/JDBC Tasks/DatabaseTask1/.idea/misc.xml b/JDBC Tasks/DatabaseTask1/.idea/misc.xml new file mode 100644 index 0000000..0548357 --- /dev/null +++ b/JDBC Tasks/DatabaseTask1/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/JDBC Tasks/DatabaseTask1/.idea/modules.xml b/JDBC Tasks/DatabaseTask1/.idea/modules.xml new file mode 100644 index 0000000..30f5d29 --- /dev/null +++ b/JDBC Tasks/DatabaseTask1/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/JDBC Tasks/DatabaseTask1/.idea/uiDesigner.xml b/JDBC Tasks/DatabaseTask1/.idea/uiDesigner.xml new file mode 100644 index 0000000..2b63946 --- /dev/null +++ b/JDBC Tasks/DatabaseTask1/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/JDBC Tasks/DatabaseTask1/.idea/vcs.xml b/JDBC Tasks/DatabaseTask1/.idea/vcs.xml new file mode 100644 index 0000000..b2bdec2 --- /dev/null +++ b/JDBC Tasks/DatabaseTask1/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/JDBC Tasks/DatabaseTask1/DatabaseTask1.iml b/JDBC Tasks/DatabaseTask1/DatabaseTask1.iml new file mode 100644 index 0000000..18369ec --- /dev/null +++ b/JDBC Tasks/DatabaseTask1/DatabaseTask1.iml @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/JDBC Tasks/DatabaseTask1/MYSQL screenshot.PNG b/JDBC Tasks/DatabaseTask1/MYSQL screenshot.PNG new file mode 100644 index 0000000..2ca0ac5 Binary files /dev/null and b/JDBC Tasks/DatabaseTask1/MYSQL screenshot.PNG differ diff --git a/JDBC Tasks/DatabaseTask1/README.md b/JDBC Tasks/DatabaseTask1/README.md new file mode 100644 index 0000000..28ddd8a --- /dev/null +++ b/JDBC Tasks/DatabaseTask1/README.md @@ -0,0 +1,28 @@ +# JDBC Tasks + +## Task 1 + + Java application for connecting to a database on MySQL Server and executing a query which selects numbers of fields from a table and prints it with any specific format, by using load a specific driver and getting the connection using DriverManager class. + +
+
+ + +## Project Description + +### Database Setup +1. Install Xampp. +2. Start Apache and MYSQL modules. +3. Open phpMyAdmin. +4. Create a new database +5. Import [SQL code](https://github.com/amira921/JobHacker-System/blob/JDBCTasks/JDBC%20Tasks/DatabaseTask1/db.sql). +6. Install [MYSQL driver](https://static.javatpoint.com/src/jdbc/mysql-connector.jar). +7. Add driver in project dependencies. + + +## Java Application +Singleton pattern used for database class. +Singleton class is created for the database that has only one instance and provides a global point of access to it, it Saves memory because the object is not created at each request, only a single instance is reused again and again. + +**UML diagram** +
\ No newline at end of file diff --git a/JDBC Tasks/DatabaseTask1/class diagram.PNG b/JDBC Tasks/DatabaseTask1/class diagram.PNG new file mode 100644 index 0000000..96056af Binary files /dev/null and b/JDBC Tasks/DatabaseTask1/class diagram.PNG differ diff --git a/JDBC Tasks/DatabaseTask1/db.sql b/JDBC Tasks/DatabaseTask1/db.sql new file mode 100644 index 0000000..67fb56d --- /dev/null +++ b/JDBC Tasks/DatabaseTask1/db.sql @@ -0,0 +1,17 @@ +CREATE TABLE `classroom` ( + `Id` int(50) NOT NULL AUTO_INCREMENT, + `Name` varchar(100) NOT NULL, + `GPA` double NOT NULL, + `Gender` enum('F','M') NOT NULL, + PRIMARY KEY(`Id`) +); + +-- +-- Dumping data for table `classroom` +-- + +INSERT INTO `classroom` (`Id`, `Name`, `GPA`, `Gender`) VALUES +(NULL, 'Amira Taha Ahmed', 4.00, 'F'), +(NULL, 'Ahmed Taha Ahmed', 3.03, 'M'), +(NULL, 'Mai Ali Mohammed', 3.99, 'F'), +(NULL, 'Nour Mohammed Ali', 2.78, 'M'); diff --git a/JDBC Tasks/DatabaseTask1/screenshot.PNG b/JDBC Tasks/DatabaseTask1/screenshot.PNG new file mode 100644 index 0000000..a7adac5 Binary files /dev/null and b/JDBC Tasks/DatabaseTask1/screenshot.PNG differ diff --git a/JDBC Tasks/DatabaseTask1/src/DBSingleton.java b/JDBC Tasks/DatabaseTask1/src/DBSingleton.java new file mode 100644 index 0000000..362fbc3 --- /dev/null +++ b/JDBC Tasks/DatabaseTask1/src/DBSingleton.java @@ -0,0 +1,38 @@ +import java.io.IOException; +import java.sql.*; +import java.util.Formatter; + +// use Singleton pattern +class DBSingleton { + private static DBSingleton connection; + private DBSingleton(){} + + public static DBSingleton getInstance(){ + if(connection == null) connection = new DBSingleton(); + return connection; + } + + public static Connection getConnection() throws SQLException, ClassNotFoundException { + Connection connection = null; + Class.forName("com.mysql.jdbc.Driver"); // loading mysql driver to establish database connection + connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/db", "root", ""); + return connection; + } + + public void viewRecords() throws SQLException, ClassNotFoundException, IOException { + Connection con = getConnection(); + CallableStatement ps = con.prepareCall("{call GetStudents()}"); + ResultSet rs = ps.executeQuery(); + + System.out.println("Classroom Info"); + Formatter format = new Formatter(); + format.format("%20s %20s %20s %20s\n", "Student Id", "Student Name", "Student Gender" , "Student GPA"); + while (rs.next()) { + format.format("%20s %20s %20s %20s\n", rs.getInt(1), rs.getString(2), rs.getString(4) , rs.getDouble(3)); + } + System.out.println(format); + + con.close(); + } +} + diff --git a/JDBC Tasks/DatabaseTask1/src/Main.java b/JDBC Tasks/DatabaseTask1/src/Main.java new file mode 100644 index 0000000..2ebdc02 --- /dev/null +++ b/JDBC Tasks/DatabaseTask1/src/Main.java @@ -0,0 +1,9 @@ +import java.io.IOException; +import java.sql.SQLException; + +public class Main { + public static void main(String[] args) throws SQLException, ClassNotFoundException, IOException { + DBSingleton classroom = DBSingleton.getInstance(); + classroom.viewRecords(); + } +} \ No newline at end of file diff --git a/JDBC Tasks/DatabaseTask2/.gitignore b/JDBC Tasks/DatabaseTask2/.gitignore new file mode 100644 index 0000000..f68d109 --- /dev/null +++ b/JDBC Tasks/DatabaseTask2/.gitignore @@ -0,0 +1,29 @@ +### IntelliJ IDEA ### +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/JDBC Tasks/DatabaseTask2/.idea/misc.xml b/JDBC Tasks/DatabaseTask2/.idea/misc.xml new file mode 100644 index 0000000..0548357 --- /dev/null +++ b/JDBC Tasks/DatabaseTask2/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/JDBC Tasks/DatabaseTask2/.idea/modules.xml b/JDBC Tasks/DatabaseTask2/.idea/modules.xml new file mode 100644 index 0000000..0a5795d --- /dev/null +++ b/JDBC Tasks/DatabaseTask2/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/JDBC Tasks/DatabaseTask2/.idea/uiDesigner.xml b/JDBC Tasks/DatabaseTask2/.idea/uiDesigner.xml new file mode 100644 index 0000000..2b63946 --- /dev/null +++ b/JDBC Tasks/DatabaseTask2/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/JDBC Tasks/DatabaseTask2/.idea/vcs.xml b/JDBC Tasks/DatabaseTask2/.idea/vcs.xml new file mode 100644 index 0000000..b2bdec2 --- /dev/null +++ b/JDBC Tasks/DatabaseTask2/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/JDBC Tasks/DatabaseTask2/.idea/workspace.xml b/JDBC Tasks/DatabaseTask2/.idea/workspace.xml new file mode 100644 index 0000000..e47b72c --- /dev/null +++ b/JDBC Tasks/DatabaseTask2/.idea/workspace.xml @@ -0,0 +1,58 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1692586476954 + + + + \ No newline at end of file diff --git a/JDBC Tasks/DatabaseTask2/DatabaseTask2.iml b/JDBC Tasks/DatabaseTask2/DatabaseTask2.iml new file mode 100644 index 0000000..18369ec --- /dev/null +++ b/JDBC Tasks/DatabaseTask2/DatabaseTask2.iml @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/JDBC Tasks/DatabaseTask2/Java app screenshot.PNG b/JDBC Tasks/DatabaseTask2/Java app screenshot.PNG new file mode 100644 index 0000000..81109fc Binary files /dev/null and b/JDBC Tasks/DatabaseTask2/Java app screenshot.PNG differ diff --git a/JDBC Tasks/DatabaseTask2/MYSQL screenshot.PNG b/JDBC Tasks/DatabaseTask2/MYSQL screenshot.PNG new file mode 100644 index 0000000..60de097 Binary files /dev/null and b/JDBC Tasks/DatabaseTask2/MYSQL screenshot.PNG differ diff --git a/JDBC Tasks/DatabaseTask2/README.md b/JDBC Tasks/DatabaseTask2/README.md new file mode 100644 index 0000000..8721e4d --- /dev/null +++ b/JDBC Tasks/DatabaseTask2/README.md @@ -0,0 +1,29 @@ +# JDBC Tasks + +## Task 2 + +Java application for Creating a table with name "Employee", with fields: Id, F_Name, L_Name, Sex, Age, Address, Phone Number, Vaction_Balance "30 days for each employees", insert 5 rows - at least- with different data using PreparedStatement object, using Batching to do the following related to the above database: +1. Modify the Vacation_Balance of employees over 45 years to be increased to 45 days rather than 30. +2. For those employees, title the F_Name with Mr/Mrs. + +
+
+ + +## Project Description + +### Database Setup +1. Install Xampp. +2. Start Apache and MYSQL modules. +3. Open phpMyAdmin. +4. Create a new database +4. Install [MYSQL driver](https://static.javatpoint.com/src/jdbc/mysql-connector.jar). +6. Add driver in project dependencies. + + +## Java Application +Singleton pattern used for database class. +Singleton class is created for the database that has only one instance and provides a global point of access to it, it Saves memory because the object is not created at each request, only a single instance is reused again and again. + +**UML diagram** +
diff --git a/JDBC Tasks/DatabaseTask2/UML Diagram.PNG b/JDBC Tasks/DatabaseTask2/UML Diagram.PNG new file mode 100644 index 0000000..86ce23b Binary files /dev/null and b/JDBC Tasks/DatabaseTask2/UML Diagram.PNG differ diff --git a/JDBC Tasks/DatabaseTask2/src/Employee.java b/JDBC Tasks/DatabaseTask2/src/Employee.java new file mode 100644 index 0000000..7c8eeae --- /dev/null +++ b/JDBC Tasks/DatabaseTask2/src/Employee.java @@ -0,0 +1,63 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; + +public class Employee { + private String firstName , lastName, address, sex; + private int age, phone; + + public Employee(){} + public Employee(String firstName , String lastName, String address, String sex, int age, int phone){ + this.address = address; + this.age = age; + this.sex = sex; + this.lastName = lastName; + this.firstName = firstName; + this.phone = phone; + } + + public String getFirstName() { + return firstName; + } + public String getLastName() { + return lastName; + } + public String getAddress() { + return address; + } + public String getSex() { + return sex; + } + public int getPhone() { + return phone; + } + public int getAge() { + return age; + } + + public Employee readEmployeeData() throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + + System.out.println("\nAdding New Employee..."); + + System.out.print("Employee First Name: "); + String firstName = br.readLine(); + + System.out.print("Employee Last Name: "); + String lastName = br.readLine(); + + System.out.print("Employee Sex (F: Female / M: Male): "); + String sex = br.readLine(); + + System.out.print("Employee Age: "); + int age = Integer.parseInt(br.readLine()); + + System.out.print("Employee Address: "); + String address = br.readLine(); + + System.out.print("Employee Phone Number: "); + int phone = Integer.parseInt(br.readLine()); + + return new Employee(firstName,lastName,address,sex,age,phone); + } +} diff --git a/JDBC Tasks/DatabaseTask2/src/EmployeeDB.java b/JDBC Tasks/DatabaseTask2/src/EmployeeDB.java new file mode 100644 index 0000000..2bf788c --- /dev/null +++ b/JDBC Tasks/DatabaseTask2/src/EmployeeDB.java @@ -0,0 +1,70 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.sql.*; + +public class EmployeeDB{ + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + public void createTable() throws SQLException, ClassNotFoundException { + Connection con = SingletonDBConnection.getConnection(); + Statement st = con.createStatement(); + String employeeTable = "CREATE TABLE `employee` (`Id` int(11) NOT NULL AUTO_INCREMENT, `F_Name` varchar(20) NOT NULL," + + "`L_Name` varchar(20) NOT NULL,`Sex` enum('F','M') NOT NULL,`Age` int(2) NOT NULL,`Address` varchar(150) NOT NULL," + + "`Phone_Number` int(11) NOT NULL, `Vacation_Balance` int(2) NOT NULL Default '30',PRIMARY KEY(`Id`));"; + st.executeUpdate(employeeTable); + System.out.println("Employee Table Created!"); + con.close(); + } + private boolean insertNewRecord() throws IOException { + System.out.print("\nDo you want to add more records (Y/N) ?"); + String check = br.readLine(); + if(check.equals("N") || check.equals("n")) return true; + return false; + } + public void insertRecords() throws SQLException, ClassNotFoundException, IOException { + Connection con = SingletonDBConnection.getConnection(); + String query = "INSERT INTO `employee`(`F_Name`, `L_Name`, `Sex`, `Age`, `Address`, `Phone_Number`) VALUES (?,?,?,?,?,?)"; + PreparedStatement pr = con.prepareStatement(query); + while(true){ + Employee employee = new Employee().readEmployeeData(); + pr.setString(1,employee.getFirstName()); + pr.setString(2,employee.getLastName()); + pr.setString(3,employee.getSex()); + pr.setInt(4,employee.getAge()); + pr.setString(5, employee.getAddress()); + pr.setInt(6,employee.getPhone()); + pr.addBatch(); + + if(insertNewRecord()) break; + } + pr.executeBatch(); + con.close(); + } + public void updateVacationBalance() throws SQLException, ClassNotFoundException { + Connection con = SingletonDBConnection.getConnection(); + Statement s = con.createStatement(); + ResultSet rs = s.executeQuery("SELECT `Vacation_Balance` FROM `employee` WHERE `age` > 45;"); + PreparedStatement ps = con.prepareStatement("UPDATE `employee` SET `Vacation_Balance`=? WHERE `age` > 45;"); + while(rs.next()){ + ps.setInt(1, 45); + ps.addBatch(); + } + ps.executeBatch(); + con.close(); + System.out.println("Vacation Balance updated from 30 days to 45 days\nfor all employees with age greater than 45"); + } + + public void updateEmployeesTitle() throws SQLException, ClassNotFoundException { + Connection con = SingletonDBConnection.getConnection(); + Statement s = con.createStatement(); + ResultSet rs = s.executeQuery("SELECT `F_Name`,`Sex` FROM `employee`"); + PreparedStatement ps = con.prepareStatement("UPDATE `employee` SET `F_Name`=?"); + while(rs.next()){ + ps.setString(1, rs.getString(2).equals("F")? "Mrs. "+rs.getString(1) : "Mr. "+rs.getString(1)); + ps.addBatch(); + } + ps.executeBatch(); + con.close(); + System.out.println("Title added for all employees(Mr./Mrs.)"); + } +} diff --git a/JDBC Tasks/DatabaseTask2/src/Main.java b/JDBC Tasks/DatabaseTask2/src/Main.java new file mode 100644 index 0000000..6b664f3 --- /dev/null +++ b/JDBC Tasks/DatabaseTask2/src/Main.java @@ -0,0 +1,12 @@ +import java.io.IOException; +import java.sql.SQLException; + +public class Main { + public static void main(String[] args) throws SQLException, ClassNotFoundException, IOException { + EmployeeDB employeeOperations = new EmployeeDB(); + employeeOperations.createTable(); + employeeOperations.insertRecords(); + employeeOperations.updateVacationBalance(); + employeeOperations.updateEmployeesTitle(); + } +} \ No newline at end of file diff --git a/JDBC Tasks/DatabaseTask2/src/SingletonDBConnection.java b/JDBC Tasks/DatabaseTask2/src/SingletonDBConnection.java new file mode 100644 index 0000000..3d07452 --- /dev/null +++ b/JDBC Tasks/DatabaseTask2/src/SingletonDBConnection.java @@ -0,0 +1,14 @@ +import java.sql.*; +class SingletonDBConnection { + private static SingletonDBConnection connection; + private SingletonDBConnection(){} + public static SingletonDBConnection getInstance(){ + if(connection == null) connection = new SingletonDBConnection(); + return connection; + } + public static Connection getConnection() throws ClassNotFoundException, SQLException { + Class.forName("com.mysql.jdbc.Driver"); + Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", ""); + return connection; + } +} diff --git a/README.md b/README.md deleted file mode 100644 index a974d78..0000000 --- a/README.md +++ /dev/null @@ -1,2 +0,0 @@ -# JobHacker-System -This repo for JobHacker Community tasks.