I made a simple application in Java Spring that should read and write to a database with two varchar fields.
package learn.spring.example.dao;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
@Component
public class PersonStoreDao implements PersonInter{
@Autowired
private JdbcTemplate jdbc;
public Long saveData(Person person){
final String querySample="insert into PersonName(first_name, last_name) values (?, ?)";
jdbc.update(new PreparedStatementCreator() {
public PreparedStatement prepStatement(Connection c) throws SQLException {
PreparedStatement prep = c.prepareStatement(querySample);
prep.setString(1, person.getFirstName());
prep.setString(2, preson.getSecondName());
return prep;
}
,key}
}
}
Class Person is just a simple bean class and PersonName is the table from database with two fields. I have several questions about this.
First question is if something is wrong
What is the effect of using @Autowired in front of jdbc variable and @Component?
How can it embedded correctly in xml files assciated to this project?
I can connect to the database but can not do writing it. What should be done so that the database is not only read but also written?