import java.util.List;
public interface ILookup {
void SaveStudentSurveyFormData(StudentBean postData);
StudentBean GetStudentSurveyData(String studentId);
Surveys GetSurveys();
List<String> GetSurveyIds();
}
StudentDbDAO.java
package gmu.swe642.hw6;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
public class StudentDbDAO implements ILookup {
private Connection con;
public StudentDbDAO(){
try {
Class.forName ("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection ( "jdbc:oracle:thin:@",
dbusername, dbpassword);
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public void SaveStudentSurveyFormData(StudentBean postData){
try {
PreparedStatement pstat = con.prepareStatement("insert into survey values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
pstat.setString(1, postData.getStudentId());
pstat.setString(2, postData.getFirstName());
pstat.setString(3, postData.getLastName());
pstat.setString(4, postData.getStreet());
pstat.setString(5, postData.getZip());
pstat.setString(6, postData.getCity());
pstat.setString(7, postData.getState());
pstat.setString(8, postData.getTelephone());
pstat.setString(9, postData.getEmail());
pstat.setString(10, postData.getUrl());
pstat.setString(11, postData.getDateOfSurvey());
pstat.setString(12, postData.getGraduationMonth());
pstat.setString(13, postData.getGraduationYear());
pstat.setString(14, postData.getLikes());
pstat.setString(15, postData.getInterests());
pstat.setString(16, postData.getRecommendation());
pstat.setString(17, postData.getComments());
pstat.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
public StudentBean GetStudentSurveyData(String studentId){
Surveys surveys = GetSurveys();
for(StudentBean survey : surveys.data){
if(survey.getStudentId().equalsIgnoreCase(studentId)){
return survey;
}
}
return null;
}
public Surveys GetSurveys(){
Surveys surveys = new Surveys();
surveys.data = new ArrayList<StudentBean>();
Statement stmt;
try {
stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select * from survey");
while (rs.next())
{
String studentId = rs.getString(Consts.STUDENTID);
String firstName = rs.getString(Consts.FIRSTNAME);
String lastName = rs.getString(Consts.LASTNAME);
String street = rs.getString(Consts.STREET);
String zip = rs.getString(Consts.ZIP);
String city = rs.getString(Consts.CITY);
String state = rs.getString(Consts.STATE);
String telephone = rs.getString(Consts.TELEPHONE);
String email = rs.getString(Consts.EMAIL);
String url = rs.getString(Consts.URL);
String dateOfSurvey = rs.getString(Consts.DATEOFSURVEY);
String graduationMonth = rs.getString(Consts.GRADUATIONMONTH);
String graduationYear = rs.getString(Consts.GRADUATIONYEAR);
String likes = rs.getString(Consts.LIKES);
String interests = rs.getString(Consts.INTERESTS);
String recommendation = rs.getString(Consts.RECOMMENDATION);
String comments = rs.getString(Consts.COMMENTS);
StudentBean s = new StudentBean(studentId, firstName, lastName, street, zip, city, state,
telephone, email, url, dateOfSurvey, graduationMonth,
graduationYear, likes, interests, recommendation, comments);
surveys.data.add(s);
}
} catch (SQLException e) {
e.printStackTrace();
}
return surveys;
}
public List<String> GetSurveyIds(){
List<String> surveyIds = new ArrayList<String>();
Surveys surveys = GetSurveys();
for(StudentBean survey : surveys.data){
surveyIds.add(survey.getStudentId());
}
return surveyIds;
}
}
No comments:
Post a Comment