日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring_JDBC连接

發(fā)布時間:2025/3/18 javascript 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。