jdbc是数据库中间代理商为了是java连接数据库而定的一个协议 或者说是方法,通过jdbc-odbc的方法进行数据库连接,是连接一次创建一次的做法 1,由数据库驱动和协议组成连接数据的方法,调用中间商提供的类包,进行创建连接 2.再由连接去调用3种状态. 3.由状态来提供对数据库的操作 jndi是一种命名树的方式,把需要的类都列成目录的样式,需要哪个只要根据命名直接去 调用,是一种比较快洁的思想和行为. 当你要操作数据库的用jdbc 操作命名服务用jndi Java Database Connectivity (JDBC)是一个标准的Java API,它由一组类和接口组成,Java应用程序开发人员使用它来访问数据库和执行SQL语句 JNDI(Java Name Directory Interface),可不仅仅是进行数据库定位的, 它是给当前应用服务器所管理的所有资源一个唯一的标识,包括数据库,网页,文件, 连接池等等。
Connection conn = null; try{ Class.forName("com.mysql.jdbc.Driver",true,Thread.currentThread().getContextClassLoader()); conn = DriverManager.getConnection("jdbc:mysql://MyDBServer?user=***&password=****"); ..... conn.close(); } catch(...){...}finally{ if(conn!=null){ try{ conn.close(); } catch(...){...} } } 存在的问题? //jndi连接 Context initCtx = new InitialContext(); Context envCtx = (Context)initCtx.lookup("java:comp/env"); DataSource ds = (DataSource)envCtx.lookup("jdbc/webtest"); conn = ds.getConnection(); //jdbc连接 Class.forName("com.mysql.jdbc.Driver",true,Thread.currentThread().getContextClassLoader()); conn = DriverManager.getConnection("jdbc:mysql://localhost/mismain?user=root&autoReconnect=true"); 程序员开发时,知道要开发访问MySQL数据库的应用,于是将一个对 MySQL JDBC 驱动程序类的引用进行了编码,并通过使用适当的 JDBC URL 连接到数据库。 Connection conn=null; try ...{ Class.forName("com.mysql.jdbc.Driver", true, Thread.currentThread().getContextClassLoader()); conn=DriverManager.getConnection("jdbc:mysql://MyDBServer?user=qingfeng&password=mingyue"); ...... conn.close(); } catch(Exception e) ...{ e.printStackTrace(); } finally ...{ if(conn!=null) ...{ try ...{ conn.close(); } catch(SQLException e) ...{} } } 这是传统的做法,也是以前非Java程序员(如Delphi、VB等)常见的做法。这种做法一般在小规模的开发过程中不会产生问题,只要程序员熟悉Java语言、了解JDBC技术和MySQL,可以很快开发出相应的应用程序。 在JBoss 的 D:\jboss420GA\docs\examples\jca 文件夹下面,有很多不同数据库引用的数据源定义模板。将其中的 mysql-ds.xml 文件Copy到你使用的服务器下,如 D:\jboss420GA\server\default\deploy。 Connection conn=null; try ...{ Class.forName("com.mysql.jdbc.Driver", true, Thread.currentThread().getContextClassLoader()); conn=DriverManager.getConnection("jdbc:mysql://MyDBServer?user=qingfeng&password=mingyue"); ...... conn.close(); } catch(Exception e) ...{ e.printStackTrace(); } finally ...{ if(conn!=null) ...{ try ...{ conn.close(); } catch(SQLException e) ...{} } } <?xml version="1.0" encoding="UTF-8"?> <datasources> <local-tx-datasource> <jndi-name>MySqlDS</jndi-name> <connection-url>jdbc:mysql://localhost:3306/lw</connection-url> <driver-class>com.mysql.jdbc.Driver</driver-class> <user-name>root</user-name> <password>rootpassword</password> <exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter</exception-sorter-class-name> <metadata> <type-mapping>mySQL</type-mapping> </metadata> </local-tx-datasource> </datasources> 1、这里,定义了一个名为MySqlDS的数据源,其参数包括JDBC的URL,驱动类名,用户名及密码等。 Connection conn=null;try ...{ Context ctx=new InitialContext(); Object datasourceRef=ctx.lookup("java:MySqlDS"); //引用数据源 DataSource ds=(Datasource)datasourceRef; conn=ds.getConnection(); ...... c.close(); } catch(Exception e) ...{ e.printStackTrace(); } finally ...{ if(conn!=null) ...{ try ...{ conn.close(); } catch(SQLException e) ...{ } } } 直接使用JDBC或者通过JNDI引用数据源的编程代码量相差无几,但是现在的程序可以不用关心具体JDBC参数了。 |
|
来自: liang1234_ > 《jndi》