java-jdbc数据库连接

java

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数据库连接 的全部内容, 来源链接: utcz.com/z/390308.html

回到顶部