Spring_JDBC连接

1.导入jarbao

2.创建pojo,dao,Impl
package com.tanlei.pojo;
public class Department {
private Long deptId;
private String deptNo;
private String deptName;
public Department() {
super();
}
public Department(Long deptId, String deptNo, String deptName) {
super();
this.deptId = deptId;
this.deptNo = deptNo;
this.deptName = deptName;
}
public Long getDeptId() {
return deptId;
}
public void setDeptId(Long deptId) {
this.deptId = deptId;
}
public String getDeptNo() {
return deptNo;
}
public void setDeptNo(String deptNo) {
this.deptNo = deptNo;
}
public String getDeptName() {
return deptName;
}
public void setDeptName(String deptName) {
this.deptName = deptName;
}
@Override
public String toString() {
return "Department [deptId=" + deptId + ", deptNo=" + deptNo + ", deptName=" + deptName + "]";
}
}
package com.tanlei.service;
import java.util.List;
import com.tanlei.pojo.Department;
public interface DepartmentDao {
public List<Department> queryDepartment() throws Exception;
}
package com.tanlei.service.impl; import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; import javax.sql.DataSource; import com.tanlei.pojo.Department;
import com.tanlei.service.DepartmentDao; public class DepartmentDaoImpl implements DepartmentDao {
public DataSource datasource; public void setDatasource(DataSource datasource) {
this.datasource = datasource;
} @Override
public List<Department> queryDepartment() throws Exception { //从数据源获取连接
Connection connection=datasource.getConnection();
//执行数据库操作
Statement statement=connection.createStatement();
//根据sql语句返回结果集
String sql="Select d.dept_id, d.dept_no, d.dept_name from department d";
ResultSet rSet=statement.executeQuery(sql);
List<Department>list =new ArrayList<Department>();
while(rSet.next()) {
Long deptId=rSet.getLong("dept_id");
String deptNo=rSet.getString("dept_no");
String deptName=rSet.getString("dept_name");
Department department=new Department(deptId, deptNo, deptName);
list.add(department);
}
return list;
} }
3.创建配置文件及bean文件及数据源文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="departmentDAO" class="com.tanlei.service.impl.DepartmentDaoImpl">
<property name="datasource" ref="dataSource"></property>
</bean> </beans>
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- 导入属性文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/> <!-- Spring配置数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${user}"></property>
<property name="password" value="${password}"></property>
<property name="driverClass" value="${driverClass}"></property>
<property name="jdbcUrl" value="${jdbcUrl}"></property>
</bean>
</beans>
#mysql
user=root
password=password
driverClass=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql://localhost:3306/mysql?serverTimezone=GMT%2B8
#oracle
#user=scott
#password=tiger
#driverClass=oracle.jdbc.OracleDriver
#jdbcUrl=jdbc:oracle:thin:@localhost:1521:ORCL
4.配置主配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--调用数据源集bean的配置文件xml -->
<import resource="dao/spring-department.xml" />
<!-- mysql -->
<import resource="database/spring-database-mysql.xml"/>
<!-- oracle -->
<!-- <import resource="oracledatabase/spring-databse-oracle.xml" /> -->
</beans>
5.创建运行类
package com.tanlei.dao; import java.util.List; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.tanlei.pojo.Department;
import com.tanlei.service.DepartmentDao; public class MainDao { public static void main(String[] args) throws Exception {
ApplicationContext context=new ClassPathXmlApplicationContext("spring-module.xml");
DepartmentDao departmentDao=(DepartmentDao) context.getBean("departmentDAO");
List<Department> departments=departmentDao.queryDepartment();
for (Department department : departments) {
System.out.println(department.toString());
System.out.println(department.getDeptId());
}
} }
6.运行结果

