2

I am using Database toolbox of Matlab to run my MySQL queries. I am doing this by using JDBC driver (Connector/J).

I am able to connect/create/delete new tables for a given database.

Is there a way by which I can directly create a new database from Matlab itself? I'm looking for a solution which lets me do this by using the toolbox or by using Java from Matlab.

Zero
  • 74,117
  • 18
  • 147
  • 154
  • Yes, you can create new database from Java application itself. For example, if you use Derby database. But, I think you can't programaticaly create database when it comes to MySQL. – Branislav Lazic Aug 07 '13 at 11:26
  • If you can run plain SQL commands using your toolbox, you could try `create database dbname`. If your toolbox supports dynamic JDBC-URLs to connect to databases not known at compile-time, you could try that way. – Beryllium Aug 07 '13 at 11:30
  • I think, we can create database using PHP and Python programaticaly – Zero Aug 07 '13 at 11:30
  • Actualy, my bad. Seems you can create MySQL database programaticaly. Have a look at [this question](http://stackoverflow.com/questions/717436/create-mysql-database-from-java) – Branislav Lazic Aug 07 '13 at 11:31

1 Answers1

3

Here's what I have used. In Matlab this works.

import java.sql.*;

ConnD = DriverManager.getConnection(...
'jdbc:mysql://localhost/?user=urname&password=urpassword');

sD=ConnD.createStatement();
Result=sD.executeUpdate('CREATE DATABASE urdatabasename');
sD.close();
ConnD.close();

Mind you, this doesn't include error handling and checks. Make sure you handle your data carefully.

Zero
  • 74,117
  • 18
  • 147
  • 154