Link to home
Start Free TrialLog in
Avatar of marrowyung
marrowyung

asked on

can anyone program/execute java in MariaDB ?

hi,

does mariaDB support java programming ?

I know oracle can .
Avatar of John Tsioumpris
John Tsioumpris
Flag of Greece image

I think you need to read this
Avatar of marrowyung
marrowyung

ASKER

sir,

that one just a connector, not coding java in MariaDB, right?
What exactly do you mean "coding java in MariaDB"
java is a programming language and MariaDB is a SQL engine
In Java you create applications that interact with SQL Engines (MariaDB for example)
SQL Engines use for programming their own language -SQL - with each engine having small variations on the implementation...but their language is only for data manipulation...you cannot for example create autonomous applications.
On the other hand...i don't know about MariaDB...in MsSQL you can create extensions in .NET and use these extensions in MsSQL in order to extend its functionality.
"What exactly do you mean "coding java in MariaDB"
java is a programming language and MariaDB is a SQL engine"

just like we have an oracle function that call java, but this kind of function can't convert to MariaDB, so I am not sure if MariaDB support java.

".in MsSQL you can create extensions in .NET and use these extensions in MsSQL in order to extend its functionality."

you mean you only know MS SQL ?
Now we are getting somewhere...i just took a look on the Oracle...its pretty much as for MsSQL..it can call Java methods directly from Oracle server.
Ok for MySQL (i expect the same for MariaDB) there seems to be a "plugin" that allows this kind of functionality...you can read about it here....but from a little search i made it doesn't seem to be widespread.
sir,

the writer say:

Note: Be advised that the MySQL server we will build is likely to crash at times. Do not use it in production! And of course, always have a safe backup of important data.

do not use it in production.
this is our code:

create or replace FUNCTION javaConvert(
	fieldValue VARCHAR2
)
RETURN STRING AS LANGUAGE JAVA
NAME 'charconverter.CharacterConverter.convert(java.lang.String) return java.lang.String';
 

Open in new window


is it a java ?
You didn't include in your question that you need it in production.
After all ...using external code to extend the functionality of a SQL Engine is not the wiser thing to do because in the end you will have 2 possible causes of error..one from the SQL Engine and one from the 3rd part extension.
That's why ...even if  this feature is quite old in MsSQL ecosystem (for comparison) is not that widely accepted... and people are striving for T-SQL only solution ...even if its officially supported...from 2005..
What does the above code suppose to do? and why you want to use it.
"That's why ...even if  this feature is quite old in MsSQL ecosystem (for comparison) is not that widely accepted... and people are striving for T-SQL only solution ...even if its officially supported...from 2005.."

you are MS SQL guy I know now, same as me ! but not sure why you keep referring to MS SQL, I thought you are saying MySQL.

"What does the above code suppose to do? and why you want to use it."

we try to convert all object one by one from Oracle to MariaDB, so that is one of the objects need convert so we worry about it. but what you can say is, in production, just one !
Right now i am an all around SQL...i worked for quite some time with MsSQL (Microsoft SQL Server) but right now i am using MySQL/Firebird/Sqlite for my  SQL needs.
About your conversion...at first forget about java..what you need is to see what exactly this function returns and try to implement it in the SQL variant of MariaDB (from the looks it seems some kind of convert but without further knowledge )...yes its a cool feature to have java code executed via a SQL Engine (Oracle) but in the end you want to keep SQL to SQL and other programming languages to the other programming languages.
"right now i am using MySQL/Firebird/Sqlite for my  SQL needs."

your own need ? btw, MariaDB is one of the best in Open source DB. it has its own proxy and OLAP solution, which other open source DB do not have.

"(from the looks it seems some kind of convert but without further knowledge )."

yes you are right ! but I don't know and I just worry about conversion !
SOLUTION
Avatar of Tomas Helgi Johannsson
Tomas Helgi Johannsson
Flag of Iceland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Oracle is much much older than Java and is written on C (Around 25 million lines of code)..
The support of Java is a feature..
Hi,

