zoukankan      html  css  js  c++  java
  • spring jdbc连接数据库

    1、在applicationContext.xml中配置jdbc bean

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
    <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property>  
    <property name="url" value="jdbc:oracle:thin:@10.217.3.2:1521:orcl"></property>  
    <property name="username" value="*"></property>  
    <property name="password" value="*"></property>  

    注:这里使用spring的datasource
    org.springframework.jdbc.datasource.DriverManagerDataSource
    可选的还有:
    org.springframework.jdbc.datasource.SingleConnectionDataSource
    区别: DriverManagerDataSource -> 在每一个连接请求时都新建一个连接
    SingleConnectionDataSource -> 在每一个连接请求时都返回同一个连接

    2、 获得dataSource bean对象

    ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");  
     ds = (DataSource)ctx.getBean("dataSource"); 

    3、获得连接对象Connection及Statement

    Connection conn = ds.getConnection();  
    Statement sm = conn.createStatement();

    4、执行向数据库插入记录操作

    String sqlString = "insert into bryanttesttable values(2,'bryant')";  
    sm.execute(sqlString);
  • 相关阅读:
    剑指Offer_08_跳台阶
    剑指Offer_07_斐波那契数列
    HDU 4283 You Are the One
    1B. Spreadsheets
    1A Theatre Square
    HDU 2476 String painter(记忆化搜索, DP)
    LightOJ 1422 Halloween Costumes(记忆化搜索)
    POJ 1651 Multiplication PuzzleDP方法:
    POJ 2955 Brackets (区间DP)
    HDU 5452 Minimum Cut
  • 原文地址:https://www.cnblogs.com/sprinng/p/4581541.html
Copyright © 2011-2022 走看看