Java database access based on data sources

Source: Internet
Author: User
Tags time in milliseconds

Overview

The earliest accessible Java Access database is through the JDBC interface. Later, after work, it is common to configure the data source on the server (such as WebLogic), use the data source through Jndi, and recently need to dynamically construct the data source in the program, check some data, and record here.

System structure

I do not have a systematic study of UML, the specification of a variety of drawings do not understand the descriptive. So the above diagram is for reference only and does not guarantee that it is compliant.

For, need to explain:

    1. Dependencies between components, from top to bottom, that is, the components above depend on the following.
    2. Spring-jdbc and COMMONS-DBCP are the names of the respective jar packages, and the version number is ignored.

The functions of the above 5 components are as follows:

Component Name Component action
Database driver Package The JDBC implementation provided by each database vendor, in fact, the data source is also based on JDBC technology
Properties configuration file Related configuration of JDBC links, and related configuration of data sources
Commons-dbcp Provides implementations of data sources, dependencies and configuration files and driver packages
Spring-jdbc The main use of the JdbcTemplate tool class, and it relies on the data source, JdbcTemplate provides a lot of practical methods, please refer to the Spring API
Business code Database interaction based on JdbcTemplate

Component Content
  1. Configuration file
    Here we are through Org.apache.commons.dbcp.BasicDataSourceFactory.createDataSource (java.util.Properties prop) To create a data source instance,But requirementsPropertiesThe property name to andbasicdatasourcefactoryThe name of the declared field is consistent. Apart fromrelated configurations for JDBC links, others should (guess) have their default values, not the ones that must be configured. The contents of the configuration file are as follows:
    Driverclassname=oracle.jdbc.driver.OracleDriverurl=jdbc:oracle:thin: @dzzx-db1.hnisi.com.cn:1523/  Zxkfkusername=***password=***# If you wantto change the following config, make sure konw Meanings, Otherwise you shoulddon't to change theminitialsize =10maxidle =3minidle =2  Maxactive=10maxwait=10000
    The name of each property can refer to basicdatasourcefactory source code, here are just a few of the examples I've used:
      • Driverclassname
        Driver class name
      • Url
        Database connection URL, different databases are written differently
      • Username
        Database user Name
      • Password
        Database user Password
      • InitialSize
        Number of data source connection pool initial connections
      • Maxidle
        Maximum number of free connections allowed in the connection pool
      • Minidle
        Minimum number of free connections allowed in the connection pool
      • Maxactive
        Maximum number of active connections allowed in the connection pool
      • Maxwait
        Maximum wait time in milliseconds for a connection

    Of the above configuration items, the first four are the necessary JDBC connection configurations, which are often encountered in JDBC programming, and the next five are data source connection pool-related configurations, which, according to their introduction, can be interpreted with a conjecture scenario:

    According to the above configuration, there will be 10 database connections in the initial case (InitialSize)
    In the initial case, there is generally no database access, that is, 10 connections are idle, so the connection will be freed, the number of connections will be reduced to 3 (maxidle)--so when configured initialsize=maxidle better
    A new connection is created when a connection is requested and the number of idle connections is reduced by less than 2 (minidle)
    If the business code requests a connection, 10 seconds (maxwait) has not been acquired and may throw an exception
    After the business code is used, the number of idle connections in the connection pool increases after the connection is freed, so the connection is reduced and the Maxidle limit is maintained

    Again, the above pattern is just speculation, not tested.

  2. Create a data source
    We passjava.io.InputStream Java.lang.Class.getResourceAsStream (String name)To load the resource file and get InputstrInputStreamEAM instance; void java.util.Properties.load (inputstream instream)To load the property configuration toPropertiesexample;DataSource Org.apache.commons.dbcp.BasicDataSourceFactory.createDataSource (Properties prop)To get the data source instance.
    New= sendutil. class. getResourceAsStream ("/cn/sinobest/jzpt/jgbm/common/message/sendmessage.properties"= Basicdatasourcefactory.createdatasource (prop);
    Of course, you can create a data source in a normal way without the Factory mode:
    New Basicdatasource (); Bds.setdriverclassname ("Oracle.jdbc.driver.OracleDriver"); Bds.seturl ("jdbc : Oracle:thin: @dzzx-db1.hnisi.com.cn:1523/zxkfk "); Bds.setusername (" * * * "); Bds.setpassword (" * * * ");        Bds.setinitialsize,bds.setmaxidle (3); Bds.setminidle (2); Bds.setmaxactive (10 ); bds.setmaxwait (10 * 1000);
  3. Accessing the database
    I prefer to useorg.springframework.jdbc.core.JdbcTemplateThis tool class, he has a package for adding and removing changes, support batch processing; there is no special syntax for writing JDBC code that can be easily transitioned. We only need to provide the data source instance, he is responsible for the database connection acquisition and release, we just provide SQL statements, parameters and parameter types, the others do not have to worry about us. My use example is as follows:
    String sql = "INSERT into S_sms_waiting_list (systemid,receiver,msg_content,senttime) VALUES (?,?,?,?)" String SystemID= "1"; String Phone= "159********"; String content= "You have a text message"; String currtime=new= {systemid, phone, content, currtime}; int [] argtypes =new  JdbcTemplate (DS);//data source created in the previous step jdbctem.update (sql, args, argtypes);

    For more JdbcTemplate methods, please consult the relevant API.

2015-08-19 19:42:02

Java database access based on data sources

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.