package com.studentmanager.dao;
import com.studentmanager.bean.NoteBean;
import com.studentmanager.util.DatabaseUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
public class NoteDao {
public static boolean addNote(String title, String inputText) {
Connection connection = DatabaseUtil.getConnectDB();
String sql = "INSERT INTO `note` (`title`, `note`) VALUES (?, ?)";
int values;
PreparedStatement preparedStatement = null;
try {
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, title);
preparedStatement.setString(2, inputText);
values = preparedStatement.executeUpdate();
return values != 0;
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
// public static boolean queryNote(String id) {
// Connection connection = DatabaseUtil.getConnectDB();
// String sql = "SELECT * FROM `note` where id=?";
// ResultSet queryResultSet;
//
// PreparedStatement preparedStatement;
// try {
// preparedStatement = connection.prepareStatement(sql);
// preparedStatement.setString(1, id);
// queryResultSet = preparedStatement.executeQuery();
// return queryResultSet.next();
// } catch (SQLException e) {
// e.printStackTrace();
// return false;
// }
// }
public static ArrayList queryNote(String id) {
ArrayList noteArrayList = new ArrayList<>();
Connection connection = DatabaseUtil.getConnectDB();
String sql = "SELECT * FROM `note` where id=?";
ResultSet queryResultSet;
PreparedStatement preparedStatement;
try {
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, id);
queryResultSet = preparedStatement.executeQuery();
while (queryResultSet.next()) {
NoteBean noteBean = new NoteBean();
noteBean.setId(queryResultSet.getString("id"));
noteBean.setUid(queryResultSet.getString("uid"));
noteBean.setTitle(queryResultSet.getString("title"));
noteBean.setNote(queryResultSet.getString("note"));
noteArrayList.add(noteBean);
}
queryResultSet.close();
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
return noteArrayList;
}
public static ArrayList noteList() {
ArrayList noteArrayList = new ArrayList<>();
Connection connection = DatabaseUtil.getConnectDB();
String sql = "SELECT * FROM `note`";
ResultSet queryResultSet;
PreparedStatement preparedStatement;
try {
preparedStatement = connection.prepareStatement(sql);
queryResultSet = preparedStatement.executeQuery();
while (queryResultSet.next()) {
NoteBean noteBean = new NoteBean();
noteBean.setId(queryResultSet.getString("id"));
noteBean.setUid(queryResultSet.getString("uid"));
noteBean.setTitle(queryResultSet.getString("title"));
noteBean.setNote(queryResultSet.getString("note"));
noteArrayList.add(noteBean);
}
queryResultSet.close();
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
return noteArrayList;
}
public static boolean deleteNote(String id) {
Connection connection = DatabaseUtil.getConnectDB();
String sql = "DELETE FROM `note` WHERE id=?";
int result;
PreparedStatement preparedStatement;
try {
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, id);
result = preparedStatement.executeUpdate();
return result != 0;
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
public static boolean updateNote(String id, String title, String inputText) {
Connection connection = DatabaseUtil.getConnectDB();
String sql = "UPDATE `note` SET `title` = ?, `note` = ? WHERE `id` = ?";
int result;
PreparedStatement preparedStatement;
try {
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, title);
preparedStatement.setString(2, inputText);
preparedStatement.setString(3, id);
result = preparedStatement.executeUpdate();
return result != 0;
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
}