Guest User

daoimpl

a guest
Oct 19th, 2018
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.40 KB | None | 0 0
  1. package 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 model.GetInfo;
  12. import model.StudentInfo;
  13.  
  14. public class DaoIMPL implements 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<GetInfo> getClassName() throws SQLException, ClassNotFoundException {
  27. List<GetInfo> getClassName=new ArrayList<GetInfo>();
  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. GetInfo classInfo=new GetInfo();
  36. classInfo.setClassId(rs.getInt("classId"));
  37. classInfo.setClassName(rs.getString("className"));
  38. getClassName.add(classInfo);
  39. }
  40. }
  41. finally {
  42. connection.close();
  43. }
  44. return getClassName;
  45. }
  46.  
  47. @Override
  48. public int addStudentInfo(StudentInfo studentInfo) throws SQLException, ClassNotFoundException {
  49. int x=0;
  50. Connection connection=null;
  51. try{
  52. connection=getConnection();
  53. String addStudent="insert into studentinfo(studentName,birthdate,mobileNo,classId) values('"+studentInfo.getStudentName()+"','"+studentInfo.getBirthdate()+"','"+studentInfo.getMobileNo()+"','"+studentInfo.getClassId()+"')";
  54. Statement statement=connection.createStatement();
  55. x=statement.executeUpdate(addStudent);
  56. }catch (Exception e) {
  57. System.out.println(e);
  58. }
  59. finally{
  60. connection.close();
  61. }
  62. return x;
  63. }
  64.  
  65. @Override
  66. public List<StudentInfo> listStudent(int classId) throws SQLException, ClassNotFoundException {
  67. List<StudentInfo> listStudent=new ArrayList<StudentInfo>();
  68. StudentInfo student=null;
  69. Connection connection=null;
  70. try{
  71. connection=getConnection();
  72. StringBuilder builder = new StringBuilder();
  73. builder.append("select si.studentId,si.studentName,si.birthdate,si.mobileNo,si.classId,ci.className");
  74. builder.append(" from studentinfo si ");
  75. builder.append(" inner join classinfo ci ON si.classId=ci.classId ");
  76. if(classId>0)
  77. builder.append(" where si.classId=").append(classId);
  78.  
  79. String retriveStudentList=builder.toString();
  80. Statement statement=connection.createStatement();
  81. //System.out.println(retriveStudentList);
  82. ResultSet rs=statement.executeQuery(retriveStudentList);
  83. while(rs.next()){
  84. student=new StudentInfo();
  85. student.setStudentId(rs.getInt("studentId"));
  86. student.setStudentName(rs.getString("studentName"));
  87. student.setBirthdate(rs.getString("birthdate"));
  88. student.setMobileNo(rs.getString("mobileNo"));
  89. student.setClassId(rs.getInt("classId"));
  90. student.setClassName(rs.getString("className"));
  91. listStudent.add(student);
  92. }
  93. }
  94. finally {
  95. connection.close();
  96. }
  97. return listStudent;
  98. }
  99.  
  100. @Override// if id is matches then return data otherwise return null
  101. public StudentInfo getStudentById(int id) throws SQLException, ClassNotFoundException {
  102. StudentInfo student=null;
  103. Connection connection=null;
  104. List<StudentInfo> list=new ArrayList<StudentInfo>();
  105. try{
  106. connection=getConnection();
  107. String retriveUserByID="select * from studentinfo where studentId="+id;
  108.  
  109. Statement statement=connection.createStatement();
  110. ResultSet rs=statement.executeQuery(retriveUserByID);
  111. if(rs.next()){
  112.  
  113. student=new StudentInfo();
  114. student.setStudentId(rs.getInt("studentId"));
  115. student.setStudentName(rs.getString("studentName"));
  116. student.setBirthdate(rs.getString("birthdate"));
  117. student.setMobileNo(rs.getString("mobileNo"));
  118. student.setClassId(rs.getInt("classId"));
  119. return student;
  120. //list.add(student);
  121. }else{
  122. return null;
  123. }
  124. }
  125. finally{
  126. connection.close();
  127. }
  128. //return student;
  129. }
  130.  
  131. @Override
  132. public int updateStudentInfo(StudentInfo student) throws SQLException, ClassNotFoundException {
  133. int x=0;
  134. Connection connection=null;
  135. try{
  136. connection=getConnection();
  137. String updateUser="update studentinfo set studentName='"+student.getStudentName()+"',birthdate='"+student.getBirthdate()+"',mobileNo='"+student.getMobileNo()+"',classId='"+student.getClassId()+"' where studentId='"+student.getStudentId()+"'";
  138. Statement statement=connection.createStatement();
  139. x=statement.executeUpdate(updateUser);
  140. }
  141. finally{
  142. connection.close();
  143. }
  144. return x;
  145. }
  146.  
  147. @Override
  148. public int deleteUser(int id) throws SQLException, ClassNotFoundException {
  149. int status=0;
  150. Connection connection=null;
  151. try{
  152. connection=getConnection();
  153. StudentInfo student=new StudentInfo();
  154.  
  155. Statement st=connection.createStatement();
  156. String deleteStudent="delete from studentinfo where studentId="+id;
  157.  
  158. status=st.executeUpdate(deleteStudent);
  159. }
  160. finally{
  161. connection.close();
  162. }
  163. return status;
  164. }
  165. }
Add Comment
Please, Sign In to add comment