pontazaricardo / Matlab_Database_connection

This is an example of how to create a connection from Matlab to a given database by JDBC.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Matlab_Database_connection

This is an example of how to create a connection from Matlab to a given database by JDBC (in our case, MySQL).

demo01

Usage

For this example:

  1. Download the attached jar (/jars/mysql-connector-java-5.1.43-bin.jar) and copy it to a fixed location (in our example, C:\jars)
  2. In the code, modify the following lines
dbname = 'name_of_your_database';
username = 'username_of_your_database';
password = 'password_of_your_database';
driver = 'com.mysql.jdbc.Driver';
dburl = ['jdbc:mysql://127.0.0.1:3306/' dbname];  %If needed, you can change the 127.0.0.1 to a different address and port.
javaclasspath('C:\jars\mysql-connector-java-5.1.43-bin.jar'); %Place the path of the jar.

The lines above are self explanatory, but in case a more detailed description is needed, we have the following:

  1. dbname: This is the database name you are trying to establish the connection to. In MySQL, these are known as Schemas.
  2. username and password: These are the username and password used to connect to the schema.
  3. driver: To connect to MySQL, keep the value of
com.mysql.jdbc.Driver

If you want to use a different database, please consult here. As an example, for Microsoft SQL, you can use

com.microsoft.sqlserver.jdbc.SQLServerDriver
  1. dburl: To connect to MySQL, keep the value of
['jdbc:mysql://127.0.0.1:3306/' dbname];

If you want to use a different database, please consult here. As an example, for Microsoft SQL, you can use

jdbc:sqlserver://localhost:port;database=databasename

How to execute queries

A connection link has to be defined.

conn = database(dbname, username, password, driver, dburl);

Server date

If you want to obtain the server's time, run the following query

query1 = 'select now()'
curs = exec(conn,query1);
curs = fetch(curs);
DatabaseTimestamp = curs.Data

Stored procedures

There are two ways to run a stored procedure from Matlab. If you want to check the status of the stored procedure, just run

query2 = 'call your_stored_procedure_name(input1, input2, input3, input4)'
curs = exec(conn,query2);
curs = fetch(curs);
result = curs.Data

where result holds the stauts of executing the procedure. If the status of a procedure is not important, just run

query3 = 'call your_stored_procedure_name(input1, input2, input3, input4)';
curs = exec(conn,query3);

Connection closure

At the end of your file, remember to close the connection as

close(conn);

demo02

Code

A full example of the code can be found below.

clear; clc;

dbname = 'name_of_your_database';
username = 'username_of_your_database';
password = 'password_of_your_database';
driver = 'com.mysql.jdbc.Driver';
dburl = ['jdbc:mysql://127.0.0.1:3306/' dbname];  %If needed, you can change the 127.0.0.1 to a different address and port.
javaclasspath('C:\jars\mysql-connector-java-5.1.43-bin.jar'); %Place the path of the jar.

conn = database(dbname, username, password, driver, dburl);

query1 = 'select now()'
curs = exec(conn,query1);
curs = fetch(curs);
DatabaseTimestamp = curs.Data

query2 = 'call your_stored_procedure_name(input1, input2, input3, input4)'
curs = exec(conn,query2);
curs = fetch(curs);
result = curs.Data

%This is an example of how to call a stored procedure without output.
query3 = 'call your_stored_procedure_name(input1, input2, input3, input4)';
curs = exec(conn,query3);

%Remember to close your connection at the end.
close(conn);

About

This is an example of how to create a connection from Matlab to a given database by JDBC.

License:Mozilla Public License 2.0


Languages

Language:MATLAB 100.0%