zoukankan      html  css  js  c++  java
  • spring-dbcp Demo

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.rimi</groupId>
      <artifactId>SpringDBCP</artifactId>
      <packaging>war</packaging>
      <version>0.0.1-SNAPSHOT</version>
      <name>SpringDBCP Maven Webapp</name>
      <url>http://maven.apache.org</url>
      <dependencies>
      
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>3.8.1</version>
          <scope>test</scope>
        </dependency>
        
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.3.9.RELEASE</version>
        </dependency>
        
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>4.3.9.RELEASE</version>
        </dependency>
        
        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.4</version>
        </dependency>
        
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.38</version>
        </dependency>
        
      </dependencies>
      <build>
        <finalName>SpringDBCP</finalName>
      </build>
    </project>
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:jdbc="http://www.springframework.org/schema/jdbc"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
            http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd">
        
        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
           <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
           <property name="url" value="jdbc:mysql://127.0.0.1:3306/j1702?useSSL=false"></property>
           <property name="username" value="root"></property>
           <property name="password" value="123456"></property>
            <property name="initialSize" value="10"></property>
           <property name="maxActive" value="100"></property>
           <property name="minIdle" value="10"></property>
           <property name="maxIdle" value="50"></property>
           <property name="defaultAutoCommit" value="false"></property>
           <property name="maxWait" value="1000"></property>
           <property name="timeBetweenEvictionRunsMillis" value="3600000"></property>
           <property name="minEvictableIdleTimeMillis" value="3600000"></property>
       </bean>
    
    </beans>
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    
    import org.apache.commons.dbcp.BasicDataSource;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import com.sun.jndi.ldap.Connection;
    
    public class App {
    
        public static void main(String[] args) {
        
            ApplicationContext ac=new ClassPathXmlApplicationContext("classpath:ApplicationConnext.xml");
            BasicDataSource ds=(BasicDataSource) ac.getBean("dataSource");
            java.sql.Connection conn = null;
            String sql="select * from user where name='admin'";
            PreparedStatement ps = null;
            ResultSet rSet=null;
            try {
                conn = ds.getConnection();
                ps =conn.prepareStatement(sql);
                rSet=ps.executeQuery();
                while(rSet.next()){
                    System.out.println("name:"+rSet.getString("name"));
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    
    }
  • 相关阅读:
    封装tip控件
    Javascirpt中创建对象的几种方式
    使用Servlet上传文件
    Struts2 基本配置
    使用JQuery实现手风琴布局
    winform下自绘提示框风格窗体
    环形进度条
    Oracle中获取当前时间半小时前的时间
    JSTL+MyEclipse8.5+Tomcat配置
    使用CSS和jQuery实现对话框
  • 原文地址:https://www.cnblogs.com/Jfh389987366/p/7375918.html
Copyright © 2011-2022 走看看