Spring Series 2--Docking Database __ Database

Source: Internet
Author: User

Database selection a lot: Jdbc->ibatis->mybatis->druid

We're using Druid now.

First, join the MAVEN library

The purpose of joining the MAVEN library is to download druid this Third-party library so that we can use it later. Add in Pom.xml:

<dependency>
   <groupId>mysql</groupId>
   <artifactid>mysql-connector-java</ artifactid>
   <version>${mysql.verison}</version>
</dependency>
<dependency >
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    < version>1.0.15</version>
</dependency>
<dependency>
   <groupId> javax.servlet</groupid>
   <artifactId>servlet-api</artifactId>
   <version>${ servlet.version}</version>
   <scope>provided</scope>
</dependency>

Second, configure DataSource

Add the bean to the spring configuration file Spring-core.xml:

<!--dataSource--> <bean id= "DataSource" class= "Com.alibaba.druid.pool.DruidDataSource" init-method= "Init" destroy-method= "Close" > <!--basic property URL, user, password--> <property name= "url" value= "${jdbc.url}"/> < Property name= "username" value= "${jdbc.username}"/> <property name= "password" value= "${jdbc.password}"/> &L t;! --Configure initialization size, MIN, max--> <property name= "InitialSize" value= "1"/> <property name= "Minidle" value= "1"/> ;p roperty name= "maxactive" value=/> <!--Configure the time to get the connection wait timeout--> <property name= "maxwait" value= "60000"/&gt

	;

	<!--how often the configuration interval is detected to detect idle connections that need to be closed, in milliseconds--> <property name= "Timebetweenevictionrunsmillis" value= "60000"/> <!--Configure a connection to the minimum surviving time in the pool, in milliseconds--> <property name= "Minevictableidletimemillis" value= "300000"/> < Property Name= "Validationquery" value= "SELECT ' x '"/> <property name= "Testwhileidle" value= "true"/> <prope Rty name= "Testonborrow" value= "FalsE "/> <property name=" Testonreturn value= "false"/> <!--open Pscache and specify the Pscache size--> on each connection Y name= "poolpreparedstatements" value= "false"/> <property name= "Maxpoolpreparedstatementperconnectionsize" Value=/> <!--configuration Monitor filters--> <property name= "Filters" value= "stat"/> </bean>
We found the database address, username, and password in this file somewhere else, so we also want to introduce JDBC database configuration information. is the next step.

Third, add database configuration information

Build jdbc.propertites files under the Resources folder and add:

Jdbc.url=jdbc:mysql://127.0.0.1:3358/db_name?characterencoding=utf-8
jdbc.username=root
jdbc.password= 123
Iv. Introducing database configuration information into the spring configuration file

In the Spring-core.xml file, add:

	<bean
		class= "Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >
		< Property Name= "Locations" >
			<list>
				<value>classpath:/jdbc.properties</value>
			</list>
		</property>
	</bean>

V. Test Database Connectivity

To test the connectivity of the database, we create a test class that has the function of creating a table and indicating that database connectivity is normal if the table is created successfully.

public class TestCase {
    @Test the public
    void Testconnectdb () throws Exception {
        DataSource DataSource;
        ApplicationContext ApplicationContext = new Classpathxmlapplicationcontext ("Spring-core.xml");
        DataSource = (DataSource) applicationcontext.getbean ("DataSource"); name

        of//bean; StringBuffer DDL = new StringBuffer ();
        Ddl.append ("CREATE TABLE t_big (FID INT auto_increment PRIMARY KEY");
        for (int i = 0; i < ++i) {
            ddl.append (",");
            Ddl.append ("F" + i);
            Ddl.append ("BIGINT NULL");
        }
        Ddl.append (")");

        Connection conn = Datasource.getconnection ();

        Statement stmt = Conn.createstatement ();
        Stmt.execute (Ddl.tostring ());
        SYSTEM.OUT.PRINTLN ("CREATE table Success");
        Stmt.close ();

        Conn.close ();
    }


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.