This blog shows how we can use a simple maven project and work with Spring to interact with a database.
First, we create a simple project using the maven command..
mvn archetype:generate
We select the maven-archetype-quickstart project (option 15)
Run the command
mvn eclipse:m2eclipse
to create the .classpath and .project files for eclipse.
We will create a file beans.xml in src/main/resources dir. Not creating the beans.xml file in the resources dir will give you the following exception:
Exception in thread “main” org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [beans.xml]; nested exception is java.io.FileNotFoundException: class path resource [beans.xml] cannot be opened because it does not exist
Here is my beans.xml file.
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="dataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://localhost:3306/database-name"></property> <property name="username" value="root"></property> <property name="password" value="root"></property> </bean> <bean id="template"> <property name="dataSource" ref="dataSource"></property> </bean> <bean id="mySQLDao"> <property name="template" ref="template"></property> </bean> </beans>
Create a main class with the following code:
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); MySQLDao mySQLDao = (MySQLDao)context.getBean("mySQLDao"); System.out.println(mySQLDao.queryForDate());
In MySQLDao we have the following code:
private JdbcTemplate template; public Date queryForDate(){ String sql = "select now() from dual;"; Date currentDate = (Date)template.queryForObject(sql, Date.class); return currentDate; } public void setTemplate(JdbcTemplate template) { this.template = template; }
In pom.xml add the following dependencies:
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>2.5.6</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>2.5.6</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>2.5.6</version> <scope>compile</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.11</version> <scope>compile</scope> </dependency>
If the mysql-connector jar is not installed in the local repository , use the following command to install the jar.
mvn install:install-file -DgroupId=mysql -DartifactId=mysql-connector-java -Dversion=5.1.11 -Dfile=mysql-connector-java-5.1.11-bin.jar -DgeneratePom=true -Dpackaging=jar
Running the main class should now give the current timestamp from the database.
To run main from the command line use the following command
mvn exec:java -Dexec.mainClass="com.wordpress.codesilo.Main"