This example presents the basic concept of initializing database in Spring JDBC.
The technologies used are :
-
– Spring 4.3.18
– H2 1.3
– Lombok 1.18
– JDK 1.80
– Maven 3.3.9
You can convert this example to an Eclipse IDE project by going to folder where is the pom.xml is, and use the command :
mvn eclipse:eclipse
Inspired from « Beginning Spring » Mert Caliskan, Kenan Sevindik, Rod Johnson (Foreword by), Jürgen Höller (Foreword by).
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>javaspringexamples.springJDBC</groupId> <artifactId>springJdbcInitializingDb</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.3.18.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.18.RELEASE</version> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <version>1.3.175</version> </dependency> </dependencies> </project>
package javaspringexamples.springJDBC.InitializingDb; import java.sql.Connection; import java.sql.SQLException; import javax.sql.DataSource; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * * @author mounir.sahrani@gmail.com * */ public class Main { public static void main(String[] args) throws SQLException { ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:conf.xml"); DataSource dataSource = applicationContext.getBean("dataSource", DataSource.class); Connection connection = dataSource.getConnection(); System.out.println(connection.isClosed()); connection.close(); System.out.println(connection.isClosed()); } }
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd"> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="org.h2.Driver" /> <property name="url" value="jdbc:h2:tcp://localhost/~/javaspringexamples" /> <property name="username" value="sa" /> <property name="password" value="" /> </bean> <!-- <jdbc:embedded-database id="dataSource" type="H2"/> --> <jdbc:initialize-database data-source="dataSource"> <jdbc:script location="classpath:schema.sql" /> <jdbc:script location="classpath:data.sql" /> </jdbc:initialize-database> </beans>
MERGE INTO USER (ID, NAME, USER_NAME, ACCESS_TIME, LOCKED) values (999, 'Mounir SAHRANI', 'msahrani', '2019-01-01',false); MERGE INTO USER (ID, NAME, USER_NAME, ACCESS_TIME, LOCKED) values (256, 'Laurent SOURDEAU', 'lsourdeau', '2019-06-01',false);
CREATE TABLE IF NOT EXISTS USER (ID BIGINT IDENTITY PRIMARY KEY, NAME VARCHAR(60), USER_NAME VARCHAR(60), ACCESS_TIME TIMESTAMP, LOCKED BOOLEAN);
Get the sources of the example from the following GitHub url