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 ...
随机推荐
- BZOJ 2165: 大楼
Time Limit: 40 Sec Memory Limit: 259 MB Submit: 957 Solved: 353 [Submit][Status][Discuss] Descriptio ...
- JSON操作总结(转自陈新汉)
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,是理想的数据交换格式.同时,JSON是 JavaScript 原生格式,这意 ...
- day65——day69
目录 DAY65 课堂笔记 1.vue实例 2.插值表达式 3.文本指令 4.面向对象js 5.js函数补充 6.事件指令 7.属性指令 DAY66 课堂笔记 1.表单指令 2.条件指令 3.循环指令 ...
- 玩转vue的slot内容分发
vue的内容分发非常适合"固定部分+动态部分"的组件的场景,固定部分可以是结构固定,也可以是逻辑固定,比如下拉loading,下拉loading只是中间内容是动态的,而拉到底部都会 ...
- sudo 授权许可使用的su,也是受限制的su
sudo 的适用条件: 由于su 对切换到超级权限用户root后,权限的无限制性,所以su并不能担任多个管理员所管理的系统.如果用su 来切换到超级用户来管理系统,也不能明确哪些工作是由哪个管理员进行 ...
- 差分进化算法(DE)的C++面向对象方法实现
代码来源于网络,写得非常棒 /*DE_test *对相应的Matlab程序进行测试 */ #include <iostream> #include <cmath> #inclu ...
- 线性求所有数模p的乘法逆元
推理: 假如当前计算的是x在%p意义下的逆元,设$p=kx+y$,则 $\Large kx+y\equiv 0(mod\ p)$ 两边同时乘上$x^{-1}y^{-1}$(这里代表逆元) 则方程变为$ ...
- UVA10215The Largest/Smallest Box(小数精度)
本身很容易却因为评测机有毒的一道题,,,看网上题解说最后一个答案要加一个很小的数才能AC,据说是因为没有speci judge #include <iostream> #include & ...
- C++11的for循环的新用法
字符串 string str = "this is a string"; for(auto ch : str) cout << ch << endl; 等价 ...
- mybatis深入理解(七)-----MyBatis缓存机制的设计与实现
缓存设计 MyBatis将数据缓存设计成两级结构,分为一级缓存.二级缓存: 一级缓存是Session会话级别的缓存,位于表示一次数据库会话的SqlSession对象之中,又被称之为本地缓存.一级缓存是 ...