-
Notifications
You must be signed in to change notification settings - Fork 1
/
CourseDAOImpl.java
102 lines (88 loc) · 3.5 KB
/
CourseDAOImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.leapfrog.academy.dao.impl;
import com.leapfrog.academy.constant.SQLConstant;
import com.leapfrog.academy.dao.CourseDAO;
import com.leapfrog.academy.dbutil.DbConnection;
import com.leapfrog.academy.entity.Course;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author admin
*/
public class CourseDAOImpl implements CourseDAO {
private DbConnection conn=new DbConnection();
@Override
public int insert(Course c) throws SQLException, ClassNotFoundException {
conn.open();
PreparedStatement stmt=conn.init(SQLConstant.COURSE_INSERT);
stmt.setString(1, c.getName());
stmt.setString(2, c.getDescription());
stmt.setDouble(3, c.getFees());
stmt.setInt(4, c.getDuration());
stmt.setString(5, c.getDurationType());
stmt.setBoolean(6, c.isStatus());
int result=conn.executeUpdate();
conn.close();
return result;
}
@Override
public int update(Course t) throws SQLException, ClassNotFoundException {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public int delete(int id) throws SQLException, ClassNotFoundException {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public Course getbyId(int id) throws SQLException, ClassNotFoundException {
Course course=null;
conn.open();
PreparedStatement stmt=conn.init(SQLConstant.COURSE_GETBYID);
stmt.setInt(1, id);
ResultSet rs=conn.executeQuery();
while(rs.next()){
course=mapData(rs);
}
conn.close();
return course;
}
@Override
public List<Course> getAll(boolean show) throws SQLException, ClassNotFoundException {
List<Course> courseList=new ArrayList<>();
conn.open();
String sql=(!show)?SQLConstant.COURSE_GETALL + " WHERE status=1":SQLConstant.COURSE_GETALL;
PreparedStatement stmt=conn.init(sql);
ResultSet rs=conn.executeQuery();
while(rs.next()){
Course course=mapData(rs);
courseList.add(course);
}
conn.close();
return courseList;
}
private Course mapData(ResultSet rs) throws SQLException{
Course course=new Course();
course.setId(rs.getInt("id"));
course.setName(rs.getString("course_name"));
course.setDescription(rs.getString("course_description"));
course.setFees(rs.getDouble("fees"));
course.setDuration(rs.getInt("duration"));
course.setDurationType(rs.getString("duration_type"));
course.setAddedDate(rs.getDate("added_date"));
course.setModifiedDate(rs.getDate("modified_date"));
course.setStatus(rs.getBoolean("status"));
return course;
}
@Override
public int count() throws SQLException, ClassNotFoundException {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}