JDBC简介及数据源配置

来源:互联网 发布:快披软件 编辑:程序博客网 时间:2024/04/27 20:38

          Java中JDBC定义了java与各种sql数据库之间的编程接口,JDBC API是一个统一的标准应用程序编程接口,这样可以屏蔽异种数据库之间的差异。 一般来说JDBC驱动分为四种类型:1,jdbc-odbc桥,2,jdbc-native方法,3,jdbc-网络,4,jdbc驱动。

          第一种是需要调用odbc 的,所以在性能上会差一点,而且要求电脑上必须要有odbc 的库,这个用的比较少。

          第二种是本地API部分用java编写的驱动,使用过程中必须安装与数据库相关的客户 端程序,其实是驱动调用客户端程序再访问dbms

          第三种是jdbc网络纯java的驱动,jdbc调用网络中间件,然后再访问dbms

          第四种则是我们用的最多的一种,本地协议纯java的驱动,将jdbc调用转换为dbms的调用,中间需要用到jar包

         下面这篇文章简述了jdbc的优缺点,也可以看看:http://blog.csdn.net/zzj5385/article/details/4311892


          另外,连接数据库操作从内原理来讲就是先通过驱动获得连接,然后再通过连接对数据库进行处理,那么从操作上来看可分为3种:(1)直接将连接操作放在程序中,每次新建连接;(2)通过windows的数据源来配置,其中就会初始化若干个连接,但是这个用的好像比较少;(3)通过JNDI方式配置数据源,在web开发中经常用到。

         下面,通过一个例子程序来说明通过配置数据源,JDBC连接Mysql数据库的例子

(1)添加jar包: mysql-connector-java-5.1.12-bin.jar

(2)eclipse下Servers中配置server.xml的   <GlobalNamingResources> </GlobalNamingResources>中加入:

<Resource name="jdbc/webgpsDS"              auth="Container" type="javax.sql.DataSource"              username="root"  password="12345"              driverClassName="com.mysql.jdbc.Driver"              url="jdbc:mysql://localhost:3306/webgps"              maxActive="100" maxIdle="30" maxWait="5000"/>
(3)context.xml中的<Context></Context>中加入:

<ResourceLink name="jdbc/webgpsDS" global="jdbc/webgpsDS" type="javax.sql.DataSourcer"/>

(4)项目web-inf目录下web.xml文件中(最后)加入:(测试发现这步可有可无)

  <resource-ref>    <description>DB Connection</description>    <res-ref-name>jdbc/webgpsDS</res-ref-name>    <res-type>javax.sql.DataSource</res-type>    <res-auth>Container</res-auth>     </resource-ref> 

(5)java程序

package com;import java.sql.Connection;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import javax.naming.InitialContext;import javax.naming.NamingException;import javax.sql.DataSource;public class Dao {protected static InitialContext context = null; //不会重复初始化protected DataSource dataSource = null;public Dao() {try {            if(context == null) {                context = new InitialContext();            }            System.out.println("dao init");            dataSource = (DataSource) context.lookup("java:comp/env/jdbc/webgpsDS");                        Connection conn = dataSource.getConnection();       Statement stmt = conn.createStatement();       ResultSet rs = stmt.executeQuery("select * from TB_RR");       while (rs.next()) {    System.out.println("conn success!");    }            } catch (NamingException | SQLException e) {            e.printStackTrace();            System.out.println("conn fail:  "+e.toString());        }}}

        测试的servlet程序

package com;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;/** * Servlet implementation class ConnServlet */@WebServlet("/ConnServlet")public class ConnServlet extends HttpServlet {private static final long serialVersionUID = 1L;    public Dao dao = new Dao();    @Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {System.out.println("ConnServlet.doPost()");}    @Override    protected void doGet(HttpServletRequest req, HttpServletResponse resp)    throws ServletException, IOException {    this.doPost(req, resp);    }}
完整程序:http://pan.baidu.com/s/1gdy1aef



0 0
原创粉丝点击