MariaDB:启用Java存储过程

时间:2014-03-06 13:42:57

标签: java mysql stored-procedures mariadb

我试图弄清楚如何使用Java在MariaDB中编写存储过程。有一个补丁浮动,说明它是可能的。我不确定这是否已成为标准。

如果您有任何人使用Java成功为MariaDB / MySQL编写存储过程,请告诉我。

2 个答案:

答案 0 :(得分:2)

我认为问题是关于使用Java作为存储过程的语言,而不是SQL。这不可能。是的,Antony Curtis做了一些工作,这将使这种存储过程成为可能。唉,它不是MySQL / MariaDB或任何发行版的一部分。因此,至少你现在无法使用它。

Link to Antony's presentation about using external languages with MySQL

答案 1 :(得分:1)

Hej Martin,

我在一个月前做过这件事。我使用JDBC连接到MySQL数据库,但MariaDB是Drop-in的替代品,所以我认为它也适用于MariaDB实例。

这是我的工作示例代码:

JDBCConnection类

package de.professional_webworkx.blog.sp.connector;

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

import com.mysql.jdbc.Driver;

public class JDBCConnector {

    public static JDBCConnector INSTANCE;

    /*
     * We need some connection information
     * CHANGE THEM!
     */
    private static final String USER    = "USER";
    private static final String PASS    = "PASS";
    private static final String HOST    = "localhost";
    private static final String DB      = "DATABASE";
    private static final int    PORT    = 3306;
    private static final String URL     = "jdbc:mysql://"+HOST+":"+PORT+"/"+DB;
    private Connection connection;

    private java.sql.PreparedStatement statement;

    private JDBCConnector() {
        connect();
    }

    private void connect() {

        try {
            Driver driver = (Driver)Class.forName("com.mysql.jdbc.Driver").newInstance();
            DriverManager.registerDriver(driver);
            connection = DriverManager.getConnection(URL, USER, PASS);
        } catch (InstantiationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    public void closeConnection() {
        if(connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                System.err.println("Something went wrong while closing the connection");
            }
        }
    }

    public void getCustomerCount() {
        try {
            Statement statement = connection.createStatement();
            ResultSet rs = statement.executeQuery("SELECT COUNT(*) FROM customer;");
            while(rs != null && rs.next()){
                System.out.println(rs.getInt(1));
            }

        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void addStoredProcedure() {
        try {
            String sql = "create procedure myProc() "
                    + "BEGIN "
                    + "SELECT COUNT(*) as total FROM customer;"
                    + "END";
            statement = connection.prepareStatement(sql);
            statement.execute();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    public static JDBCConnector getInstance() {
        if(INSTANCE == null) {
            INSTANCE = new JDBCConnector();
        }

        return INSTANCE;
    }
}

然后开始吧:

package de.professional_webworkx.blog.sp;

import de.professional_webworkx.blog.sp.connector.JDBCConnector;


public class App 
{
    public static void main( String[] args )
    {
        JDBCConnector connector = JDBCConnector.getInstance();
        // create the Stored Procedure in your DB
        connector.addStoredProcedure();
    }
}

我还将此示例代码推送到GitHub