spring-database Operations via JdbcTemplate-code implementation, no configuration files

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.