First, create a spring project
Project Name: spring101301
Second, add the relevant jar in the spring project
1. Create a Lib directory on the project
/lib
2. Add spring-supported jar packages to the Lib directory
Commons-logging.jar
Junit-4.10.jar
Log4j.jar
Mysql-connector-java-5.1.18-bin.jar
Spring-beans-3.2.0.release.jar
Spring-context-3.2.0.release.jar
Spring-core-3.2.0.release.jar
Spring-expression-3.2.0.release.jar
--For database operations
Spring-jdbc-3.2.0.release.jar
Spring-tx-3.2.0.release.jar
Third, testing
1. Create the test directory on the project
/test
2. Creating a test package under the test directory
Package Name: Cn.jbit.spring101301.test
3. Create a test class under a test package
Test class Name: Createtabledemo.java
Test the contents of the class:
Package cn.jbit.spring101301.test;
Import Org.junit.Test;
Import Org.springframework.jdbc.core.JdbcTemplate;
Import Org.springframework.jdbc.datasource.DriverManagerDataSource;
public class Createtabledemo {
/**
* Create a table using spring JdbcTemplate
*/
@Test
public void Testjdbctemplate () {
Create a data source
Drivermanagerdatasource DataSource = new Drivermanagerdatasource ();
Setting Database connection Information
Datasource.seturl ("jdbc:mysql://localhost:3306/spring");
Datasource.setdriverclassname ("Com.mysql.jdbc.Driver");
Datasource.setusername ("root");
Datasource.setpassword ("root");
Create an object that connects to the database JdbcTemplate
JdbcTemplate jdbctemplate = new JdbcTemplate (DataSource);
Execute SQL statement
String sql = "CREATE TABLE temp (tid int primary key,tname nvarchar (30))";
Invoke execution method, execute SQL
Jdbctemplate.execute (SQL);
}
}
This article is from the "Yan" blog, please be sure to keep this source http://suyanzhu.blog.51cto.com/8050189/1563216
spring-database Operations via JdbcTemplate-code implementation, no configuration files