---
title: "JDBC Call MySQL Proc"
author: "taink"
date: "2011-03-25T15:10:36Z"
tags: ["JDBC", "Java", "MySQL", "SQL"]
categories: ["企业架构"]
slug: "jdbc-call-mysql-proc"
draft: false
lastmod: "2011-03-25T15:10:36Z"
id: 975304
url: "https://taink.iteye.com/blog/975304"
---

**MySQL PROC :**

\-----------------------------------------------------------------------------------------------------------------------

 

CREATE DEFINER=\`root\`@\`%\` PROCEDURE \`proc\_sequence\`(OUT sequence varchar(25))  
BEGIN  
        SELECT CONCAT(prefix, imsi, Date\_format(yearmonthday,'%Y%m%d'), sufix) INTO sequence  
        FROM ra\_sequence  
        WHERE seq\_id=1 ;  
    UPDATE ra\_sequence SET yearmonthday = current\_date , sufix =sufix + 1  
        WHERE prefix='xg' AND imsi='00000000001' AND seq\_id = 1 ;  
END

 

\-----------------------------------------------------------------------------------------------------------------------

 

JAVA CODE

 

==================================================================

import java.sql.Connection;  
import java.sql.DriverManager;  
import java.sql.ResultSet;  
import java.sql.SQLException;  
import java.sql.Statement;

import com.mysql.jdbc.CallableStatement;

public class ConnectionMySQL {  
    public static Connection getConnection() throws Exception {  
        String driver = "org.gjt.mm.mysql.Driver";  
        String url = "jdbc:mysql://127.0.0.1:3306/redantsDB?useUnicode=true&amp;characterEncoding=UTF-8";  
        String username = "root";  
        String password = "123";

        Class.forName(driver);  
        Connection conn = (Connection) DriverManager.getConnection(url,  
                username, password);  
        return conn;  
    }

    public static void main(String\[] args) {

        Connection connection = null;  
        CallableStatement callableStatement = null;  
        try {  
            connection = getConnection();  
            String proc\_name = "{CALL proc\_sequence(?)}";  
            callableStatement = (CallableStatement) connection  
                    .prepareCall(proc\_name);  
            callableStatement.registerOutParameter(1, java.sql.Types.VARCHAR);  
            callableStatement.executeUpdate();

            System.out.println(callableStatement.getString(1));  
        } catch (Exception e) {  
            e.printStackTrace();  
        } finally {

            try {  
                if (null != callableStatement) {  
                    callableStatement.close();  
                }  
                if (null != connection) {  
                    connection.close();  
                }  
            } catch (SQLException e) {  
                e.printStackTrace();  
            }  
        }  
    }  
}