简体   繁体   中英

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);

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.

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 eg (DoctorDao)
  6. Add some fields eg first name and last name
  7. Press on generate code and you will get your class ready and working as expected.

选择语言 (Java)

选择技术 (Java SE)

选择 Transformer(JDBC 类)

添加元数据

生成的代码

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM