Guest User

dao-ajax

a guest
Oct 31st, 2018
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.78 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. import java.util.Map;
  11.  
  12. import studentinformation.model.ClassInfo;
  13. import studentinformation.model.StudentInfo;
  14.  
  15. public class Dao {
  16.  
  17. public static Connection getConnection()throws SQLException, ClassNotFoundException{
  18.  
  19. Connection connection=null;
  20.  
  21. Class.forName("com.mysql.jdbc.Driver");
  22. connection= DriverManager.getConnection("jdbc:mysql://localhost:3306/StudentManagement","root","root");
  23.  
  24. return connection;
  25. }
  26.  
  27. public List<ClassInfo> getClassName() throws Exception {
  28. List<ClassInfo> classInfoList=new ArrayList<ClassInfo>();
  29. Connection connection=null;
  30. try{
  31. connection=getConnection();
  32. String getName="select classId,className from classinfo";
  33. Statement statement=connection.createStatement();
  34. ResultSet rs=statement.executeQuery(getName);
  35. while(rs.next()){
  36. ClassInfo classInfo=new ClassInfo();
  37. classInfo.setClassId(rs.getInt("classId"));
  38. classInfo.setClassName(rs.getString("className"));
  39. classInfoList.add(classInfo);
  40. }
  41. }
  42. finally {
  43. connection.close();
  44. }
  45. return classInfoList;
  46. }
  47.  
  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. public List<StudentInfo> listStudent(int classId) throws SQLException, ClassNotFoundException {
  66. new ArrayList<StudentInfo>();
  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. DatabaseUtil.toMapList(rs);
  82. }
  83. finally {
  84. connection.close();
  85. }
  86. return null;
  87. }
  88.  
  89. public List<Map<String, Object>> listStudent1(int classId) throws SQLException, ClassNotFoundException {
  90.  
  91. Connection connection=null;
  92. try{
  93.  
  94. connection=getConnection();
  95. StringBuilder builder = new StringBuilder();
  96. builder.append("select si.studentId,si.studentName,si.birthdate,si.mobileNo,si.classId,ci.className");
  97. builder.append(" from studentinfo si ");
  98. builder.append(" inner join classinfo ci ON si.classId=ci.classId ");
  99. if(classId>0)
  100. builder.append(" where si.classId=").append(classId);
  101.  
  102. String retriveStudentList=builder.toString();
  103. Statement statement=connection.createStatement();
  104. //System.out.println(retriveStudentList);
  105. ResultSet rs=statement.executeQuery(retriveStudentList);
  106. List<Map<String, Object>> list=DatabaseUtil.toMapList(rs);
  107.  
  108. return list;
  109. }
  110. finally {
  111. connection.close();
  112. }
  113. }
  114.  
  115. public StudentInfo getStudentById(int id) throws SQLException, ClassNotFoundException {
  116. StudentInfo student=null;
  117. Connection connection=null;
  118. //List<StudentInfo> list=new ArrayList<StudentInfo>();
  119. try{
  120. connection=getConnection();
  121. String retriveUserByID="select * from studentinfo where studentId="+id;
  122.  
  123. Statement statement=connection.createStatement();
  124. ResultSet rs=statement.executeQuery(retriveUserByID);
  125. if(rs.next()){
  126.  
  127. student=new StudentInfo();
  128. student.setStudentId(rs.getInt("studentId"));
  129. student.setStudentName(rs.getString("studentName"));
  130. student.setBirthdate(rs.getString("birthdate"));
  131. student.setMobileNo(rs.getString("mobileNo"));
  132. student.setClassId(rs.getInt("classId"));
  133. return student;
  134. //list.add(student);
  135. }else{
  136. return null;
  137. }
  138. }
  139. finally{
  140. connection.close();
  141. }
  142. //return student;
  143. }
  144.  
  145. public int updateStudentInfo(StudentInfo student) throws SQLException, ClassNotFoundException {
  146. int x=0;
  147. Connection connection=null;
  148. try{
  149. connection=getConnection();
  150. String updateUser="update studentinfo set studentName='"+student.getStudentName()+"',birthdate='"+student.getBirthdate()+"',mobileNo='"+student.getMobileNo()+"',classId='"+student.getClassId()+"' where studentId='"+student.getStudentId()+"'";
  151. Statement statement=connection.createStatement();
  152. x=statement.executeUpdate(updateUser);
  153. }
  154. finally{
  155. connection.close();
  156. }
  157. return x;
  158. }
  159.  
  160. public int deleteUser(int id) throws SQLException, ClassNotFoundException {
  161. int status=0;
  162. Connection connection=null;
  163. try{
  164. connection=getConnection();
  165. //StudentInfo student=new StudentInfo();
  166.  
  167. Statement st=connection.createStatement();
  168. String deleteStudent="delete from studentinfo where studentId="+id;
  169.  
  170. status=st.executeUpdate(deleteStudent);
  171. }
  172. finally{
  173. connection.close();
  174. }
  175. return status;
  176. }
  177.  
  178. }
Add Comment
Please, Sign In to add comment