John, you are right Oracle was first written in C/C++ (in fact the kernel is written in C )  but large chunks of today's Oracle database are written in Java and PL/SQL. :)
And all peripheral tools suchs as SQL Developer and Grid control etc are written in Java.

Regards,
    Tomas Helgi
The code for the Engine is C/C++ ...everything else is peripheral and they are not associated with its core functionality...
they could easily be written in any language and the presence of Java is purely because Oracle bought Java...i am pretty sure that in older versions prior to Java/Sun acquisition there were written in another language (probably C)
Tomas Helgi Johannsson,

"Only SQL/PL language is supported for stored procedures and UDF's. C/C++ are supported for external UDF's creation."

you mean oracle UDF is supported in MariaDB?


"However MariaDB supports Java  through it's client interface as many other databases.
"

You mean java management tools to connect to MariaDB?

"
"Main reason for the tight Java integration in Oracle is the fact that the Oracle Database is almost or entirely written in Java. "

so you also agree that we can write java in oracle SP and function, etc.. ?

but Java is slow and take up so much memory, how can it make oracle fast ?

"And all peripheral tools suchs as SQL Developer and Grid control etc are written in Java

yeah you are right and the tools include JDK,  that's why..

but it will make Oracle slow and take up so much memory ?

John Tsioumpris,

"i am pretty sure that in older versions prior to Java/Sun acquisition there were written in another language (probably C)"

tks.

so should I think in the way that old oracle is faster than new oracle because of this ?
No...the codebase is still the same C/C++ ...when you have a Racing horse you don't substitute with a mule...
What i meant is all the tools/utilities/etc that Tomas  mentioned are converted From (?) to Java in order to support the use of Java(marketing)...
ok, oracle code based still on C/C++ so it is fast !

but tools connect to Oracle are mostly converted to Java from other language, right?

