1. 程式人生 > >java-jdbc數據庫連接

java-jdbc數據庫連接

ref posit mbo 特殊 return str 3.x urn ssp

web.xml:(web.xml)

<!-- 加載spring容器 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring.xml,classpath:spring-mybatis.xml,classpath:spring-shiro.xml</param-value>
    </context-param>

mybatis.xml:(spring-mybatis.xml)

<?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:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd 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/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd"
> <!-- 導入資源文件 --> <!-- 加載db.properties文件中的內容,db.properties文件中key命名要有一定的特殊規則 --> <context:property-placeholder location="classpath:jdbc.properties" /> <!-- 配置C3P0數據源 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <!-- 數據庫的連接參數 --> <property name="user" value="${jdbc.user}"></property> <property name="password" value="${jdbc.password}"></property> <property name="driverClass" value="${jdbc.driverClass}"></property> <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property> <property name="initialPoolSize" value="${jdbc.initalPoolSize}"></property> <property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property> </bean> <!--配置NamedParameterJdbcTemplate,該對象可以使用具名參數,其沒有無參數的構造器,必須為其構造器指定參數 --> <bean id="namedParameterJdbcTemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate"> <constructor-arg ref="dataSource"></constructor-arg> </bean> </beans>

UserDao.java

@Repository
public class UserDao {
    public User findUserByLoginName(String loginName) {
        String sql = "select * from user where loginName = :loginName";
        User user = new User();
        user.setLoginName(loginName);
        //user.setPassWord(passWord);
        SqlParameterSource parameterSource = new BeanPropertySqlParameterSource(user);
        BeanPropertyRowMapper<User> rowMapper = new BeanPropertyRowMapper<User>(User.class);
        new JDBCMySql();
        NamedParameterJdbcTemplate namedParameterJdbcTemplate = JDBCMySql.namedParameterJdbcTemplate;
        try {
            user = namedParameterJdbcTemplate.queryForObject(sql,parameterSource,rowMapper);
        } catch (DataAccessException e) {
            return null;
        }
        return user;
    }
}
JDBCMySql.java
public class JDBCMySql {
    private static ApplicationContext ctx = null;
    public static NamedParameterJdbcTemplate namedParameterJdbcTemplate;
    {
        try {
            if (ctx == null) {
                ctx = new ClassPathXmlApplicationContext("spring-mybatis.xml");
            }
            namedParameterJdbcTemplate = (NamedParameterJdbcTemplate)ctx.getBean(NamedParameterJdbcTemplate.class);

            /*ComboPooledDataSource pool= (ComboPooledDataSource) ctx.getBean("dataSource");
            jdbcUser = pool.getUser();
            jdbcPassword = pool.getPassword();
            jdbcUrl = pool.getJdbcUrl();
            driverClass = pool.getDriverClass();
            initialPoolSize = pool.getInitialPoolSize();
            maxPoolSize = pool.getMaxPoolSize();*/
        } catch (Exception e) {
            System.out.println("錯誤:" +e.getMessage()+ e.getStackTrace());
        }
    }
}

java-jdbc數據庫連接