spring通過jdbc連接數(shù)據(jù)庫
本文實(shí)例為大家分享了spring通過jdbc連接數(shù)據(jù)庫的具體代碼,供大家參考,具體內(nèi)容如下
首先看下整個(gè)工程的架構(gòu)目錄:

需要的jar包:

一、建表
create table student( id int primary key auto_increment, name varchar(32), age int, phone varchar(32) );
二、新建與數(shù)據(jù)庫對(duì)應(yīng)JavaBean
package com.etoak.bean;
public class Student {
/**
* 一個(gè)標(biāo)準(zhǔn)的javaBean對(duì)象 :
* 表字段對(duì)應(yīng)的屬性
* 屬性對(duì)應(yīng)的getter、setter方法
* 無參構(gòu)造器
* 除id[主鍵]之外其他參數(shù)組成的構(gòu)造器
* 所有參數(shù)組成的構(gòu)造器
*/
private Integer id;
private String name;
private Integer age;
private String phone;
public Student() {
super();
}
public Student(String name, Integer age, String phone) {
super();
this.name = name;
this.age = age;
this.phone = phone;
}
public Student(Integer id, String name, Integer age, String phone) {
super();
this.id = id;
this.name = name;
this.age = age;
this.phone = phone;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
三、spring的applicationContext配置文件
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" 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-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd"> <!-- 使用spring提供的整合jdbc功能 需要導(dǎo)入DAO層提供的兩個(gè)jar包[spring-jdbc spring-tx] 通過ioc依賴注入 將JdbcTemplate注入給StuDaoImpl --> <bean id="dao" class="com.etoak.dao.StuDaoImpl"> <!-- name="jt" setJt(JdbcTemplate jt) ref="jt" id="jt" 自定義對(duì)象 ref="" --> <property name="jt" ref="jt"></property> </bean> <bean id="jt" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="ds"></property> </bean> <!-- 此時(shí)的JdbcTemplate還不具備數(shù)據(jù)庫連接能力 為了讓其具備數(shù)據(jù)庫連接能力,需要為其提供DataSource 連接池、數(shù)據(jù)源 setDataSource(DataSource ds) 需要在ioc容器中再配置一個(gè)DataSource對(duì)象: driverClassName url username password maxIdle maxActive maxWait DataSource 接口 1 實(shí)現(xiàn)類 BasicDataSource commons-dbcp.jar spring框架自帶了DataSource實(shí)現(xiàn)類 DriverManagerDataSource setDriverClassName(String driver) setUrl(String url) setUsername(String u) setPassword(String p) [ ref屬性 : 表示調(diào)用該方法需要注入的數(shù)據(jù)類型 : 自定義類型/引用類型 value屬性 : 表示調(diào)用該方法需要注入的數(shù)據(jù)類型 : 基本數(shù)據(jù)類型/String類型/Class類型 ] 2 工廠bean --> <bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/etoak"/> <property name="username" value="root"/> <property name="password" value="root"/> </bean> </beans>
四、編寫Dao
package com.etoak.dao;
import java.util.List;
import java.util.Map;
import org.springframework.jdbc.core.JdbcTemplate;
import com.etoak.bean.Student;
/**
* 使用jdbc方式對(duì)student表數(shù)據(jù)進(jìn)行CRUD操作
* 1 傳統(tǒng)的jdbc開發(fā)方式 [ConFactory ...]
* 2 spring提供的整合方案 JdbcTemplate
*/
public class StuDaoImpl {
private JdbcTemplate jt;
public void setJt(JdbcTemplate jt) {
this.jt = jt;
}
/**
* JdbcTemplate將連接數(shù)據(jù)庫執(zhí)行添加操作的流程封裝在其update(sql)
*/
public boolean addStu(Student stu){
String sql = "insert into student values(null,?,?,?)";
Object[] args = {stu.getName() , stu.getAge() , stu.getPhone()};
int result = jt.update(sql , args);
// result 執(zhí)行當(dāng)前操作影響的數(shù)據(jù)量
return result==1;
}
public boolean delStuById(Integer id){
String sql = "delete from student where id="+id;
return jt.update(sql)==1;
}
public boolean updateStu(Student stu){
String sql = "update student set name=?,age=?,phone=? where id=?";
Object[] args = {stu.getName() , stu.getAge() , stu.getPhone() , stu.getId()};
return jt.update(sql , args)==1;
}
/**
* jt.queryForMap(sql) - Map
* Jdbc不是ORM工具,不知道sql查詢的對(duì)應(yīng)哪個(gè)對(duì)象
* 只能將查詢出的關(guān)系型數(shù)據(jù)封裝在一個(gè)Map集合中返回
* {字段名=字段值,...}
* map.get("id/name/age/phone")
* 注意 :
* 在使用queryForMap(sql)查詢單條數(shù)據(jù)時(shí)
* 必須能夠確保根據(jù)傳入的sql語句能夠并且只能查詢出單條數(shù)據(jù)
* 否則使用該方法會(huì)拋出異常
*/
public Map selStuById(Integer id){
String sql = "select * from student where id="+id;
Map map = jt.queryForMap(sql);
return map;
}
// List<Map> 每一個(gè)student被封裝成了一個(gè)Map對(duì)象
public List selectAllStus(){
String sql = "select * from student";
return jt.queryForList(sql);
}
public int selectStuCount(){
String sql = "select count(*) from student";
return jt.queryForInt(sql);
}
public List selectStusByPage(int start , int max){
String sql = "select * from student limit ?,?";
Object[] args = {start , max};
return jt.queryForList(sql , args);
}
}
五、測試
package com.etoak.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.etoak.bean.Student;
import com.etoak.dao.StuDaoImpl;
public class Test {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
StuDaoImpl dao = (StuDaoImpl)ac.getBean("dao");
Student stu = new Student("sheldon",30,"111");
boolean flag = dao.addStu(stu);
System.out.println(flag);
}
}
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- java使用JDBC連接數(shù)據(jù)庫的五種方式(IDEA版)
- Java 如何使用JDBC連接數(shù)據(jù)庫
- Java連接數(shù)據(jù)庫JDBC技術(shù)之prepareStatement的詳細(xì)介紹
- JDBC利用C3P0數(shù)據(jù)庫連接池連接數(shù)據(jù)庫
- Java實(shí)現(xiàn)JDBC連接數(shù)據(jù)庫簡單案例
- java使用jdbc連接數(shù)據(jù)庫簡單實(shí)例
- Java基于JDBC連接數(shù)據(jù)庫及顯示數(shù)據(jù)操作示例
- Spring的連接數(shù)據(jù)庫以及JDBC模板(實(shí)例講解)
- Java中JDBC連接數(shù)據(jù)庫詳解
- java 中JDBC連接數(shù)據(jù)庫代碼和步驟詳解及實(shí)例代碼
- Spring Boot JDBC 連接數(shù)據(jù)庫示例
- Java編程中使用JDBC API連接數(shù)據(jù)庫和創(chuàng)建程序的方法
- java開發(fā)中基于JDBC連接數(shù)據(jù)庫實(shí)例總結(jié)
- Java基礎(chǔ)之JDBC的數(shù)據(jù)庫連接與基本操作
相關(guān)文章
Java 入門圖形用戶界面設(shè)計(jì)之事件處理上
圖形界面(簡稱GUI)是指采用圖形方式顯示的計(jì)算機(jī)操作用戶界面。與早期計(jì)算機(jī)使用的命令行界面相比,圖形界面對(duì)于用戶來說在視覺上更易于接受,本篇精講Java語言中關(guān)于圖形用戶界面的事件處理2022-02-02
springboot項(xiàng)目連接多種數(shù)據(jù)庫該如何操作詳析
在Spring Boot應(yīng)用中連接多個(gè)數(shù)據(jù)庫或數(shù)據(jù)源可以使用多種方式,下面這篇文章主要給大家介紹了關(guān)于springboot項(xiàng)目連接多種數(shù)據(jù)庫該如何操作的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-08-08
Java實(shí)現(xiàn)學(xué)生信息管理系統(tǒng)(使用數(shù)據(jù)庫)
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)學(xué)生信息管理系統(tǒng),使用數(shù)據(jù)庫,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01
手把手帶你理解java線程池之工作隊(duì)列workQueue
這篇文章主要介紹了java線程池之工作隊(duì)列workQueue,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-09-09
Java swing實(shí)現(xiàn)的計(jì)算器功能完整實(shí)例
這篇文章主要介紹了Java swing實(shí)現(xiàn)的計(jì)算器功能,結(jié)合完整實(shí)例形式分析了java基于swing組件實(shí)現(xiàn)計(jì)算器布局與運(yùn)算功能的具體操作技巧,需要的朋友可以參考下2017-12-12
Java線程池隊(duì)列PriorityBlockingQueue原理分析
這篇文章主要介紹了Java線程池隊(duì)列PriorityBlockingQueue原理分析,PriorityBlockingQueue隊(duì)列是?JDK1.5?的時(shí)候出來的一個(gè)阻塞隊(duì)列,但是該隊(duì)列入隊(duì)的時(shí)候是不會(huì)阻塞的,永遠(yuǎn)會(huì)加到隊(duì)尾,需要的朋友可以參考下2023-12-12
Java實(shí)現(xiàn) 基于密度的局部離群點(diǎn)檢測------lof算法
這篇文章主要介紹了Java實(shí)現(xiàn) 基于密度的局部離群點(diǎn)檢測------lof算法,本文通過算法概述,算法Java源碼,測試結(jié)果等方面一一進(jìn)行說明,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07

