Spring JDBC insert into DB will not work

Multi tool use


Spring JDBC insert into DB will not work
For some reason my jdbctemplate is not picking up the info in my application.properties file. I can only insert in my db when I make my own connection exposing my username and password in the file. Why isn't spring boot picking this up? Please help!
I am calling the addTaglocation mode while I am still reading tags from multiple RFID readers. Would this cause the issue? If so how do I solve it. Thanks in advance.
where I am calling the create method:
public void addTaglocation(Taglocation tl){
log.info("in addtaglocation");
Taglocation newtagloca= new Taglocation(tl.getReadername(),tl.getRfidtag(),tl.getZone(), tl.getTaggingsource(), tl.getVineight(), tl.getZonedate());
TaglocationJDBCTemplate newTag= new TaglocationJDBCTemplate();
newId= newTag.create(newtagloca);
}
(UPDATED)
Application.properties file:
spring.datasource.url=jdbc:mysql://localhost:3306/DB
spring.datasource.username=
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
(UPDATED)
taglocationJDBCTemplate file
@Repository
public class TaglocationJDBCTemplate implements TaglocationDAO {
@Autowired
JdbcTemplate jdbcTemplate;
public void create(Taglocation tl){
KeyHolder keyHolder = new GeneratedKeyHolder();
jdbcTemplate.update(new PreparedStatementCreator(){
public PreparedStatement createPreparedStatement(final Connection connection) throws SQLException {
PreparedStatement ps = connection.prepareStatement("INSERT INTO TAGLOCATION (READERNAME, RFIDTAG, TAGGINGSOURCE, ZONE, VINEIGHT, ZONEDATE) VALUES (?,?,?,?,?,?)",
Statement.RETURN_GENERATED_KEYS);
ps.setString(1, tl.getReadername());
ps.setString(2,tl.getRfidtag());
ps.setString(3, tl.getTaggingsource());
ps.setString(4, tl.getZone());
ps.setString(5, tl.getVineight());
ps.setTimestamp(6, new Timestamp(System.currentTimeMillis()));
return ps;
}
}, keyHolder);
return (Integer) keyHolder.getKey();
}
OK....so its not inserting record because there was no id....I have the id auto incrementing...how do i get last id? Do I search for it before I insert record?
– Roro
2 days ago
You are using Spring Boot (according to your tags) then why are you creating a new context to retrieve the bean?! Never do that. Inject the dao instead of creating a new context. Also if you are really using Spring Boot you can completely remove your
SpringJDBConfig
as all of that is already done by Spring Boot.– M. Deinum
2 days ago
SpringJDBConfig
OK changed that but it still is not inserting record. Updated create method.
– Roro
yesterday
If you are using Spring Boot, remove your configuration and add 3 properties to your
application.properties
to configure the DataSource
, spring boot will do the rest. And put @Transactional
on your service method. Trying to insert stuff in the database without a transaction isn’t going to work.– M. Deinum
yesterday
application.properties
DataSource
@Transactional
1 Answer
1
you need to do use this annotation @ContextConfiguration(classes = Config.class)
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
So the context is working....it just won't insert the record and there is not error.....
– Roro
2 days ago