-3

I'm making program for teachers with Java and mysql , so I've many tables in mysql such as students table which contains student name and number and ID .. etc , also another table that called groups , It contains group name , place and students .. etc. So , the problem is , in Groups table I'll add IDs of students in one row just like the image bellow beside the name of group , how can I take this IDs from mysql in this form and separate them automatically in program by java to search in the students table about the information of them .

image : https://i.stack.imgur.com/NFQ6t.jpg

  try {
    Class.forName("java.sql.Driver");
    Connection con = (Connection)   DriverManager.getConnection("jdbc:mysql://localhost:3306/teacherassistant?allowMultiQueries=true","root","1234");
    Statement st = (Statement) con.createStatement();
    String query = "select * from Groups;";
    ResultSet rs = st.executeQuery(query);

    while(rs.next()){
    String name = rs.getString("Name");
    String StudentIDs= rs.getString("Students");
    String Center = rs.getString("Center");

    }

    } catch (Exception e) {
        JOptionPane.showMessageDialog(this,"Error");
    }
evilxbyte
  • 3
  • 2
  • 2
    Are you asking how to make a program? I'd try with a more specific question. – javier_domenech May 25 '16 at 20:59
  • It's not a shame that I don't now this ! maybe it's easy for you but I really need to know how to separate the info to use them again :3 – evilxbyte May 25 '16 at 21:01
  • Please add your current code into the question by editing... then we can help you better! – vbnet3d May 25 '16 at 21:02
  • 1
    Please, take the good point of the critic. Try with a more specific question, not asking for a while program. Try by yourself and once you stuck with something search for info and, if any doubt , ask here. – javier_domenech May 25 '16 at 21:03
  • You shouldn't store the group information like this. You should have three tables - students, groups, and student-to-group. That's the classical way of representing a many to many relationship in a relational database. Research this topic, as well as database normalization, design your database properly, and the problem will solve itself. – RealSkeptic May 25 '16 at 21:09
  • Okay thank you guys :D – evilxbyte May 25 '16 at 21:12

1 Answers1

0

Don't do that.

The relationship between groups and students is clearly a many-to-many relationship. Instead make a new table that contains this relationship. Something like;

STUDENT_GROUP_REL
------------------
(student_id, group_id)

There is a related question and a very good answer about m:n relations here

Community
  • 1
  • 1
tafa
  • 7,146
  • 3
  • 36
  • 40