Nick C
Nick C

Reputation: 69

Jdbc template crud operations

So I have the first steps of a webapp, have class Doctor and I want to perform some operations like view all, insert, delete, etc. :

public class Doctor {
public String firstName;
public String lastName;
public int id;

public Doctor(){
}
public Doctor(int id, String first, String last){
setId(id); 
setFirstName(first);
setLastName(last);
}
// getters and setters

Here is an implementation of one method from my interface Service. They are all pretty much the same with the appropriate sql queries. I tried following several different tutorials.

public class DAOImpl implements DAO{

public void insertUpdateDoctor(Doctor doctor){ 

    String sql = "INSERT INTO doc_flight.docflight_doctors(id, first_name,last_name)" + "Values(?,?,?)";             



    jdbcTemplateObject.update(sql,new Object[]{doctor.getId(),doctor.getFirstName(),doctor.getLastName()});

Heres the part in main where I try to call it. The program doesn't even try to enter the method, it doesn't come up in debug and moves to the next method I try in main, view all, which works. Presumably, I'm not calling the method correctly and tried rewriting all parts several times. Help?!

  Doctor test = new Doctor(17,"jack", "sparrow"); 
service.insertUpdateDoctor(test);

Upvotes: 1

Views: 500

Answers (2)

Khaled Abu Shamat
Khaled Abu Shamat

Reputation: 1134

For me I would not write this class from scratch, I would prefer to generate it in few clicks and save my time using The Cloud Wizard:

  1. Go to https://codegen.cloud-wizard.com
  2. Click on Java
  3. From the technologies section press on Java SE
  4. Select JDBC Class transformer.
  5. In the metadata section enter a name for the JDBC Class e.g. (DoctorDao)
  6. Add some fields e.g. first name and last name
  7. Press on generate code and you will get your class ready and working as expected.

Choose Language (Java)

Choose Technology (Java SE)

Choose Transformer (JDBC Class)

Add Metadata

Generated Code

Upvotes: 0

Marco Vargas
Marco Vargas

Reputation: 1337

The issue itself it's not pretty clear for me.

If the problem is that when calling this:

Doctor test = new Doctor(17,"jack", "sparrow"); 
service.insertUpdateDoctor(test);

The runtime is not getting inside insertUpdateDoctor, just check how you are instantiating the object service

if the problem is that it's not executing correctly the sql statement, try by using a PreparedStatement (it's a good practice) by doing something like:

        String connectionStr = StringUtils.format("INSERT INTO %s.docflight_doctors(id, first_name,last_name) Values(?,?,?)", this.databaseName);
        try (Connection connection = this.dataSource.getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(connectionStr)) {
            preparedStatement.setInt(1, doctor.getId());
            preparedStatement.setString(2, doctor.getFirstName());
            preparedStatement.setString(3, doctor.getLastName());
            preparedStatement.execute();
            connection.commit();
        } catch (Exception ex) {
            this.logger.error(String.format("Error when inserting: %s", ex.toString()));
        }

Hope it may help you.

Upvotes: 1

Related Questions