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 ...
随机推荐
- 常见的5个runtime exception
NullPointException(空指针异常),ArrIndexOutOfBoundsException(数组越界异常),ClassCastException(类型转换异常),ClassNotFo ...
- LUOGU P1903 [国家集训队]数颜色 / 维护队列
传送门 解题思路 带修莫队,第一次写,其实和普通莫队差不多,就是多了个时间轴,块分n^(2/3)最优,时间复杂度O(n^(5/3)). #include<iostream> #includ ...
- c语言学习笔记 - 枚举类型
今天学习了c语言的枚举类型的使用,可能是PHP里没使用过,开始看的时候还是觉得有点怪,后来做了下例子才理解,这里做个笔记记录一下. #include <stdio.h> enum anim ...
- Spring注解驱动开发(五)-----扩展原理
扩展原理 1.BeanPostProcessor-----bean后置处理器,bean创建对象初始化前后进行拦截工作的 2.BeanFactoryPostProcessor-----beanFacto ...
- C++星号的含义
[转载] [http://blog.sina.com.cn/s/blog_4a50d85b0100uk3c.html] 1.乘法运算符 2.定义指针 int *p = 0; 还是 int* p ...
- 转:IO模型-- 同步和阻塞,异步和非阻塞的区别
源地址 http://hi.baidu.com/deep_pro/item/db0c581af1c1f17e7b5f2534 这些词之间的区别难倒了很多人,还有什么同步阻塞, 同步非阻塞, 异步阻塞, ...
- Maven实战06_坐标和邮件服务模块
1:何为Maven坐标 为了能够自动化地解析任何一个Java构件,Maven就必须要将其唯一标识,这就是依赖管理的底层基础--坐标. 学过数学的人都知道平面直角坐标系,x,y分别为其横,纵坐标,将会在 ...
- git放弃本地所有未提交的修改
1.未添加至暂存区的 git checkout . 2.已添加至暂存区的 git reset HEAD . git checkout .
- [mybatis]Example的用法 标签: mybatis 2017-05-21 21:46 651人阅读 评论(11)
Example类是什么? Example类指定如何构建一个动态的where子句. 表中的每个non-BLOB列可以被包括在where子句中. 例子是展示此类用法的最好方式. Example类可以用来生 ...
- GYM100633J. Ceizenpok’s formula 扩展lucas模板
J. Ceizenpok’s formula time limit per test 2.0 s memory limit per test 256 MB input standard input o ...