Reputation: 391
I am new to prepared statement and i'm trying to use basic insert and update using jdbcTemplate which is also working fine but i want logs to be printed for sql queries in consle and i'm not sure how to achieve that. I have added required properties in application.properties file but it's not working. Kindly help me out by giving some appropriate suggestions or refernece link which may solve my problem. Thanks in advance...
SampleConfiguration.java
@Configuration
public class SampleConfiguration
{
@Bean
public DataSource mysqlDataSource()
{
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("org.hsqldb.jdbcDriver");
dataSource.setUrl("jdbc:hsqldb:hsql://localhost/");
dataSource.setUsername("SA");
dataSource.setPassword("");
return dataSource;
}
@Bean
public JdbcTemplate jdbcTemplate(DataSource dataSource)
{
JdbcTemplate jdbcTemplate = new JdbcTemplate();
jdbcTemplate.setDataSource(dataSource);
return jdbcTemplate;
}
}
SampleService.java
@Service
public interface SampleService
{
public int insert();
public long getCount();
}
SampleServiceImpl.java
@Service
public class SampleServiceImpl implements SampleService
{
@Autowired
JdbcTemplate jdbcTemplate;
@Override
public int insert()
{
return jdbcTemplate.update("insert into batch values(?,?)","field1","field2");
}
@Override
public long getCount()
{
return jdbcTemplate.queryForObject("select count(*) from batch", Long.class);
}
}
SpringjdbcApplication.java
@SpringBootApplication
public class SpringjdbcApplication
{
public static void main(String[] args)
{
ApplicationContext context = SpringApplication.run(SpringjdbcApplication.class, args);
SampleService service = context.getBean(SampleService.class);
System.err.println("The number of rows inserted = "+service.insert());
System.err.println("The count of batch class is = "+service.getCount());
}
}
Entity.java
@Entity
public class Batch implements Serializable
{
private static final long serialVersionUID = -5687736664713755991L;
@Id
@Column(name="field1")
private String field1;
@Column(name="field2")
private String field2;
... getter, setter and no-arg constructor
}
application.properties
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=none
spring.jpa.properties.hibernate.format_sql=true
logging.level.org.hibernate.SQL=debug
logging.level.org.hibernate.type.descriptor.sql=trace
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
I want to know how to turn on sql statement logging on console and how to set other properties like auto commit off or on ???
Upvotes: 1
Views: 5241
Reputation: 1528
You should add this in the property file
logging.level.org.springframework.jdbc.core = TRACE
This will enable Spring Jdbc logs
Upvotes: 4