Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Why is my 2nd insert not working ?

  1. #1
    Member
    Join Date
    Jul 2011
    Posts
    51
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Why is my 2nd insert not working ?

    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
    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");
    }
    }}
    Controller : note: I have left out the doPost(HttpRequest req, HttpResponse res)
    try{
    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);
    subject
    public class subject {

    public subject() {
    super();
    // TODO Auto-generated constructor stub
    }

    private ArrayList<String> subj;

    getter setter

  2. #2
    Member
    Join Date
    May 2017
    Location
    Eastern Florida
    Posts
    68
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default Re: Why is my 2nd insert not working ?

    Please use code tags, not quote tags when posting code.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.

Similar Threads

  1. 2nd time not executed while in my program
    By RSelvan in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 26th, 2012, 12:08 PM
  2. wanting 2nd octet of an IP address
    By roofninja in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 11th, 2012, 04:20 AM
  3. BufferedReader - Freezes on 2nd read
    By mds1256 in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: March 14th, 2011, 03:26 PM
  4. Replies: 2
    Last Post: June 8th, 2010, 02:14 PM

Tags for this Thread