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连接的更多相关文章

  1. nodejs进阶(6)—连接MySQL数据库

    1. 建库连库 连接MySQL数据库需要安装支持 npm install mysql 我们需要提前安装按mysql sever端 建一个数据库mydb1 mysql> CREATE DATABA ...

  2. SQL Server 无法连接到服务器。SQL Server 复制需要有实际的服务器名称才能连接到服务器。请指定实际的服务器名称。

    异常处理汇总-数据库系列  http://www.cnblogs.com/dunitian/p/4522990.html SQL性能优化汇总篇:http://www.cnblogs.com/dunit ...

  3. Linux 开机时网络自动连接

      简单版本: cd /etc/sysconfig/network-scripts/ vi ifcfg-enoXXX 输入:reboot重启 或者输入:service network restart ...

  4. 在ubuntu16.10 PHP测试连接MySQL中出现Call to undefined function: mysql_connect()

    1.问题: 测试php7.0 链接mysql数据库的时候发生错误: Fatal error: Uncaught Error: Call to undefined function mysqli_con ...

  5. 【初学python】使用python连接mysql数据查询结果并显示

    因为测试工作经常需要与后台数据库进行数据比较和统计,所以采用python编写连接数据库脚本方便测试,提高工作效率,脚本如下(python连接mysql需要引入第三方库MySQLdb,百度下载安装) # ...

  6. 一起学微软Power BI系列-使用技巧(1)连接Oracle与Mysql数据库

    说起Oracle数据库,以前没用过Oracle不知道,但是这1年用Oracle后,发现真的是想狂吐槽,特别是那个.NET驱动和链接字符串,特别奇葩.总归是和其他数据库不一样,标新立异,不知道为何.另外 ...

  7. 使用SecureCRT连接虚拟机(ubuntu)配置记录

    这种配置方法,可以非常方便的操作虚拟机里的Linux系统,且让VMware在后台运行,因为有时候我直接在虚拟机里操作会稍微卡顿,或者切换速度不理想,使用该方法亲测本机效果确实ok,特此记录. Secu ...

  8. c# 字符串连接使用“+”和string.format格式化两种方式

    参考文章:http://www.liangshunet.com/ca/201303/218815742.htm 字符串之间的连接常用的两种是:“+”连接.string.format格式化连接.Stri ...

  9. 如何使用本地账户"完整"安装 SharePoint Server 2010+解决“New-SPConfigurationDatabase : 无法连接到 SharePoint_Config 的 SQL Server 的数据 库 master。此数据库可能不存在,或当前用户没有连接权限。”

    注:目前看到的解决本地账户完整安装SharePoint Server 2010的解决方案如下,但是,有但是的哦: 当我们选择了"完整"模式安装SharePointServer201 ...

随机推荐

  1. vue.js_02_vue.js的基础指令

    1.v-cloak 作用:解决插值表达式闪烁的问题 当网速过慢时,或者加载数据时间过长时,网页会出现  {{msg}}  的现象 使用方法: <!--缺陷需要写style样式--> < ...

  2. day67test

    作业 1.按照上方 知识点总结 模块,总结今天所学知识点: 2.有以下广告数据(实际数据命名可以略做调整) ad_data = { tv: [ {img: 'img/tv/001.png', titl ...

  3. python中os的常用方法

    1.os模块:os模块在python中包含普遍的操作系统功能,下面列出了一些在os模块中比较有用的部分. os.sep可以取代操作系统特定的路径分隔符.windows下为 “\\” os.name字符 ...

  4. Java IO : NIO与IO的区别

    参考资料:https://dzone.com/articles/java-nio-vs-io NIO是面向流的,IO是面向缓冲流的. NIO是非线程阻塞的,IO是线程阻塞的. NIO的Selector ...

  5. 一次.NET项目反编译的实战经验(WinForm)

    最近由于业务需求,需要对一个老项目进行功能调整.但是项目的源代码已经找不到了.所以只能尝试对项目行进反编译. 一.反编译工具的选择 提到.NET的反编译,第一个想到的就是大名鼎鼎的Reflector. ...

  6. 【python之路42】web框架们的具体用法

    Python的WEB框架 (一).Bottle Bottle是一个快速.简洁.轻量级的基于WSIG的微型Web框架,此框架只由一个 .py 文件,除了Python的标准库外,其不依赖任何其他模块. p ...

  7. bzoj 1093 [ZJOI2007]最大半连通子图——缩点+拓扑

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1093 缩点+拓扑,更新长度的时候维护方案数. 结果没想到处理缩点后的重边,这样的话方案数会算 ...

  8. swiper 报错 ‘ Can't find variable: Dom7’

    一般报这个错是因为浏览器兼容问题,例如低版本的IE 现在通过npm install swiper 安装的版本都是4.x的 我的解决方法就是安装低版本的swiper, npm install swipe ...

  9. js数组操作方法

    数组的操作方法在js中可谓是诸子百家,各种某乱的方法让人眼花缭乱,有时候就会分不清什么是自带的什么是曾今写的api了 数组的增删 1 . push() , pop()  和 shift() unshi ...

  10. gin入门-1

    Gin框架介绍 1. 简介Gin框架介绍A. 基于httprouter开发的web框架.http://github.com/julienschmidt/httprouterB. 提供Martini风格 ...