日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

spring jdbctample方式訪問數據庫

作者:仰望星空的快樂 更新時間: 2022-05-10 編程語言

springjdbcTemplatetest訪問mysql數據庫,批量插入數據-Java文檔類資源-CSDN下載

這是傳統的javaweb方式訪問數據庫:

1.獲取 驅動

2.獲取連接 ----配置鏈接池,并從鏈接池獲取連接,就是鏈接池技術

3.創建執行sql語句的處理器 ----

Statement stat = conn.createStatement(); 或者 preparedstatement

或者dbutil輔助類,接收不同類型的數據庫查詢結果

4.執行sql

5.處理結果

6.回收資源

jdbctample

spring中提供了 jdbctample輔助查詢,不用再管連接池 回收資源 之類的

1.需要在xml中聲明 jdbctample,并提供數據源




    
        
        
        
        
    

    
        
    

    

2.調用時,@autowired聲明 jdbctample

package com.hxut.rj92.zyk.dao;

import com.hxut.rj92.zyk.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;
import java.util.Arrays;
import java.util.List;

@Component
public class UserdaoImpl implements Userdao{
    @Autowired

JdbcTemplate jdbcTemplate;

    @Override
    public int add_dao(Student student) {

        int a= jdbcTemplate.update(" insert  student values(?,?,?);", new Object[]{student.getName(), (Integer)student.getId(), student.getLoc()});
        return a;
    }
    @Override
    public int change_dao(Student student) {
        int a= jdbcTemplate.update(" update  student set name=?,loc=? where id=?;", new Object[]{student.getName(),student.getLoc(),(Integer)student.getId()  });
        return a;
    }
    public int del_dao(int id)
    {
        int a =jdbcTemplate.update("delete  from student where id =?",new Object [] {id});
        return  a;
    }
    public int select_counts_dao()//查詢行數
    {
        int a=jdbcTemplate.queryForObject("select count(*) from student",Integer.class);//第二個參數表示返回類型的class,因為是int類型,所以就是intenger
        return a;
    }
    public Student select_one_info_dao(int id)//查詢一行數據的詳細信息
    {   //注意student類中要有無參的構造函數
        String sql="select * from student where id=?";
        //第一個參數是sql語句,第二個是返回值類型(固定寫法),第三個是sql參數的值
        Student student=jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper(Student.class),id);
        return student;
    }
    public List select_all_list_dao()
    {//和查詢一個相比,使用的是query方法,參數沒變
        String sql="select * from student where id<=?";
        List students=jdbcTemplate.query(sql,new BeanPropertyRowMapper(Student.class),99);
       return  students;
    }
    //批量插入,一下插入好幾行數據
    public void add_batch_dao(List a)
    {
        //原理:a是一個列表,batchUpdate(sql,a)會遍歷這個列表,用列表中的每個元素去執行那句sql,
        // 所以返回值是一個int類型數組,里面是每句的執行情況,成功返回[1, 1, 1]
        String sql="insert into student values(?,?,?)";
        int [] result=jdbcTemplate.batchUpdate(sql,a);
        System.out.println(Arrays.toString(result));
    }
    public  void update_batch_dao(List a)
    {   //原理:a是一個列表,batchUpdate(sql,a)會遍歷這個列表,用列表中的每個元素去執行那句sql,
        // 所以返回值是一個int類型數組,里面是每句的執行情況,成功返回[4, 4, 3]
        //代表第一句改了四行,第二句四行,第三句三行
        String sql="update  student set name=?,loc=? where id=?;";
        int [] result=jdbcTemplate.batchUpdate(sql,a);
        System.out.println(Arrays.toString(result));
    }
    public  void delete_batch_dao(List a)
    {   //原理:a是一個列表,batchUpdate(sql,a)會遍歷這個列表,用列表中的每個元素去執行那句sql,
        // 所以返回值是一個int類型數組,里面是每句的執行情況,成功返回[4, 4, 3]
        //代表第一句改了四行,第二句四行,第三句三行
        String sql="delete from student where id=?";
        int [] result=jdbcTemplate.batchUpdate(sql,a);
        System.out.println(Arrays.toString(result));
    }

}

原文鏈接:https://blog.csdn.net/sharesb/article/details/123967037

欄目分類
最近更新