System.out.println("Timer has schedule the tasks...");
}
}
Output
Timer has schedule the tasks...
Send emails..
Clean up files...
Clean up files...
Clean up files...
Clean up files...
Clean up files...
Clean up files...
Clean up files...
Clean up files...
Clean up files...
Clean up files...
Clean up files...
/*
* Schedules the specified task for execution at the specified
* time. If the time is in the past, the task is scheduled for
* immediate execution.
*
* Parameters:
*
* task - task to be scheduled.
*
* time - time at which task is to be executed.
*/
timer.schedule(cleanUpTimerTask, scheduleDateTime); System.out.println("Timer has schedule the -------cleanUpTimerTask..."); }
}
Output:
scheduleDateTime = Mon Jan 14 10:15:02 IST 2019
Timer has schedule the -------cleanUpTimerTask...
Clean up files...
Join the Ram N Java community for weekly coding deep-dives!
Implementing Spring JdbcTemplate: A Beginner's Guide
Database management can often feel overwhelming for beginners. In this tutorial, we break down how to use Spring JdbcTemplate to handle database operations with much less code compared to traditional JDBC. Let's dive in!
1. Why Use Spring JdbcTemplate?
Traditionally, JDBC requires a lot of "boilerplate" code—opening connections, handling exceptions, and closing connections. Spring's JdbcTemplate takes care of all those repetitive tasks for you. This allows you to focus purely on the SQL logic and your application data.
2. Setting Up Your Project
To get started, you need to ensure your environment is ready. You will need:
Java Development Kit (JDK) installed.
Spring Framework libraries in your project.
A Database Driver (like MySQL or Oracle) to connect to your database.
3. The DAO (Data Access Object) Pattern
In this example, we follow the DAO pattern. We create an interface (e.g., EmployeeDAO) and an implementation class (EmployeeDAOImpl). Inside the implementation, we use JdbcTemplate to execute our queries, making the code clean and organized.
4. Configuring the DataSource
Spring needs to know how to find your database. You define a DataSource bean in your configuration file, providing the database URL, username, and password. This DataSource is then "injected" into the JdbcTemplate, giving it the power to talk to your database.
5. Executing an Update
Whether you are inserting a new record or updating an existing one, the update() method is your best friend. In the video, we demonstrate how to pass parameters safely into an SQL query, preventing issues like SQL injection while keeping the logic simple.
More From Ram N Java
If you enjoyed this tutorial, check out these other videos to continue your learning journey:
Jan 29, 2019 10:29:43 AM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@736e9adb: startup date [Tue Jan 29 10:29:43 IST 2019]; root of context hierarchy
Jan 29, 2019 10:29:43 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [applicationContext.xml]
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
Jan 29, 2019 10:29:44 AM org.springframework.jdbc.datasource.DriverManagerDataSource setDriverClassName
INFO: Loaded JDBC driver: com.mysql.jdbc.Driver
Tue Jan 29 10:29:45 IST 2019 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
Employee record inserted successfully.
Jan 26, 2019 10:30:05 AM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@35d176f7: startup date [Sat Jan 26 10:30:05 IST 2019]; root of context hierarchy
Jan 26, 2019 10:30:05 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [Spring-Module.xml]
Jan 26, 2019 10:30:06 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [database/Spring-Datasource.xml]
Jan 26, 2019 10:30:06 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [employee/Spring-Employee.xml]
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
Jan 26, 2019 10:30:07 AM org.springframework.jdbc.datasource.DriverManagerDataSource setDriverClassName
INFO: Loaded JDBC driver: com.mysql.jdbc.Driver
Sat Jan 26 10:30:08 IST 2019 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
Sat Jan 26 10:30:22 IST 2019 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
Employee [employeeId=1, name=Peter, age=28, salary=80000]