mysql jdbc linux,MySQL JDBC for Linux

MySQL JDBC for Linux

This tutorial shows how to set up a data source

and connect to your MySQL® database. This tutorial uses the MySQL Connector/J

5.1.17 driver to connect to the MySQL Version 5.5.16 database.

Step 1. Verify the driver installation.

If the JDBC driver for MySQL is not installed on your computer,

find the link on the Driver

Installation page to install the driver. Follow the instructions

to download and install this driver on your computer.

Step 2. Add the JDBC driver to the MATLAB static Java class path.Run the prefdir command in the

Command Window. The output is a file path to a folder on your computer.

Close MATLAB® if it is running.

Navigate to the folder and create a file called javaclasspath.txt in

the folder.

Open javaclasspath.txt. Add the

full path to the database driver JAR file in javaclasspath.txt.

The full path includes the path to the folder where you downloaded

the JAR file from the database provider and the JAR file name. For

example, /home/user/DB_Drivers/mysql-connector-java-5.1.17-bin.jar.

Save and close javaclasspath.txt.

Restart MATLAB.

Alternatively, you can use javaaddpath to

add your JDBC driver to the dynamic Java® class path. For details

about static and dynamic class paths, see Java Class Path (MATLAB).

Step 3. Set up the data source using Database Explorer.

This step is required only for connecting to Database Explorer.

If you want to use the command line to connect to your database, see Connect to MySQL Using JDBC Driver and Command LineOpen Database Explorer by clicking the Apps tab

on the MATLAB Toolstrip. Then, select Database Explorer from

the Database Connectivity and Reporting section

in the apps gallery. Alternatively, enter dexplore at

the command line. If no data sources are set up, a message box opens.

Click OK to close it. Otherwise, the Connect

to a Data Source dialog box opens. Click Cancel to

close this dialog box.

Click the Database Explorer tab,

and then select New > JDBC.

6be6c788c350168c23e2e47f02ffd633.png

The Create a New JDBC data source dialog box opens.

d016033a07297cb845fd67796fdb04f0.png

Select MYSQL from the Vendor list.

After selecting the vendor, if you did not add the JDBC driver file

path to the Java class path, this dialog box displays this message

at the bottom. Address this message by following the steps described

in Step 2.

4cf34b1d2320d76ee0db92bb0f2f9c36.png

Enter the database server name in the Server

Name field, port number in the Port Number field,

user name in the Username field, password in

the Password field, and database name in the Database field.

Click Test to test the connection.

If your connection succeeded, Database Explorer displays Connection

Successful!

Enter a data source name in the Data Source

Name field in the Create a New JDBC data source dialog

box. Use a new data source name that does not appear in the existing

list of data source names. Click Save. The

new JDBC data source appears in the list of data sources in the Connect

to a Data Source dialog box.

If this time is the first time that you are creating

a data source using Database Explorer, the New file to store JDBC

connection parameters dialog box opens. Use this dialog box to create

a MAT-file that saves your specified data source information for future

Database Explorer sessions. This MAT-file name is stored in setdbprefs('JDBCDataSourceFile') and

is valid for all MATLAB sessions.

Navigate to the folder where you want to put the MAT-file, specify

a name for it that includes a .mat extension, and

click Save.

After you complete the data source setup, connect to the MySQL database

using Database Explorer or the command line with the JDBC connection.

Step 4. Connect using Database Explorer or the command line.

Connect to MySQL using Database Explorer.After setting up the data source, connect to your

database by selecting the data source name for the MySQL database

from the Data Sources list. Enter a user name

and password. Click Connect.

87f67c6b79fbb955421e1fb00ca8b145.png

Database Explorer connects to your database and displays its

contents in a tab named with the data source name.

f39fc8b981c4813ba689410d3c5aad75.png

Close the connection using Database Explorer by hovering

the cursor over the Close button (3c532fd4ed5850e3f4e44b1dd726002a.png) next to the MySQL data

source name on the database tab. The Close button

turns into a red circle (57916d2c2b948befd2b0b86c4b1bd7a1.png). Click it to close the

database connection. If you want to close Database Explorer and all

database connections, click the Close button

(c9dd914067f2318d20ccf23290894a71.png) in the top-right corner.

If Database Explorer is docked, click the Close button

(417de4e3f8a613417c45423d421d8f8b.png) to close all database connections

and Database Explorer.

0fb00551c4c0535a238fa2099d4902fa.png

Connect to MySQL Using JDBC Driver and Command Line

When using the command line, you do not have to set up a data

source with Database Explorer. You can use the command line to pass

all the required parameters for connection.Use the Vendor name-value pair

argument of database to specify a connection to

a MySQL database. For example, the following code assumes you

are connecting to a database named dbname on a

database server named sname with user name username and

password pwd.

conn = database('dbname','username','pwd', ...

'Vendor','MySQL', ...

'Server','sname');

Close the database connection.

close(conn)

See Also

Related Topics

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值