java-jdbc数据库连接
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数据库连接的更多相关文章
- Java jdbc数据库连接池总结!(转)
1. 引言 近年来,随着Internet/Intranet建网技术的飞速发展和在世界范围内的迅速普及,计算机 应用程序已从传统的桌面应用转到Web应用.基于B/S(Browser/Server)架构的 ...
- JAVA jdbc(数据库连接池)学习笔记(二) SQL注入
PS:今天偶然间发现了SQL的注入...所以就简单的脑补了一下,都是一些简单的例子...这篇写的不怎么样...由于自己没有进行很深的研究... 学习内容: 1.SQL注入的概念... 所谓SQL注 ...
- JAVA jdbc(数据库连接池)学习笔记(一)
学习内容: 1.JDBC的含义... JDBC想必学过JAVA的就不会陌生,JDBC到底是什么呢?其实就是由JAVA的一些类和接口构成的API,保存在java.sql和javax.sql..包中的一些 ...
- JAVA jdbc(数据库连接池)学习笔记(转)
学习内容: 1.JDBC的含义... JDBC想必学过JAVA的就不会陌生,JDBC到底是什么呢?其实就是由JAVA的一些类和接口构成的API,保存在java.sql和javax.sql..包中的一些 ...
- java JDBC数据库连接操作
import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public clas ...
- Java -- JDBC 数据库连接池
1. 原理代码示例 public class JdbcPool implements DataSource { private static LinkedList<Connection> ...
- Java自学-JDBC 数据库连接池
数据库连接池 与线程池类似的,数据库也有一个数据库连接池. 不过他们的实现思路是不一样的. 本章节讲解了自定义数据库连接池类:ConnectionPool,虽然不是很完善和健壮,但是足以帮助大家理解C ...
- JAVA之JDBC数据库连接池总结篇
JDBC数据库连接池 一.JDBC数据库连接池的必要性 二.数据库连接池技术 三.多种开源的数据库连接池 3.1 C3P0数据库连接池 3.2 DBCP数据库连接池 3.3 Druid(德鲁伊)数据库 ...
- JAVA基础知识之JDBC——JDBC数据库连接池
JDBC数据库连接池 数据库的连接和关闭是很耗费资源的操作,前面介绍的DriverManager方式获取的数据库连接,一个Connection对象就对应了一个物理数据库连接,每次操作都要打开一个连接, ...
- Java -- JDBC 学习--数据库连接池
JDBC数据库连接池的必要性 在使用开发基于数据库的web程序时,传统的模式基本是按以下步骤: 在主程序(如servlet.beans)中建立数据库连接. 进行sql操作 断开数据库连接. 这种模式开 ...
随机推荐
- 解决java.lang.NoClassDefFoundError: org/aopalliance/intercept/MethodInterceptor问题
hibernate整合spring当在spring配置文件中加入如下代码 <!--2.配置事务属性,需要事务管理器--> <tx:advice id="txAdvice&q ...
- Charles安装及配置
安装包及jar包下载地址: 1.下载Charles Proxy v4.2.dmg镜像文件,双击打开,将Charles拖拽到Applications中,Mac中打开一次Charles后关掉. 2.将下载 ...
- Java学习笔记13(equals()方法;toString()方法)
equals()方法: equals方法是Object类中的方法:Object是所有类的祖宗,所以所有类都有equals()方法: boolean equals(Object obj); equals ...
- 字体图标Font Awesome 的使用
下载地址:http://fontawesome.dashgame.com/ 将下载下来的压缩包解压,然后解压,将下载的整个文件夹复制到你的项目中,在你需要用字体图标的html中引入“font-awes ...
- ubuntu16.04系统安装
0x1镜像下载 (1)下载地址http://cn.ubuntu.com/download/ 0x2 安装 (1)打开vmware,创建新的虚拟机 (2)选择自定义安装 (3)直接下一步,选择稍后安装系 ...
- TensorRT caffemodel serialize
1.TensorRT的需要的文件 需要的基本文件(不是必须的) 1>网络结构文件(deploy.prototxt) 2>训练的权重模型(net.caffemodel) TensorRT 2 ...
- Linux批量解压文件
最近下载了Imagenet2012的数据文件,训练数据下有很多tar文件,这些tar文件都在一个目录内,所以想批量解压到该目录下每个单独的文件夹内 批量解压的步骤是, 1.列出所有的以tar为后缀的文 ...
- python调用caffe实现预测
对于已经训练完成的caffemodel,对于单个的图片预测,用python接口来调用是一件非常方便的事情,下面就来讲述如何用python调用已经训练完成的caffemodel,以及prototxt,网 ...
- SSH升级到7.7
#!/bin/bash#删除旧版ssh包 危险操作,不删除也可以安装,建议跳过此操作.#rpm -e `rpm -qa | grep openssh` #安装zlib依赖包wget -c http:/ ...
- CodeForces - 441D: Valera and Swaps(置换群)
A permutation p of length n is a sequence of distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n). A permu ...