I have a java program that will take a student's name from one table and join it with a classname and class_id from another table. I want the student_name,classname,and class_id to be written to a join table and saved in MySQL. Do I need to create a join table or can it be done through a SELECT statement?
This is what I have so far:
The students table has 3 columns: student_id, student_name, and hometown and the classes table has 3 columns: class_id, classname, description.
ResultSet rs3 = myStmt3.executeQuery("SELECT * FROM ClassSelector.classes WHERE class_id = " + selectedClass);
while(rs3.next()){
String innerJoin = (userEnterId + " has been added to " + rs3.getString("classname") + " " + rs3.getString("class_id"));
System.out.println(innerJoin);
String student_classJoin = "INSERT INTO students" + "(student_name, class_id, classname)" + "VALUES (?, ?, ?)";
PreparedStatement pStmt = con.prepareStatement(student_classJoin);
pStmt.setString(1, userEnterId);
pStmt.setString(2, rs2.getString("class_id"));
pStmt.setString(3, rs2.getString("classname"));
pStmt.executeUpdate();
input.close();
}