Spring's coming, -05-jdbc.

Source: Internet
Author: User
Tags dname

Overview

    1. Spring support for the C3P0 connection pool is perfect
    2. Spring provides jdbctemplate to JDBC to simplify JDBC operations, jdbctemplate template tool classes, similar to dbutils components

Jdbc:

1  PackageCn.fuyi.h_jdbc;2 3 Importjava.sql.Connection;4 ImportJava.sql.DriverManager;5 Importjava.sql.PreparedStatement;6 7  Public classUserdao {8 9      Public voidSave () {Ten         Try { OneString sql = "INSERT INTO dept (dname) VALUES (?)"; AClass.forName ("Com.mysql.jdbc.Driver"); -Connection Connection =DriverManager -. getconnection ("Jdbc:mysql:///mydb", "root", "950613"); thePreparedStatement pstmt =connection.preparestatement (SQL); -Pstmt.setstring (1, "Test"); - Pstmt.execute (); -              + pstmt.close (); - connection.close (); +}Catch(Exception e) { A e.printstacktrace (); at         } -     } -}

Using DataSource

1  PackageCn.fuyi.h_jdbc;2 3 Importjava.sql.Connection;4 ImportJava.sql.DriverManager;5 Importjava.sql.PreparedStatement;6 7 ImportJavax.sql.DataSource;8 9  Public classUserdao {Ten      One     PrivateDataSource DataSource; A      Public voidSetdatasource (DataSource DataSource) { -          This. DataSource =DataSource; -     } the  -      Public voidSave () { -         Try { -String sql = "INSERT INTO dept (dname) VALUES (?)"; +Class.forName ("Com.mysql.jdbc.Driver"); -Connection Connection =datasource.getconnection (); +PreparedStatement pstmt =connection.preparestatement (SQL); APstmt.setstring (1, "test1111"); at Pstmt.execute (); -              - pstmt.close (); - connection.close (); -}Catch(Exception e) { - e.printstacktrace (); in         } -     } to}
1<bean id= "DataSource"class= "Com.mchange.v2.c3p0.ComboPooledDataSource" >2<property name= "Jdbcurl" value= "Jdbc:mysql:///mydb" ></property>3<property name= "Driverclass" value= "Com.mysql.jdbc.Driver" ></property>4<property name= "User" value= "root" ></property>5<property name= "Password" value= "950613" ></property>6<property name= "Initialpoolsize" value= "5" ></property>7<property name= "Maxpoolsize" value= "ten" ></property>8</bean>9     Ten<bean id= "Userdao"class= "Cn.fuyi.h_jdbc." Userdao "> One<property name= "DataSource" ref= "DataSource" ></property> A</bean>

Using JdbcTemplate:

1) Introduction of JAR file

Spring-jdbc-3.2.5.release.jar JDBC Support Package

Spring-tx-3.2.5.release.jar Transaction Pack

C3p0-0.9.1.2.jar c3p0
Mysql-connector-java-5.1.12-bin.jar Drive Class

1  PackageCn.fuyi.h_jdbc;2 3 Importjava.sql.Connection;4 ImportJava.sql.DriverManager;5 Importjava.sql.PreparedStatement;6 7 ImportJavax.sql.DataSource;8 9 Importorg.springframework.jdbc.core.JdbcTemplate;Ten  One  Public classUserdao { A      -     PrivateDataSource DataSource; -      Public voidSetdatasource (DataSource DataSource) { the          This. DataSource =DataSource; -     } -      -      Public voidsave1 () { +String sql = "INSERT INTO dept (dname) VALUES (' test123 ')"; -JdbcTemplate JdbcTemplate =NewJdbcTemplate (dataSource); + jdbctemplate.execute (SQL); A     } at}
1<bean id= "DataSource"class= "Com.mchange.v2.c3p0.ComboPooledDataSource" >2<property name= "Jdbcurl" value= "Jdbc:mysql:///mydb" ></property>3<property name= "Driverclass" value= "Com.mysql.jdbc.Driver" ></property>4<property name= "User" value= "root" ></property>5<property name= "Password" value= "950613" ></property>6<property name= "Initialpoolsize" value= "5" ></property>7<property name= "Maxpoolsize" value= "ten" ></property>8</bean>9     Ten<bean id= "Userdao"class= "Cn.fuyi.h_jdbc." Userdao "> One<property name= "DataSource" ref= "DataSource" ></property> A</bean>

Spring's coming, -05-jdbc.

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.