import java.sql.*;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
public class StudentDAO {
private String driver;
private String url;
private String userName;
private String password;
Connection con;
Statement stmt;
public void setDriver(String driver)
{
this.driver = driver;
}
public void setUrl(String url) { this.url = url; }
public void setUserName(String userName)
{
this.userName = userName;
}
public void setPassword(String password)
{
this.password = password;
}
@PostConstruct
public void init()
throws SQLException, ClassNotFoundException
{
createStudentDBConnection();
}
public void createStudentDBConnection()
throws ClassNotFoundException, SQLException
{
Class.forName(driver);
con = DriverManager.getConnection(url, userName,
password);
stmt = con.createStatement();
}
public void selectAllRows()
throws ClassNotFoundException, SQLException
{
System.out.println("Retrieving all student data..");
ResultSet rs = stmt.executeQuery(
"SELECT * FROM studentdb.hostelstudentinfo");
while (rs.next()) {
int studentId = rs.getInt(1);
String studentName = rs.getString(2);
double hostelFees = rs.getDouble(3);
String foodType = rs.getString(4);
System.out.println(studentId + " " + studentName
+ " " + hostelFees + " "
+ foodType);
}
}
public void deleteStudentRecord(int studentId)
throws ClassNotFoundException, SQLException
{
System.out.println("Delete student data..");
stmt.executeUpdate(
"delete from studentdb.hostelstudentinfo where Student_id = "
+ studentId);
System.out.println("Record Deleted with the ID "
+ studentId);
}
public void closeConnection() throws SQLException
{
con.close();
}
@PreDestroy public void destroy() throws SQLException
{
closeConnection();
}
}