MySQL預存程序執行個體

來源:互聯網
上載者:User

標籤:儲存   .com   sql預存程序   driver   callable   getc   cut   cal   public   

一、建立MySQL資料庫函數

 

TCC:無參數,查詢fruit表中的所有資料

 

TAA:兩個參數,查詢fruit總共有多少行;查詢ids為某個值時水果表的資料

 

TDD:兩個參數,查詢ids不等於某個值,數量大於某個值,水果表的資料

二、Java操作MySQL函數

package com.itnba.maya.cunchu;import java.util.*;import java.sql.*;public class Test {    public static void main(String[] args) throws Exception{        String div="com.mysql.jdbc.Driver";        String url="jdbc:mysql://127.0.0.1:3306/mydb?characterEncoding=GBK";        String uid="root";        String pwd="";                Class.forName(div);        Connection conn = DriverManager.getConnection(url,uid,pwd);        //CallableStatement stat = conn.prepareCall("call TCC()");    //調用沒有參數的預存程序                //CallableStatement stat = conn.prepareCall("call TAA(?,?)");//調有參數的預存程序(輸入參數)        //stat.setString(1, "k002");        //stat.setInt(2, 99);                                CallableStatement stat = conn.prepareCall("call TDD(?,?)");//調有參的預存程序(輸出參數)        stat.setString(1, "k001");        stat.setInt(2,80);        ResultSet rs = stat.executeQuery();            while(rs.next()){            System.out.println(rs.getString(2));        }        conn.close();    }}

運行結果

菠蘿桔子葡萄櫻桃桃子香蕉草莓

 

 

對照fruit表

 

MySQL預存程序執行個體

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.