-
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.
- Loading branch information
1 parent
822ceda
commit e11dca1
Showing
4 changed files
with
117 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
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,37 @@ | ||
package com.napier.sem; | ||
|
||
public class Department { | ||
public String dept_name; | ||
public String dept_no; | ||
public Employee manager; | ||
|
||
public Department(String dept_name, String dept_no, Employee manager) { | ||
this.dept_name = dept_name; | ||
this.dept_no = dept_no; | ||
this.manager = manager; | ||
} | ||
|
||
public String getDept_name() { | ||
return dept_name; | ||
} | ||
|
||
public void setDept_name(String dept_name) { | ||
this.dept_name = dept_name; | ||
} | ||
|
||
public String getDept_no() { | ||
return dept_no; | ||
} | ||
|
||
public void setDept_no(String dept_no) { | ||
this.dept_no = dept_no; | ||
} | ||
|
||
public Employee getManager() { | ||
return manager; | ||
} | ||
|
||
public void setManager(Employee manager) { | ||
this.manager = manager; | ||
} | ||
} |
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,44 @@ | ||
package com.napier.sem; | ||
|
||
import java.sql.Connection; | ||
import java.sql.PreparedStatement; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
|
||
|
||
public class DepartmentSQL { | ||
|
||
private Connection con; | ||
|
||
public DepartmentSQL(Connection conn) { | ||
|
||
this.con = conn; | ||
} | ||
|
||
public Department getDepartment(String dept_name) { | ||
|
||
try { | ||
// Prepare SQL statement to prevent SQL injection | ||
String strSelect = "SELECT dept_no, dept_name FROM departments WHERE dept_name = ?"; | ||
PreparedStatement pstmt = con.prepareStatement(strSelect); | ||
pstmt.setString(1, dept_name); | ||
ResultSet rset = pstmt.executeQuery(); | ||
|
||
|
||
if (rset.next()) { | ||
// Here, we fetch department number as a string (like "d005") | ||
String dept_no = rset.getString("dept_no"); | ||
// Assume that fetching the manager is handled separately | ||
Employee manager = null; // Placeholder for manager if needed | ||
String dep_name = rset.getString("dept_name"); | ||
return new Department(dep_name, dept_no, manager); | ||
} else { | ||
return null; | ||
} | ||
} catch (SQLException e) { | ||
System.out.println(e.getMessage()); | ||
System.out.println("Failed to get department details"); | ||
return null; | ||
} | ||
} | ||
} |
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