• 吴裕雄--天生自然轻量级JAVA EE企业应用开发Struts2Sping4Hibernate整合开发学习笔记:Spring_DataSource


    <?xml version="1.0" encoding="GBK"?>
    <project name="spring" basedir="." default="">
        <property name="src" value="src"/>
        <property name="dest" value="classes"/>
    
        <path id="classpath">
            <fileset dir="../../lib">
                <include name="**/*.jar"/>
            </fileset>
            <pathelement path="${dest}"/>
        </path>
    
        <target name="compile" description="Compile all source code">
            <delete dir="${dest}"/>
            <mkdir dir="${dest}"/>
            <copy todir="${dest}">
                <fileset dir="${src}">
                    <exclude name="**/*.java"/>
                </fileset>        
            </copy>
            <javac destdir="${dest}" debug="true" includeantruntime="yes"
                deprecation="false" optimize="false" failonerror="true">
                <src path="${src}"/>
                <classpath refid="classpath"/>
                <compilerarg value="-Xlint:deprecation"/>
            </javac>
        </target>
    
        <target name="run" description="Run the main class" depends="compile">
            <java classname="lee.BeanTest" fork="yes" failonerror="true">
                <classpath refid="classpath"/>
            </java>
        </target>
    
    </project>
    drop database if exists spring;
    create database spring;
    use spring;
    
    create table news_inf
    (
     news_id int auto_increment primary key,
     news_title varchar(255),
     news_content varchar(255)
    );
    <?xml version="1.0" encoding="GBK"?>
    <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://www.springframework.org/schema/beans"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
        <!-- 定义数据源Bean,使用C3P0数据源实现 -->
        <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
            destroy-method="close">
            <!-- 指定连接数据库的驱动 -->
            <property name="driverClass" value="com.mysql.jdbc.Driver"/>
            <!-- 指定连接数据库的URL -->
            <property name="jdbcUrl" value="jdbc:mysql://localhost/spring"/>
            <!-- 指定连接数据库的用户名 -->
            <property name="user" value="root"/>
            <!-- 指定连接数据库的密码 -->
            <property name="password" value="32147"/>
            <!-- 指定连接数据库连接池的最大连接数 -->
            <property name="maxPoolSize" value="200"/>
            <!-- 指定连接数据库连接池的最小连接数 -->
            <property name="minPoolSize" value="2"/>
            <!-- 指定连接数据库连接池的初始连接数 -->
            <property name="initialPoolSize" value="2"/>
            <!-- 指定连接数据库连接池的连接的最大空闲时间 -->
            <property name="maxIdleTime" value="200"/>
        </bean>
    </beans>
    package lee;
    
    import javax.sql.DataSource;
    import java.sql.*;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    /**
     * Description:
     * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
     * <br/>Copyright (C), 2001-2016, Yeeku.H.Lee
     * <br/>This program is protected by copyright laws.
     * <br/>Program Name:
     * <br/>Date:
     * @author  Yeeku.H.Lee kongyeeku@163.com
     * @version  1.0
     */
    public class BeanTest
    {
        public static void main(String[] args)
            throws Exception
        {
            // 实例化Spring容器。Spring容器负责实例化Bean
            ApplicationContext ctx =
                new ClassPathXmlApplicationContext("beans.xml");
            // 获取容器中id为dataSource的Bean
            DataSource ds = ctx.getBean("dataSource", DataSource.class);
            // 通过DataSource来获取数据库连接
            Connection conn = ds.getConnection();
            // 通过数据库连接获取PreparedStatement
            PreparedStatement pstmt = conn.prepareStatement(
                "insert into news_inf values(null , ? , ?)");
            pstmt.setString(1 , "疯狂Java联盟成立了");
            pstmt.setString(2 , "疯狂Java地址:www.crazyit.org");
            // 执行SQL语句
            pstmt.executeUpdate();
            // 清理资源,回收数据库连接资源。
            if (pstmt != null)pstmt.close();
            if (conn != null)conn.close();
        }
    }
  • 相关阅读:
    环境搭建-----IntelliJ idea之resin配置
    Maven的pom.xml文件详解------Build Settings
    JVM调优总结
    JAVA基础-栈与堆,static、final修饰符、内部类和Java内存分配
    JVM之字节码——Class文件格式
    java中的深复制和浅复制
    java创建对象的四种方式
    iOS获取所有机型
    博客已停止更新,请移步简书
    NSCache的简单使用
  • 原文地址:https://www.cnblogs.com/tszr/p/12370298.html
Copyright © 2020-2023  润新知