0

i am trying to develop a java student registration app with mysql database in netbeans,

Alex D Rex
  • 89
  • 1
  • 9
  • What you are trying to do is not possible. The programm cannot know which of the two methods would be called in what instance. It cannot know if an insert or update is required. You have to provide the logic for that yourself. – OH GOD SPIDERS Dec 13 '16 at 12:47
  • 1
    in short: You should probably just rename the second method to updateStudent. – OH GOD SPIDERS Dec 13 '16 at 12:47
  • or you can have a single method and it checks if the student is already there in database, then you have to update it otherwise just create a new student. – Sikorski Dec 13 '16 at 12:49
  • And one hint for the future: you want us to spend our time to help you. So you please spend the few minutes it takes to properly format/indent all your source code. It is not very polity to drop such a mess on us. – GhostCat Dec 13 '16 at 13:33

4 Answers4

1

Your problem is actually a wrong mental model.

Adding a new student to a database is not at all the same as updating the records for some "existing" student that already has a record stored in the database.

Those are two completely different activities; and all your design/code should make that very clear.

So, even when it would be technically possible to have two methods with the same signature but different return types; it would still not make sense to do that here.

Because your two different methods do different things, so they should not even have the same name! You should better rename both, like createStudent and updateExistingStudent.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • Sir, Student parameter is what comes from import.student_details.Student class, if i change that i get errors, – Alex D Rex Dec 13 '16 at 14:08
  • I am not telling you to change your **Student** class. I am telling you that you **can not** and **should not** have two methods in **StudentController** that both read `int addStudent(Student)`. That doesn't work (gives you compiler errors already); and doesn't make sense; as explained above. – GhostCat Dec 13 '16 at 14:12
0

the method signature must be unique in a class. To overload the method, you have to change the parameters of one of both methods. Changing the return type doesn't work. Are you sure, that the ` in your sql below will work?

gregh221
  • 1
  • 4
  • sir, i am sure, but how can i change the parameters, the first parameter is Student that i cant change which is Student class that use getters and setters, – Alex D Rex Dec 13 '16 at 12:54
  • In MySQL backtick can delimit identifiers (table and column names etc); see http://stackoverflow.com/questions/10573922/what-does-the-sql-standard-say-about-usage-of-backtick . Here it's unnecessary and confusingly inconsistent but not wrong. – dave_thompson_085 Dec 13 '16 at 14:30
0

You cannot have two methods with the exact signature in the same class, something must be different or the compiler won't be able to know which method you are calling.

If you want to use the same method to update and insert a Student, you should use one method that checks the existence of that Student and then chooses between inserting or updating. But then don't call it addStudent, you should call it addOrUpdate, upsert (update+insert) or something like that.

Pablo Lozano
  • 10,122
  • 2
  • 38
  • 59
0

As a suggestion, you could perform a check to see if a student already exists on your database. If the student exists, update his/her records, if not, add a new record. You could use the ID to perform the check since it is supposed to be unique. And also try to separate Database connection code to a utility class that provides connection on demand (I could share this with you if you want). And lastly, I prefer a PreparedStatement to a Statement... keeps the SQL Query and the general code clean. Good luck

jaletechs
  • 501
  • 1
  • 7
  • 19