Java Learning Database Operation traditional JDBC, SPRINGJDBC

Source: Internet
Author: User

Spring makes Java development easy and saves developers a lot of time, including connection and use of databases. Here are three ways to connect to a database: the traditional JDBC approach, the SPRINGJDBC approach.

1. Traditional JDBC Approach

First the project needs to lead Mysql-connector-java jar Package

Or use MAVEN to import jar packages

        <dependency>            <groupId>mysql</groupId>            <artifactid>mysql-connector-java</ artifactid>            <version>5.1.38</version>        </dependency>

The code is as follows:

//1 Configuration by direct assignment//String url = "Jdbc:mysql:///dbname ";//String user = "root";//String password = "";//String Driverclass = "Com.mysql.jdbc.Driver";//2 through the. Properties configuration file Read configuration InputStream InputStream = Jdbcutil.Class.getclassloader (). getResourceAsStream ("Dbconfig.properties"); Properties Properties =NewProperties (); Properties.load (InputStream); String url = properties.getproperty ("Jdbc.url"); String user = Properties.getproperty ("Jdbc.user"); String Password = properties.getproperty ("Jdbc.password"); String Driverclass = Properties.getproperty ("Jdbc.driverclass");//Connection operationClass.forName (Driverclass); Connection Connection = drivermanager.getconnection (URL, user, password); // Crud (add and revise) operation String sql = "Sql_crud_statement" ; PreparedStatement = connection.preparestatement (sql); ResultSet = Preparedstatement.executequery (); // parse result Student Student =  Null; while (Resultset.next ()) { int id = resultset.getint ("id" int age = Resultset.getint ("Age" //do sth}          

Analysis: This traditional connection method is simple to implement, but the code is relatively large, and after the use of manual release of resources, relative need to have a lot of reread code.

2.srping JDBC Mode

Of course, the first task is to introduce a package that involves a little bit more of the package, so prefer maven to import

<!--MySQL driver--><dependency>    <groupId>mysql</groupId>    <artifactId> mysql-connector-java</artifactid>    <version>5.1.38</version></dependency><!-- Spring--><dependency><groupid>org.springframework</groupid><artifactid>spring-jdbc </artifactId><version>4.3.5.RELEASE</version></dependency><dependency>< Groupid>org.springframework</groupid><artifactid>spring-context</artifactid><version >4.3.5.RELEASE</version></dependency>

Once configured, you can make an exciting link to database operations.

The approximate process is to first obtain the JdbcTemplate (data template) object through the context mechanism inside the framework, and then perform curd operations through the JdbcTemplate object:

1. XML Configuration for context Beans.xml
<?xml version= "1.0" encoding= "UTF-8"? ><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.xsd ">    <bean id=" DataSource " class= "Org.springframework.jdbc.datasource.DriverManagerDataSource" >        <property name= "Driverclassname" Value= "Com.mysql.jdbc.Driver"/>        <property name= "username" value= "root"/>        <property name= " Password "value=" root "/>        <property name=" url "value=" Jdbc:mysql:///spring_data "/>    </bean>    <bean id= "JdbcTemplate" class= "org.springframework.jdbc.core.JdbcTemplate" >        <property name= " DataSource "ref=" DataSource "/>    </bean></beans>
2. Load XML to get JdbcTemplate object, implement Curd
CTX = new Classpathxmlapplicationcontext ("Beans.xml"), JdbcTemplate = Ctx.getbean ("JdbcTemplate"); jdbctemplate.query (SQL, new RowCallbackHandler () {///override Processrow to implement its own loop-processing result set    @Override public    void Processrow (ResultSet rs) Throws SQLException {        int id = rs.getint ("id");        String name = rs.getstring ("name"); Do  sth    }});

Analysis: Although this approach requires configuration context, but it is easy to implement, the implementation of the code on a few lines, look at the effect is good, and the JdbcTemplate object is already implemented object, we just need to rewrite the Processrow method inside can be implemented. This approach is a lot more sophisticated than the traditional approach, and there is no need to consider releasing resources.

However this is not all the framework wants, if you want to other operations, such as paging operation, it is not easy to implement, all the SQL also need to write their own, is not very convenient ah, then the problem comes, there is no real "good" way to achieve it? The next article will analyze spring implementation curd and its advantages.

Java Learning Database Operation traditional JDBC, SPRINGJDBC

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.