so MariaDB do not support Java anyway?
Java Tools connecting to Oracle are based on the connector/s (they are more than one i think)..they just connect to Oracle DB Engine and perform some operations easier....like every other 3rd party tool that interacts with a SQL engine.
Executing Java straight from Oracle is just a feature...in the background it uses a JVM to pass the arguments and receives the output....its not something native in the core of the database engine...its just an extension in order to leverage its functionality
Executing Java from MySQL/MariaDB is based either on what i posted earlier on executing system commands on triggers (i read it somewhere but i didn't kept the links)
With every new version of every SQL engine companies just try to "push" more and more features that make the developers' lifes easier ...but in the end it all boils to plain simple SQL what ever the implementation is..
that's why (speaking from the MsSQL side) the books from MsSQL 2000 are still considered top and why Celko's SQL puzzles and solutions are considered the best SQL guidance/education you can get.
"its not something native in the core of the database engine...its just an extension in order to leverage its functionality"

ok.

"Executing Java from MySQL/MariaDB is based either on what i posted earlier on executing system commands on triggers (i read it somewhere but i didn't kept the links)"


you mean this  ?

https://planet.mysql.com/entry/?id=13420

"that's why (speaking from the MsSQL side) the books from MsSQL 2000 are still considered top and why Celko's SQL puzzles and solutions are considered the best SQL guidance/education you can get."

you mean coding in SQL in SQL Server still better than programming java in MS SQL ?
Coding in SQL/Java....is not a matter of better/speedier
When you code in Java essentially you are using the same SQL of the SQL engine you use to perform exactly the same operations as if you were interacting with the SQL engine via Console or some manager.
It might look different if you are using an ORM or a wrapper or a class library or whatever but in the end whatever you have programmed in Java is the fundamentals SQL commands : SELECT/UPDATE/INSERT/DELETE along with the other extra commands.
The connector just takes that code..because you have told your application that you want to connect to a SQL engine...and the specific parts that refer to the SQL engine ...which is plain text are just passed as an argument to the SQL engine...pretty much as if you were executing queries via console....
Just to clarify it a bit...you are using Java and you are connected to a MariaDB engine ...you must use the MySQL variant of SQL ...you can't for example use CTE that are owned by MsSQL variant of SQL.
Something more...
In the link i send you about planetmysql if you compare it to the Oracle documentation for executing Java you will see its pretty much the same.
The Engine "raises" a JVM...tries to find all the references..arguments are passed to the class and it gets the output.
The difference is that Oracle's is official and supported/test while the other is more a "test".
"The connector just takes that code..because you have told your application that you want to connect to a SQL engine...and the specific parts that refer to the SQL engine ...which is plain text are just passed as an argument to the SQL engine...pretty much as if you were executing queries via console...."

actually I don't agree with it , you can see my code posted it has java code inside a function.

"The connector just takes that code..because you have told your application that you want to connect to a SQL engine...and the specific parts that refer to the SQL engine ...which is plain text are just passed as an argument to the SQL engine...pretty much as if you were executing queries via console...."

don't understand.
Your case is different...
In your case you have a Java compiled class that does something - it doesn't matter --lets assume it just takes a string and makes it upper case ...the Oracle Engine based on the extensibility concept..."reads" that this some piece of external code...it makes the necessary actions..like "activating" a JVM that looks if the code is properly referenced...send the arguments --> the string ...the class takes the string..change its case to upper case ...returns the output to the Engine..the Engine just "includes" the output of the Java code to the results.

In the normal programming concept ..a programming language interacting with a SQL engine  you have code like this (taken for here)
package com.mkyong.jdbc;

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

public class JDBCStatementSelectExample {

	private static final String DB_DRIVER = "oracle.jdbc.driver.OracleDriver";
	private static final String DB_CONNECTION = "jdbc:oracle:thin:@localhost:1521:MKYONG";
	private static final String DB_USER = "user";
	private static final String DB_PASSWORD = "password";

	public static void main(String[] argv) {

		try {

			selectRecordsFromDbUserTable();

		} catch (SQLException e) {

			System.out.println(e.getMessage());

		}

	}

	private static void selectRecordsFromDbUserTable() throws SQLException {

		Connection dbConnection = null;
		Statement statement = null;

		String selectTableSQL = "SELECT USER_ID, USERNAME from DBUSER";

		try {
			dbConnection = getDBConnection();
			statement = dbConnection.createStatement();

			System.out.println(selectTableSQL);

			// execute select SQL stetement
			ResultSet rs = statement.executeQuery(selectTableSQL);

			while (rs.next()) {

				String userid = rs.getString("USER_ID");
				String username = rs.getString("USERNAME");

				System.out.println("userid : " + userid);
				System.out.println("username : " + username);

			}

		} catch (SQLException e) {

			System.out.println(e.getMessage());

		} finally {

			if (statement != null) {
				statement.close();
			}

			if (dbConnection != null) {
				dbConnection.close();
			}

		}

	}

	private static Connection getDBConnection() {

		Connection dbConnection = null;

		try {

			Class.forName(DB_DRIVER);

		} catch (ClassNotFoundException e) {

			System.out.println(e.getMessage());

		}

		try {

			dbConnection = DriverManager.getConnection(DB_CONNECTION, DB_USER,
					DB_PASSWORD);
			return dbConnection;

		} catch (SQLException e) {

			System.out.println(e.getMessage());

		}

		return dbConnection;

	}

}

Open in new window

which if take a look is just SQL commands
String selectTableSQL = "SELECT USER_ID, USERNAME from DBUSER";

Open in new window

that are prepared in Java and finally send to the SQL engine
ResultSet rs = statement.executeQuery(selectTableSQL);

Open in new window

in order to either get results
or in other cases  do some kind of data manipulation
but what you are saying this, other than the REAL SQL code, ALL OTHER code is not execute INSIDE MariaDB, right ?

very diff from my case.
yes ...exactly.
anyway, my point is just that, MariaDB support building function/SP using java? if not I am not going to research further.
From Java you can instruct MariaDB to create SQL functions/SP or whatever you need(SQL)  but adapting Java methods as its own...only with the way posted earlier
"From Java you can instruct MariaDB to create SQL functions/SP or whatever you need(SQL)"

you mean create SQL function and SP via java application, right ?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
tks all .