Spring_JDBC连接的更多相关文章
- nodejs进阶(6)—连接MySQL数据库
1. 建库连库 连接MySQL数据库需要安装支持 npm install mysql 我们需要提前安装按mysql sever端 建一个数据库mydb1 mysql> CREATE DATABA ...
- SQL Server 无法连接到服务器。SQL Server 复制需要有实际的服务器名称才能连接到服务器。请指定实际的服务器名称。
异常处理汇总-数据库系列 http://www.cnblogs.com/dunitian/p/4522990.html SQL性能优化汇总篇:http://www.cnblogs.com/dunit ...
- Linux 开机时网络自动连接
简单版本: cd /etc/sysconfig/network-scripts/ vi ifcfg-enoXXX 输入:reboot重启 或者输入:service network restart ...
- 在ubuntu16.10 PHP测试连接MySQL中出现Call to undefined function: mysql_connect()
1.问题: 测试php7.0 链接mysql数据库的时候发生错误: Fatal error: Uncaught Error: Call to undefined function mysqli_con ...
- 【初学python】使用python连接mysql数据查询结果并显示
因为测试工作经常需要与后台数据库进行数据比较和统计,所以采用python编写连接数据库脚本方便测试,提高工作效率,脚本如下(python连接mysql需要引入第三方库MySQLdb,百度下载安装) # ...
- 一起学微软Power BI系列-使用技巧(1)连接Oracle与Mysql数据库
说起Oracle数据库,以前没用过Oracle不知道,但是这1年用Oracle后,发现真的是想狂吐槽,特别是那个.NET驱动和链接字符串,特别奇葩.总归是和其他数据库不一样,标新立异,不知道为何.另外 ...
- 使用SecureCRT连接虚拟机(ubuntu)配置记录
这种配置方法,可以非常方便的操作虚拟机里的Linux系统,且让VMware在后台运行,因为有时候我直接在虚拟机里操作会稍微卡顿,或者切换速度不理想,使用该方法亲测本机效果确实ok,特此记录. Secu ...
- c# 字符串连接使用“+”和string.format格式化两种方式
参考文章:http://www.liangshunet.com/ca/201303/218815742.htm 字符串之间的连接常用的两种是:“+”连接.string.format格式化连接.Stri ...
- 如何使用本地账户"完整"安装 SharePoint Server 2010+解决“New-SPConfigurationDatabase : 无法连接到 SharePoint_Config 的 SQL Server 的数据 库 master。此数据库可能不存在,或当前用户没有连接权限。”
注:目前看到的解决本地账户完整安装SharePoint Server 2010的解决方案如下,但是,有但是的哦: 当我们选择了"完整"模式安装SharePointServer201 ...
随机推荐
- Lucene 评分机制一
1. 评分公式 1.1 公式介绍 这个公式是Lucene实际计算时使用的公式,是由原型公式推导而来 tf(t in d) 表示某个term的出现频率,定义了term t出现在当前document d的 ...
- HZOI 可怜与超市 树形dp
学长留的题,质量还是灰常高的. 而且我树规本身较弱,一道也不想放下 题目链接:https://www.cnblogs.com/Juve/articles/11203824.html 题解:这道题我们可 ...
- ajax发送验证码
$.ajax({ url:url, type:"POST", data:data, dataType:"JSON", s ...
- jeeCMS首页加载流程
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/gyshun/article/details/79669293 如果JEECMS部署完毕之后,在浏览器 ...
- switch的练习
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- Springboot2集成Activiti设计器并去除security
前言 鉴于项目需要将acitiviti设计器整合到原工程中,在网上查了不少资料都不太适用,经过借鉴和自己倒腾终于搞定了,分享一下经验,如果有问题,可以在留言区咨询. 文中用到的资源代码链接: http ...
- 64位电脑上安装MySQL进行MFC开发的相关问题
本人环境: 64位win7 + MySQL5.6 安装MySQL的时候有个选项是 选择:32位/64位.默认情况下是安装当前操作系统的位数.但我在使用VS进行开发的时候发现问题: error LNK2 ...
- C++stl中vector的几种常用构造方法
#include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #i ...
- 【html、CSS、javascript-2】CSS基础
CSS CSS是Cascading Style Sheets的简称,中文称为层叠样式表,用来控制网页数据的表现,可以使网页的表现与数据内容分离. 一 css的四种引入方式 1.行内式 ...
- Codeforces 455B
题目链接 B. A Lot of Games time limit per test 1 second memory limit per test 256 megabytes input standa ...