mysql返回一個結果集的儲存過程小例子

LemonSnm發表於2019-09-27

1、資料庫:

mysql資料庫,在mysql資料庫中新建一個tbtest資料庫,在tbtest庫中新建person表。欄位如下
    人員表(姓名(字串),手機(字串),地址(字串))

CREATE TABLE `person` (
  `p_name` varchar(50) DEFAULT NULL,
  `p_number` varchar(50) DEFAULT NULL,
  `p_address` varchar(50) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

建立好表,自己新增一點資料

 2、建立結果集:

2.1、要求:

1.儲存過程名稱:sp_test_get_persons
        2.返回含有2個欄位的結果集,一個欄位是int型別的id,另外一個是varchar(100)型別的name
        3.呼叫時,可以指定最大返回結果集中的資料個數。例如:call sp_test_get_persons(10) 呼叫後返回的結果集中最多10條記錄。

2.1、Navicat建立結果集

 

 

 

儲存,設定名字為:sp_test_get_persons

 一個簡單的結果集就ok了

 3、需要的jar包:

    1、maven專案在pom.xml中<dependencies>里加入這個:

            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.0.8</version>
            </dependency>

  2、或者 簡單的java專案,在加入jar包,並匯入專案依賴

mysql-connector-java5.0.8

4、新建java專案:

4.1、DBManager.java

建立jdbc工具類

package com.lemon.test;
 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
 
public class DBManager {
 
	 //資料庫資訊
    private static final String url = "jdbc:mysql://localhost:3306/tbtest";
    private static final String name = "com.mysql.jdbc.Driver";
    private static final String username = "root";
    private static final String password = "123456";
    
    private static Connection connection = null;
    private static PreparedStatement preparedStatement = null;
 
    //建立連線
    public DBManager(){
        try{
            Class.forName(name);
            connection = DriverManager.getConnection(url, username, password);
 
        }catch(Exception e){
            e.printStackTrace();
        }
    }
 
    //關閉連線
    private void close(){
        try{
            connection.close();
            preparedStatement.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    
    //查詢
    public void queryAll() throws SQLException{
    	String sql = "select * from person";
    	preparedStatement = connection.prepareStatement(sql);
    	
    	 String name,number,address;
 
         try
         {
             //執行sql語句
             ResultSet result = preparedStatement.executeQuery();
 
             //查詢到資料,對應賦值
             while(result.next()){
                 name = result.getString(1);
                 number = result.getString(2);
                 address = result.getString(3);
 
                 //顯示
                 System.out.println(name + ",\t" + number + ",\t" + address + ".\t");
             }
 
             result.close();
 
         }catch (Exception e){
             e.printStackTrace();
         }
    }
	
    //增加
    public void addPerson(String name ,String number ,String address) throws SQLException{
    	String sql = "insert into person (p_name,p_number,p_address) values (?,?,?)";
    	preparedStatement = connection.prepareStatement(sql);
    	preparedStatement.setString(1, name);
    	preparedStatement.setString(2, number);
    	preparedStatement.setString(3, address);
    	
    	int row = preparedStatement.executeUpdate();
    		if(row > 0){
    			System.out.println("新增成功!");
    	    }
    		preparedStatement.close();
    	    
    }
    
    //刪除
    public void delPersonByName(String name) throws SQLException{
    	String sql = "delete from person where p_name = ?";
    	//
    	preparedStatement = connection.prepareStatement(sql);
    	//完整sql語句
    	preparedStatement.setString(1, name);
 
    	int row = preparedStatement.executeUpdate();
    	if(row > 0){
    	    System.out.println("刪除成功!");
    	}else{
    	    System.out.println("請正確輸入需要刪除的姓名!");
    	}
    	preparedStatement.close();
    	    
    }
    
    //更新
    public void updatePerson(String name ,String number ,String address) throws SQLException{
 
    	String sql = "update person set";
    	if(number != null){
    		sql = sql + " p_number = ?";
    	}
    	if(number != null && address != null){
    	    sql = sql + ",";
        }
    	if(address != null){
    		sql = sql + "p_address = ?";
    	}
    	sql = sql + "where p_name = ?";
    	//
    	preparedStatement = connection.prepareStatement(sql);
 
    	int i = 1;
        //完整sql語句
        if(number != null){
            preparedStatement.setString(i, number);
            i++;
        }
        if(address != null){
            preparedStatement.setString(i, address);
            i++;
        }
        preparedStatement.setString(i, name);
        int row = preparedStatement.executeUpdate();
        if(row > 0){
            System.out.println("更新成功!");
        }else{
            System.out.println("請正確輸入需要更新的姓名!");
        }
        preparedStatement.close();
    }
}

4.2、test.java

測試類:

package com.lemon.test;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

import javax.swing.DefaultButtonModel;

public class test {

	public static void main(String[] args) throws SQLException, ClassNotFoundException {
        Scanner input = new Scanner(System.in);
        //例項化
        DBManager dbManager = new DBManager();
        
        //查詢
        //dbManager.queryAll();
        
        //新增
        //dbManager.addPerson("張三","123456789","上海");
        //dbManager.queryAll();
        
        //按照名字刪除
        //dbManager.delPersonByName("張三");
        //按照名字更新
        //dbManager.updatePerson("sss","123213","12321");

        System.out.println("請輸入返回的最大結果集");
        int num = input.nextInt();
        System.out.println(num);
        //返回結果集
        dbManager.printResult(num);
	}
}

 

 

 

 

 

相關文章