Guest User

Dao

a guest
Oct 20th, 2018
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.32 KB | None | 0 0
  1. package studentinformation.dao;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10.  
  11. import studentinformation.model.ClassInfo;
  12. import studentinformation.model.StudentInfo;
  13.  
  14. public class Dao {
  15.  
  16. public static Connection getConnection()throws SQLException, ClassNotFoundException{
  17.  
  18. Connection connection=null;
  19.  
  20. Class.forName("com.mysql.jdbc.Driver");
  21. connection= DriverManager.getConnection("jdbc:mysql://localhost:3306/StudentManagement","root","root");
  22.  
  23. return connection;
  24. }
  25.  
  26. public List<ClassInfo> getClassName() throws Exception {
  27. List<ClassInfo> classInfoList=new ArrayList<ClassInfo>();
  28. Connection connection=null;
  29. try{
  30. connection=getConnection();
  31. String getName="select classId,className from classinfo";
  32. Statement statement=connection.createStatement();
  33. ResultSet rs=statement.executeQuery(getName);
  34. while(rs.next()){
  35. ClassInfo classInfo=new ClassInfo();
  36. classInfo.setClassId(rs.getInt("classId"));
  37. classInfo.setClassName(rs.getString("className"));
  38. classInfoList.add(classInfo);
  39. }
  40. }
  41. finally {
  42. connection.close();
  43. }
  44. return classInfoList;
  45. }
  46.  
  47. public int addStudentInfo(StudentInfo studentInfo) throws SQLException, ClassNotFoundException {
  48. int x=0;
  49. Connection connection=null;
  50. try{
  51. connection=getConnection();
  52. String addStudent="insert into studentinfo(studentName,birthdate,mobileNo,classId) values('"+studentInfo.getStudentName()+"','"+studentInfo.getBirthdate()+"','"+studentInfo.getMobileNo()+"','"+studentInfo.getClassId()+"')";
  53. Statement statement=connection.createStatement();
  54. x=statement.executeUpdate(addStudent);
  55. }catch (Exception e) {
  56. System.out.println(e);
  57. }
  58. finally{
  59. connection.close();
  60. }
  61. return x;
  62. }
  63.  
  64. public List<StudentInfo> listStudent(int classId) throws SQLException, ClassNotFoundException {
  65. List<StudentInfo> listStudent=new ArrayList<StudentInfo>();
  66. StudentInfo student=null;
  67. Connection connection=null;
  68. try{
  69. connection=getConnection();
  70. StringBuilder builder = new StringBuilder();
  71. builder.append("select si.studentId,si.studentName,si.birthdate,si.mobileNo,si.classId,ci.className");
  72. builder.append(" from studentinfo si ");
  73. builder.append(" inner join classinfo ci ON si.classId=ci.classId ");
  74. if(classId>0)
  75. builder.append(" where si.classId=").append(classId);
  76.  
  77. String retriveStudentList=builder.toString();
  78. Statement statement=connection.createStatement();
  79. //System.out.println(retriveStudentList);
  80. ResultSet rs=statement.executeQuery(retriveStudentList);
  81. while(rs.next()){
  82. student=new StudentInfo();
  83. student.setStudentId(rs.getInt("studentId"));
  84. student.setStudentName(rs.getString("studentName"));
  85. student.setBirthdate(rs.getString("birthdate"));
  86. student.setMobileNo(rs.getString("mobileNo"));
  87. student.setClassId(rs.getInt("classId"));
  88. student.setClassName(rs.getString("className"));
  89. listStudent.add(student);
  90. }
  91. }
  92. finally {
  93. connection.close();
  94. }
  95. return listStudent;
  96. }
  97.  
  98. public StudentInfo getStudentById(int id) throws SQLException, ClassNotFoundException {
  99. StudentInfo student=null;
  100. Connection connection=null;
  101. //List<StudentInfo> list=new ArrayList<StudentInfo>();
  102. try{
  103. connection=getConnection();
  104. String retriveUserByID="select * from studentinfo where studentId="+id;
  105.  
  106. Statement statement=connection.createStatement();
  107. ResultSet rs=statement.executeQuery(retriveUserByID);
  108. if(rs.next()){
  109.  
  110. student=new StudentInfo();
  111. student.setStudentId(rs.getInt("studentId"));
  112. student.setStudentName(rs.getString("studentName"));
  113. student.setBirthdate(rs.getString("birthdate"));
  114. student.setMobileNo(rs.getString("mobileNo"));
  115. student.setClassId(rs.getInt("classId"));
  116. return student;
  117. //list.add(student);
  118. }else{
  119. return null;
  120. }
  121. }
  122. finally{
  123. connection.close();
  124. }
  125. //return student;
  126. }
  127.  
  128. public int updateStudentInfo(StudentInfo student) throws SQLException, ClassNotFoundException {
  129. int x=0;
  130. Connection connection=null;
  131. try{
  132. connection=getConnection();
  133. String updateUser="update studentinfo set studentName='"+student.getStudentName()+"',birthdate='"+student.getBirthdate()+"',mobileNo='"+student.getMobileNo()+"',classId='"+student.getClassId()+"' where studentId='"+student.getStudentId()+"'";
  134. Statement statement=connection.createStatement();
  135. x=statement.executeUpdate(updateUser);
  136. }
  137. finally{
  138. connection.close();
  139. }
  140. return x;
  141. }
  142.  
  143. public int deleteUser(int id) throws SQLException, ClassNotFoundException {
  144. int status=0;
  145. Connection connection=null;
  146. try{
  147. connection=getConnection();
  148. //StudentInfo student=new StudentInfo();
  149.  
  150. Statement st=connection.createStatement();
  151. String deleteStudent="delete from studentinfo where studentId="+id;
  152.  
  153. status=st.executeUpdate(deleteStudent);
  154. }
  155. finally{
  156. connection.close();
  157. }
  158. return status;
  159. }
  160.  
  161. }
Add Comment
Please, Sign In to add comment