Skip to content
Open
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
29 changes: 29 additions & 0 deletions JDBC Tasks/DatabaseTask1/.gitignore
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions JDBC Tasks/DatabaseTask1/.idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions JDBC Tasks/DatabaseTask1/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions JDBC Tasks/DatabaseTask1/.idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

124 changes: 124 additions & 0 deletions JDBC Tasks/DatabaseTask1/.idea/uiDesigner.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions JDBC Tasks/DatabaseTask1/.idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions JDBC Tasks/DatabaseTask1/DatabaseTask1.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?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" exported="">
<library>
<CLASSES>
<root url="jar://$MODULE_DIR$/../../../mysql-connector.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
</component>
</module>
Binary file added JDBC Tasks/DatabaseTask1/MYSQL screenshot.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
28 changes: 28 additions & 0 deletions JDBC Tasks/DatabaseTask1/README.md
Original file line number Diff line number Diff line change
@@ -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.

<center> <img src="screenshot.PNG" width=700 length=300> </center>
<center> <img src="MYSQL screenshot.PNG" width=700 length=300> </center>


## 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**
<center><img src="class diagram.PNG" width=300 higth=300></center>
Binary file added JDBC Tasks/DatabaseTask1/class diagram.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 17 additions & 0 deletions JDBC Tasks/DatabaseTask1/db.sql
Original file line number Diff line number Diff line change
@@ -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');
Binary file added JDBC Tasks/DatabaseTask1/screenshot.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
38 changes: 38 additions & 0 deletions JDBC Tasks/DatabaseTask1/src/DBSingleton.java
Original file line number Diff line number Diff line change
@@ -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();
}
}

9 changes: 9 additions & 0 deletions JDBC Tasks/DatabaseTask1/src/Main.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
29 changes: 29 additions & 0 deletions JDBC Tasks/DatabaseTask2/.gitignore
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions JDBC Tasks/DatabaseTask2/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions JDBC Tasks/DatabaseTask2/.idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading