javax.sql.DataSource之api学习

本文详细介绍了javax.sql.DataSource接口,包括其三种实现方式:基本实现、连接池实现及分布式事务实现。此外,还介绍了如何通过DataSource对象获取数据库连接。

javax.sql.DataSource之api学习

介绍: DataSource 
一个与数据源对象代表物理数据源连接的工厂。 对于DriverManager工具,数据源对象是获得连接的首选方法。实现DataSource接口的对象通常会根据JavaTM命名和目录(JNDI)API的命名服务注册。
DataSource接口由驱动程序供应商实现。这里有3种类型的实现:
1、基本实现——生成一个标准连接对象。
2、连接池实现——产生一个将自动参与连接池的连接对象。这个实现与中间层连接池管理器一起工作。
3、分布式事务实现——生成可用于分布式事务的连接对象,并且几乎总是参与连接池。这个实现与中间层事务管理器一起工作,而且几乎总是与连接池管理器相关。
DataSource对象具有可以在必要时修改的属性。例如,如果数据源被转移到另一个服务器,则可以更改服务器的属性。好处是,由于数据源的属性可以更改,任何访问该数据源的代码都不需要更改。
通过DataSource对象访问的驱动程序不用在驱动程序管理器中注册。相反,数据源对象是通过查找操作检索的,然后用于创建连接对象。通过基本实现,通过DataSource对象获得的连接与通过DriverManager设施获得的连接相同。

Method Summary

Methods  
Modifier and Type Method and Description
Connection getConnection()
尝试建立与dataSource对象所代表的数据源的连接。
Connection getConnection(String username, String password)
尝试建立与dataSource对象所代表的数据源的连接。

Method Detail

  • getConnection
    Connection getConnection()
                             throws SQLException

    尝试建立与dataSource对象所代表的数据源的连接。

    Returns:
    a connection to the data source 与数据源的连接
    Throws:
    SQLException - if a database access error occurs  访问数据库错误
  • getConnection
    Connection getConnection(String username,
                           String password)
                             throws SQLException

    尝试建立与dataSource对象所代表的数据源的连接。

    Parameters:
    username - the database user on whose behalf the connection is being made
    password - the user's password
    Returns:
    a connection to the data source
    Throws:
    SQLException - if a database access error occurs
    Since:
    1.4













上面的方案调整一下,kettle的jndi在src/main/resources/simple-jndi/jdbc.properties,编译后在target/classes/simple-jndi/jdbc.properties,希望修改里面的连接能重新加载kettle环境。 SampleData/type=javax.sql.DataSource SampleData/driver=org.h2.Driver SampleData/url=jdbc:h2:file:samples/db/sampledb;IFEXISTS=TRUE SampleData/user=PENTAHO_USER SampleData/password=PASSWORD Quartz/type=javax.sql.DataSource Quartz/driver=org.hsqldb.jdbcDriver Quartz/url=jdbc:hsqldb:hsql://localhost/quartz Quartz/user=pentaho_user Quartz/password=password Hibernate/type=javax.sql.DataSource Hibernate/driver=org.hsqldb.jdbcDriver Hibernate/url=jdbc:hsqldb:hsql://localhost/hibernate Hibernate/user=hibuser Hibernate/password=password Shark/type=javax.sql.DataSource Shark/driver=org.hsqldb.jdbcDriver Shark/url=jdbc:hsqldb:hsql://localhost/shark Shark/user=sa Shark/password= PDI_Operations_Mart/type=javax.sql.DataSource PDI_Operations_Mart/driver=org.postgresql.Driver PDI_Operations_Mart/url=jdbc:postgresql://localhost:5432/hibernate?searchpath=pentaho_operations_mart PDI_Operations_Mart/user=hibuser PDI_Operations_Mart/password=password live_logging_info/type=javax.sql.DataSource live_logging_info/driver=org.postgresql.Driver live_logging_info/url=jdbc:postgresql://localhost:5432/hibernate?searchpath=pentaho_dilogs live_logging_info/user=hibuser live_logging_info/password=password #01552 BASE_BIMS_01552/type=javax.sql.DataSource BASE_BIMS_01552/driver=com.mysql.jdbc.Driver BASE_BIMS_01552/url=jdbc:mysql://192.168.168.223:3306/plasma_test?useUnicode=true&characterEncoding=utf8&useSSL=false&useOldAliasMetadataBehavior=true BASE_BIMS_01552/user=plasma BASE_BIMS_01552/password=szrouting2004plasma #122 BASE_BIMS_122/type=javax.sql.DataSource BASE_BIMS_122/driver=com.mysql.jdbc.Driver BASE_BIMS_122/url=jdbc:mysql://192.168.168.223:3306/plasma_nyjl?useUnicode=true&characterEncoding=utf8&useSSL=false&useOldAliasMetadataBehavior=true BASE_BIMS_122/user=plasma BASE_BIMS_122/password=Szrouting2004P(@$ma #01255 BASE_BIMS_01255/type=javax.sql.DataSource BASE_BIMS_01255/driver=com.mysql.jdbc.Driver BASE_BIMS_01255/url=jdbc:mysql://192.168.168.223:3306/plasma?useUnicode=true&characterEncoding=utf8&useSSL=false&useOldAliasMetadataBehavior=true BASE_BIMS_01255/user=plasma BASE_BIMS_01255/password=szrouting2004plasma #etl BASE_ETL/type=javax.sql.DataSource BASE_ETL/driver=oracle.jdbc.driver.OracleDriver BASE_ETL/url=jdbc:oracle:thin:@192.168.168.218:1521:orcl BASE_ETL/user=BASE_ETL BASE_ETL/password=szrouting2015bip #bim BASE_BIM/type=javax.sql.DataSource BASE_BIM/driver=oracle.jdbc.driver.OracleDriver BASE_BIM/url=jdbc:oracle:thin:@192.168.168.218:1521:orcl BASE_BIM/user=BIM_NY BASE_BIM/password=szrouting2015bip #api ETL_POSITIVE/type=javax.sql.DataSource ETL_POSITIVE/driver=oracle.jdbc.driver.OracleDriver ETL_POSITIVE/url=jdbc:oracle:thin:@192.168.168.218:1521:orcl ETL_POSITIVE/user=BASE_ETL ETL_POSITIVE/password=szrouting2015bip #api BIM_POSITIVE/type=javax.sql.DataSource BIM_POSITIVE/driver=oracle.jdbc.driver.OracleDriver BIM_POSITIVE/url=jdbc:oracle:thin:@192.168.168.218:1521:orcl BIM_POSITIVE/user=BIM_NY BIM_POSITIVE/password=szrouting2015bip
09-17
package com.itheima.config; import com.alibaba.druid.pool.DruidDataSource; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import javax.sql.DataSource; public class JdbcConfig { @Value("${jdbc.url}") private String url; @Value("${jdbc.password}") private String password; @Value("${jdbc.username}") private String username; @Value("${jdbc.driverClassName}") private String driverClassName; @Bean public DataSource dataSource(){ //1.创建druid的dataSource DruidDataSource druidDataSource = new DruidDataSource(); druidDataSource.setDriverClassName(driverClassName); druidDataSource.setUsername(username); druidDataSource.setPassword(password); druidDataSource.setUrl(url); return druidDataSource; } } package com.itheima.config; import org.junit.jupiter.api.Test; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.mapper.MapperScannerConfigurer; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import javax.sql.DataSource; @Configuration public class MyBatisConfig { //1.配置sqlSession工厂 @Test public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource){ SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean(); sessionFactoryBean.setTypeAliasesPackage("com.itheima.pojo"); sessionFactoryBean.setDataSource(dataSource); return sessionFactoryBean; } //2.扫包mapper包 @Bean public MapperScannerConfigurer mapperScannerConfigurer(){ MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer(); mapperScannerConfigurer.setBasePackage("com.itheima.dao"); return mapperScannerConfigurer; } } package com.itheima.config; import com.alibaba.druid.pool.DruidDataSource; import org.springframework.context.annotation.*; import javax.sql.DataSource; @Configuration @ComponentScan("com.itheima") @Import({JdbcConfig.class}) @PropertySource({"classpath:jdbc-8.properties"}) public class SpringConfig { // jdbc.username=root // jdbc.password=itheima // jdbc.url=jdbc:mysql://localhost:3306/spring_db?serverTimezone=Asia/Shanghai&characterEncoding=utf8&useUnicode=true&useSSL=false&allowPublicKeyRetrieval=true // jdbc.driverClassName=com.mysql.cj.jdbc.Driver } package com.itheima; import com.itheima.config.SpringConfig; import org.junit.Test; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import javax.sql.DataSource; import java.sql.SQLException; public class annotationTest { @Test public void test01() throws SQLException { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class); DataSource bean = ctx.getBean(DataSource.class); System.out.println(bean.getConnection()); System.out.println("bean = "+bean); ctx.close(); } }
最新发布
09-25
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值