Dear expert,
I am trying to get the servlet to read the values in this case 'subject' from my html and insert into MySQL database.
The column subject is supposed to have more than 1 subject or rather it should have a list of subjects enter into the column. E.g Science, Maths, Physics etc in that column.
The database is a many-to-many relationship.
Table 1: tutor
tutorName, tutorEmail, tutorID etc
Table 2 :subject
subject
Table 3 : tutor-subject (this is the link table)
tutorID, subject
I hope someone can tell me what's wrong with my code :
SujectDAOImpl : Connection portion left out
Controller : note: I have left out the doPost(HttpRequest req, HttpResponse res)public class subjectDAOImpl implements subjectDAO {
private final String INSERT_QRY1 = "INSERT INTO project.subject subject VALUES ('?')";
Connection conn;
public void insertSubject(ArrayList<String>subj) throws MyDataException {
try {
subject s = new subject();
subj = new ArrayList<String>();
openConnection();
String qry = INSERT_QRY1;
ps = connection.prepareStatement(qry);
ps.setObject(1, s.getSubj());
ps.executeUpdate();
if (ps !=null )
closeConnection();
}
catch (SQLException e) {
e.printStackTrace();
throw new MyDataException("DB Error");
}
}}
subjecttry{
subject m = new subject();
ArrayList<String>subj = new ArrayList<String>();
String[] sub = request.getParameterValues("subject");
for (int i = 0; i< sub.length; i++){
for (String s : sub)
subj.add(s);
m.setSubj(subj);
mgr.insertSubject(subj);
public class subject {
public subject() {
super();
// TODO Auto-generated constructor stub
}
private ArrayList<String> subj;
getter setter