DataManager:public class DataManager {
private Connection connection;
private PreparedStatement preparedStatement;
private ResultSet resultSet;
public void connect() throws ClassNotFoundException, SQLException {
String username = "root";
String password = "1234";
String URL = "jdbc:mysql://localhost:3306/developia";
String driverName = "com.mysql.jdbc.Driver";
Class.forName(driverName);
connection = DriverManager.getConnection(URL, username, password);
}
public void disconnect() throws SQLException{
if(resultSet != null){
resultSet.close();
}
if(preparedStatement != null){
preparedStatement.close();
}
if(connection != null){
connection.close();
}
}
public List<Student> findall() throws SQLException{
String query = "select * from students LEFT JOIN university on university.id = university.id where status = 1";
List<Student> students = new ArrayList<>();
try {
connect();
preparedStatement = connection.prepareStatement(query);
resultSet = preparedStatement.executeQuery();
while(resultSet.next()){
University university = new University();
Student student = new Student();
student.setId(resultSet.getLong(1));
student.setName(resultSet.getString(2));
student.setSurname(resultSet.getString(3));
student.setCourse(resultSet.getInt(5));
student.setBday(resultSet.getDate(6));
student.setDone(resultSet.getString(7));
university.setId(resultSet.getInt(8));
university.setName(resultSet.getString(9));
student.setUniversity(university);
students.add(student);
}
} catch (Exception e) {
e.printStackTrace();
}
finally {
disconnect();
}
return students;
}
public void addStudent(Student student){
String query = "insert into developia.students (name, surname ,university_id, course,bday) "
+ "values (?,?,?,?,?) ";
try {
connect();
preparedStatement = connection.prepareStatement(query);
preparedStatement.setString(1, student.getName());
preparedStatement.setString(2, student.getSurname());
preparedStatement.setInt(3, student.getUniversity().getId());
preparedStatement.setInt(4, student.getCourse());
preparedStatement.setDate(5, (Date) student.getBday());
preparedStatement.execute();
}
catch(Exception e) {
e.printStackTrace(System.err);
} finally{
try {
disconnect();
} catch (SQLException e) {
e.printStackTrace(System.err);
}
}
}
public void updateStudent(Student student){
String query ="update developia.students set name = ? , "
+ "surname = ? , "
+ "university_id = ?, "
+ "course = ?, "
+"bday = ? "
//+"taskstatus = ?"
+ "where id = ?";
try {
connect();
preparedStatement = connection.prepareStatement(query);
preparedStatement.setString(1, student.getName());
preparedStatement.setString(2, student.getSurname());
preparedStatement.setInt(3, student.getUniversity().getId());
preparedStatement.setInt(4, student.getCourse());
preparedStatement.setDate(5, (Date) student.getBday());
//preparedStatement.setString(7, student.getDone());
preparedStatement.setLong(6, student.getId());
preparedStatement.execute();
}
catch(Exception e) {
e.printStackTrace(System.err);
} finally{
try {
disconnect();
} catch (SQLException e) {
e.printStackTrace(System.err);
}
}
}
public List findAllUniversity() throws SQLException{
String query = “select * from university”;
List universities = new ArrayList<>();
try {
connect();
preparedStatement = connection.prepareStatement(query);
resultSet = preparedStatement.executeQuery();
while(resultSet.next()){
University university = new University();
university.setId(resultSet.getInt(1));
university.setName(resultSet.getString(2));
universities.add(university);
}
} catch(ClassNotFoundException | SQLException e) {
e.printStackTrace(System.err);
} finally{
disconnect();
}
return universities;
}
public void deleteStudent(long s){
String query = "update developia.students set "
+ "status = 0 "
+ "where id = ? ";
try {
connect();
preparedStatement = connection.prepareStatement(query);
preparedStatement.setLong(1, s);
preparedStatement.execute();
}
catch(Exception e) {
e.printStackTrace(System.err);
} finally{
try {
disconnect();
} catch (SQLException ex) {
ex.printStackTrace(System.err);
}
}
}
public void done(int id) throws SQLException{
String query = "update developia.students set "
+ "taskstatus = 'Done' "
+ "where id = ? ";
try {
connect();
preparedStatement = connection.prepareStatement(query);
preparedStatement.setLong(1, id);
preparedStatement.execute();
}
catch(Exception e) {
e.printStackTrace(System.err);
} finally{
try {
disconnect();
} catch (SQLException ex) {
ex.printStackTrace(System.err);
}
}
}
public List<Student> find(Map find) throws SQLException{
String query = "select * from students LEFT JOIN university on students.university_id = university.id where status = 1 and"
+ " (students.name LIKE '%"
+ find.get("name") + "%' and students.surname LIKE '%" + find.get("surname") + "%' )";
List<Student> students = new ArrayList<>();
try {
connect();
preparedStatement = connection.prepareStatement(query);
resultSet = preparedStatement.executeQuery();
while(resultSet.next()){
University university = new University();
Student student = new Student();
student.setId(resultSet.getLong(1));
student.setName(resultSet.getString(2));
student.setSurname(resultSet.getString(3));
student.setCourse(resultSet.getInt(5));
student.setBday(resultSet.getDate(7));
student.setDone(resultSet.getString(8));
//university.setId(resultSet.getInt(8));
university.setName(resultSet.getString(10));
student.setUniversity(university);
students.add(student);
}
} catch (Exception e) {
e.printStackTrace();
}
finally {
disconnect();
}
return students;
}
}
STviewController:
private DataManager dataManager;
@FXML
TableView<StudentView> studentsTableView;
@FXML
TableColumn<StudentView, String> nameCol, surnameCol,universityCol, statusCol,tnameCol;
@FXML
TableColumn<StudentView, Integer> courseCol;
@FXML
TableColumn<StudentView, Date> bdayCol;
@FXML
TextField nameField;
@FXML
TextField surnameField;
@FXML
ComboBox<University> universityCMB;
@FXML
TextField courseField;
@FXML
Button delete;
@FXML
DatePicker bdayField;
@FXML
CheckBox findId;
@FXML
public void save(){
try {
StudentView selectedStudent = studentsTableView.getSelectionModel().getSelectedItem();
java.sql.Date birthDate = java.sql.Date.valueOf(bdayField.getValue());
if(selectedStudent == null){
Student s = new Student(0,
nameField.getText(),
surnameField.getText(),
universityCMB.getValue(),
birthDate,
Integer.parseInt(courseField.getText()));
dataManager.addStudent(s);
}
else {
Student s = new Student(selectedStudent.getId(),
nameField.getText(),
surnameField.getText(),
universityCMB.getValue(),
birthDate,
Integer.parseInt(courseField.getText()));
dataManager.updateStudent(s);
}
} catch (Exception e) {
e.printStackTrace(System.err);
}
finally{
fillTable();
emptyFields();
}
}
@FXML
public void find() throws SQLException{
if(findId.isSelected()){
Map<String, String> map = new HashMap<String, String>();
map.put("name", nameField.getText());
map.put("surname",surnameField.getText());
List students = dataManager.find(map);
studentsTableView.getItems().setAll(students);
}
}
@FXML
public void deleteSelectedStudent(){
try {
StudentView selectedStudent = studentsTableView.getSelectionModel().getSelectedItem();
if (selectedStudent != null) {
long s = selectedStudent.getId();
dataManager.deleteStudent(s);
}
} catch (Exception e) {
e.printStackTrace(System.err);
} finally {
delete.setVisible(false);
emptyFields();
fillTable();
}
}
@FXML
public void done(){
try {
StudentView selectedStudent = studentsTableView.getSelectionModel().getSelectedItem();
dataManager.done((int) selectedStudent.getId());
} catch (Exception e) {
e.printStackTrace(System.err);
}
finally{
fillTable();
emptyFields();
}
}
@FXML
public void onStudentSelect(){
StudentView selectedStudent = studentsTableView.getSelectionModel().getSelectedItem();
if(selectedStudent != null){
LocalDate birthadteFormat = new java.sql.Date(selectedStudent.getBday().getTime()).toLocalDate();
nameField.setText(selectedStudent.getName());
surnameField.setText(selectedStudent.getSurname());
universityCMB.setValue(selectedStudent.getUniversity());
courseField.setText(String.valueOf(selectedStudent.getCourse()));
bdayField.setValue(birthadteFormat);
delete.setVisible(true);
}
else {
emptyFields();
delete.setVisible(false);
}
}
@Override
public void initialize(URL url, ResourceBundle rb) {
dataManager = new DataManager();
nameCol.setCellValueFactory(new PropertyValueFactory<>("name"));
surnameCol.setCellValueFactory(new PropertyValueFactory<>("surname"));
universityCol.setCellValueFactory(new PropertyValueFactory<>("university"));
courseCol.setCellValueFactory(new PropertyValueFactory<>("course"));
bdayCol.setCellValueFactory(new PropertyValueFactory<>("bday"));
statusCol.setCellValueFactory(new PropertyValueFactory<>("done"));
fillTable();
fillComboBox();
}
private void fillTable(){
try {
List<Student> students = dataManager.findall();
List<StudentView> studentViews = students
.stream()
.map(student -> new StudentView(student))
.collect(Collectors.toList());
System.out.println(studentViews);
studentsTableView.getItems().setAll(studentViews);
} catch (SQLException ex) {
ex.printStackTrace();
}
}
private void fillComboBox() {
try {
List<University> universities = dataManager.findAllUniversity();
for (University university : universities) {
universityCMB.getItems().add(university);
}
} catch (SQLException e) {
e.printStackTrace(System.err);
}
}
private void emptyFields() {
nameField.setText("");
surnameField.setText("");
universityCMB.setValue(null);
courseField.setText("");
}
}
Student:
*/
public class Student {
private long id;
private String name;
private String surname;
private University university;
private Date bday;
private String done;
private int course;
public void setDone(String done) {
this.done = done;
}
//konstruktor yarat, getter setter ele, tostring ele
public Student(){
}
public int getCourse() {
return course;
}
public void setCourse(int course) {
this.course = course;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
public University getUniversity() {
return university;
}
public void setUniversity(University university) {
this.university = university;
}
public Date getBday() {
return bday;
}
public void setBday(Date bday) {
this.bday = bday;
}
public String getDone() {
return done;
}
public void setStatus(String done) {
this.done = done;
}
@Override
public String toString() {
return "Student{" + "id=" + id + ", name=" + name + ", surname=" + surname + ", university=" + university + ", bday=" + bday + ", done=" + done + ", course=" + course + '}';
}
public Student(long id, String name, String surname, University university, Date bday, String done, int course) {
this.id = id;
this.name = name;
this.surname = surname;
this.university = university;
this.bday = bday;
this.done = done;
this.course = course;
}
public Student(long id, String name, String surname, University university, Date bday, int course) {
this.id = id;
this.name = name;
this.surname = surname;
this.university = university;
this.bday = bday;
this.course = course;
}
}
STview:
public class StudentView {
private String name;
private String surname;
private University university;
private int course;
private long id;
private Date bday;
private String done;
public String getDone() {
return done;
}
public void setDone(String done) {
this.done = done;
}
public University getUniversity() {
return university;
}
public void setUniversity(University university) {
this.university = university;
}
public Date getBday() {
return bday;
}
public void setBday(Date bday) {
this.bday = bday;
}
public StudentView(Student student) {
this.name = student.getName();
this.surname = student.getSurname();
this.university = student.getUniversity();
this.course = student.getCourse();
this.id = student.getId();
this.bday = student.getBday();
this.done = student.getDone();
}
public Student toStudent(){
return new Student(id,name, surname, university,bday, done,course );
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public StudentView(){
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
public int getCourse() {
return course;
}
public void setCourse(int course) {
this.course = course;
}
@Override
public String toString() {
return "StudentView{" + "name=" + name + ", surname=" + surname + ", university=" + university + ", course=" + course + ", id=" + id + ", bday=" + bday + ", done=" + done + '}';
}
}
esas:
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
DataManager dt = new DataManager();
dt.connect();
System.out.println("Ugurlu");
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}