skip to Main Content

I am new to programming and I am trying to write a java program which can solve the following problem:

There are 100 students and 100 books. The books are grouped under
four category (Artificial intelligence, machine learning, deep
learning and IOT ) and each category has 25 books. we can’t give a
book each student needs so that students will choose the books using
category. then the program assigns one book from the category for each
student by considering choose order and gpa of students’ result. students will be compared by GPA so that the system
will assign 1st choose, 2nd choose,….,4th choose based on
comparison. one student will have only one book. I tried as following.

PreparedStatement ps = null;
String query_s = "SELECT choice1,choice2, choice3, choice4, GPA FROM 
Students_table ";
ResultSet rs = stmt.executeQuery(query_s);
while (rs.next()) {
  double gpa = rs.getDouble("GPA");
  String choice1 = rs.getString("choice1");
  String choice2 = rs.getString("choice2");
  String choice3 = rs.getString("choice3");
  String choice4 = rs.getString("choice4");
  if (gpa > 3.5) {
    ps = conn.prepareStatement("UPDATE Students_table SET Book =" + choice1 + " ");
    ps.execute();
  }
  if (gpa < 3.5 && gpa > 3) {
    ps = conn.prepareStatement("UPDATE Students_table SET Book  =" + choice2 + "");
    ps.execute();
  }
  if (gpa < 3 && gpa > 2.5) {
    ps = conn.prepareStatement("UPDATE Students_table SET Book =" + choice3 + "");
    ps.execute();
  } else {
    ps = conn.prepareStatement("UPDATE Students_table SET Book  =" + choice4 + "");
    ps.execute();
  }
}

It says:
java.sql.SQLSyntaxErrorException: Column ‘MACHINE_LEARING’ is either not in any table in the FROM list or appears within a join specification and is outside the scope of the join specification or appears in a HAVING clause and is not in the GROUP BY list. If this is a CREATE or ALTER TABLE statement then ‘MACHINE_LEARING’ is not a column in the target table. table "Students_table" looks at this.
Students_table sample

But I can’t write a method to compare students using choose order and GPA to give the book they want. please help!

2

Answers


  1. Use where to identify row and use ? to avoid SQL Injection.

    PreparedStatement stmt=conn.prepareStatement("update Students_table set Book=? where Name=?");  
    stmt.setString(1,"choice1"); 
    stmt.setString(2,"Mark");  
    
    Login or Signup to reply.
  2. This isn’t an answer, it’s a comment. I wish to clarify your question.
    Here is your sample data.

    Name  Choice1                  Choice2                  Choice3  Choice4           GPA
    ====  =======================  =======================  =======  ================  ===
    Mark  Machine_learning         Artificial_intelligence  IoT      Deep_learning     3.0
    John  Artificial_intelligence  Machine_learning         IoT      Deep_learning     2.8
    Ward  Deep_learning            Artificial_intelligence  IoT      Machine_learning  4.0
    Mose  Machine_learning         Artificial_intelligence  IoT      Deep_learning     3.5
    

    The expected result is as follows.

    1. Ward is the student with the highest GPA so he is assigned his first choice which is Deep_learning.
    2. Mose is the student with the second highest GPA so he also gets his first choice, which is Machine_learning, also because that book has not yet been assigned.
    3. The student with the third highest GPA is Mark. His first choice – Machine_learning – has already been assigned (to Mose), so he gets his second choice – Artificial_intelligence.
    4. That leaves John whose first and second choices have already been assigned, so he gets his third choice which is IoT

    That means you need to perform the following UPDATE statements.

    UPDATE Students_table SET Book = 'Deep_learning' WHERE Name = 'Ward'
    UPDATE Students_table SET Book = 'Machine_learning' WHERE Name = 'Mose'
    UPDATE Students_table SET Book = 'Artificial_intelligence' WHERE Name = 'Mark'
    UPDATE Students_table SET Book = 'IoT' WHERE Name = 'John'
    

    If the above is correct, then edit your question and add the above explanation and I will delete this "answer".

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search