简体   繁体   English

MariaDB:启用Java存储过程

[英]MariaDB: Enable Java Stored Procedure

I am trying to figure out how to use Java for writing stored procedures in MariaDB. 我试图弄清楚如何使用Java在MariaDB中编写存储过程。 There is a patch floating around stating that it is possible. 周围有一个补丁,说有可能。 I am unsure if this had become available standard. 我不确定这是否已成为标准。

If any of you have used Java to successfully write a stored procedure for MariaDB / MySQL please let me know. 如果您使用Java成功地为MariaDB / MySQL编写了存储过程,请告诉我。

I assume the question is about using Java as a language for stored procedures, rather than SQL. 我假设问题是关于使用Java作为存储过程的语言,而不是SQL。 It it not possible. 这是不可能的。 Yes there was some work by Antony Curtis, that would make this kind of stored procedures possible. 是的,安东尼·柯蒂斯做了一些工作,这将使这种存储过程成为可能。 Alas, it is not part of MySQL/MariaDB or any distribution. las,它不是MySQL / MariaDB或任何发行版的一部分。 Thus you won't be able to use it, at least right now. 因此,至少在现在,您将无法使用它。

Link to Antony's presentation about using external languages with MySQL 链接到有关在MySQL中使用外部语言的Antony的演示文稿

Hej Martin, Hej Martin,

i did this some month ago. 我一个月前做了。 I used a JDBC Connection to a MySQL Database, but MariaDB is the Drop-in replacement, so i think it will also work on a MariaDB Instance. 我使用了与MySQL数据库的JDBC连接,但是MariaDB是Drop-in的替代品,因此我认为它也可以在MariaDB实例上使用。

Here is my working example code: 这是我的工作示例代码:

JDBCConnection class 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;
    }
}

And start it: 并启动它:

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();
    }
}

I also pushed this example code to GitHub 我还将此示例代码推送到GitHub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM