當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring_JDBC连接
生活随笔
收集整理的這篇文章主要介紹了
Spring_JDBC连接
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
1.導入jarbao
?
?2.創(chuàng)建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;}@Overridepublic String toString() {return "Department [deptId=" + deptId + ", deptNo=" + deptNo + ", deptName=" + deptName + "]";}} View Code?
package com.tanlei.service;import java.util.List;import com.tanlei.pojo.Department;public interface DepartmentDao {public List<Department> queryDepartment() throws Exception; } View Code?
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;}@Overridepublic List<Department> queryDepartment() throws Exception {//從數(shù)據(jù)源獲取連接Connection connection=datasource.getConnection();//執(zhí)行數(shù)據(jù)庫操作Statement statement=connection.createStatement();//根據(jù)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;}} View Code?
?3.創(chuàng)建配置文件及bean文件及數(shù)據(jù)源文件
<?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.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"><!-- 導入屬性文件 --> <context:property-placeholder location="classpath:jdbc.properties"/><!-- Spring配置數(shù)據(jù)源 --><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"><!--調(diào)用數(shù)據(jù)源集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.創(chuàng)建運行類
?
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.運行結果
?
轉載于:https://www.cnblogs.com/tanlei-sxs/p/10131727.html
總結
以上是生活随笔為你收集整理的Spring_JDBC连接的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 禁止浏览器放大字体
- 下一篇: 前端之JavaScript:JS之DOM