Main Content

mysqlJDBCWindows

本教程显示了如何设置数据源并连接到MySQL®使用数据库Explorer应用程序或命令行的数据库。本教程使用MySQL连接器/J 5.1.46驱动程序连接到MySQL版本5.5.16数据库。

步骤1.验证驱动程序安装。

If the JDBC driver for MySQL is not installed on your computer, find the link on the驱动程序安装页面以安装驱动程序。请按照说明下载并在计算机上安装此驱动程序。

步骤2.设置数据源。

You set up a data source using the Database Explorer app or the command line.

使用数据库Explorer应用程序设置数据源

  1. Open the Database Explorer app by clicking the应用tab on the MATLAB®工具条。然后,在右边应用部分,单击显示更多箭头打开应用程序画廊。在下面数据库连接ivity and Reporting,,,,click数据库资源管理器。或者,输入Databaseexplorer在命令行。

  2. 在里面Data Source部分,选择配置数据源>配置JDBC数据源

    JDBC数据源配置对话框打开。

  3. 在里面姓名框,输入数据源的名称。(此示例使用名为的数据源mysql。)您使用此名称来建立与数据库的连接。

  4. 来自Vendor列表,选择mysql

  5. 在里面Driver Location框,输入JDBC驱动程序文件的完整路径。

  6. 在里面数据库box, enter the name of your database. In the服务器框,输入数据库服务器的名称。请咨询您的数据库管理员以获取数据库服务器的名称。在里面Port Number框,输入端口号。

  7. 在下面连接ion Options,,,,in the姓名列,输入其他特定于驱动程序的选项的名称。然后,在Valuecolumn, enter the value of the driver-specific option. Click the plus sign+指定其他特定于驱动程序的选项。

  8. 点击测试。打开“测试连接”对话框。输入数据库的用户名和密码,或者如果您的数据库不需要它们,则将这些盒子空白。点击测试

    如果连接成功,Database Explorerdialog box displays a message indicating the connection is successful. Otherwise, it displays an error message.

  9. 点击Save。The JDBC Data Source Configuration dialog box displays a message indicating the data source is saved successfully. Close this dialog box.

Set Up Data Source Using Command Line

  1. 为MySQL数据库创建JDBC数据源。

    vendor =“ mysql”;opts = databaseconnectionoptions("jdbc",,,,vendor);
  2. Set the JDBC connection options. For example, this code assumes that you are connecting to a JDBC data source namedmysql,JDBC驱动程序文件的完整路径C:\Drivers\mysql-connector-java-5.1.34-bin.jar,,,,database nametoystore_doc,数据库服务器DBTB01和端口号3306

    opts = setOptions(opts,...'DataSourceName',,,,“ mysql”,,,,...'JDBCDriverLocation',,,,“ c:\驱动程序\ mysql-connector-java-5.1.34-bin.jar”,,,,...'DatabaseName',,,,“ toystore_doc”,,,,'Server',,,,"dbtb01",,,,...'portnumber',3306);
  3. 通过指定用户名来测试数据库连接用户名和密码pwd,,,,or leave these arguments blank if your database does not require them.

    用户名=“用户名”;password ="pwd";状态= testConnection(OPTS,用户名,密码);
  4. 保存JDBC数据源。

    saveasdatasource(opts)

完成数据源设置后,使用数据库Explorer应用程序或JDBC驱动程序和命令行连接到MySQL数据库。

步骤3.使用数据库Explorer应用程序或命令行连接。

连接至mysqlUsing Database Explorer App

  1. 数据库资源管理器tab, in the连接部分,单击连接and select the data source for the connection.

  2. 在“连接”对话框中,输入用户名和密码,或者如果您的数据库不需要它们,则将这些盒子空白。点击连接

    The Catalog and Schema dialog box opens.

  3. 在里面Catalog列表,选择the catalog. ClickOK

    The app connects to the database and displays its tables in theData Browser窗格。数据源选项卡在窗格的右侧出现。数据源选项卡的标题是您在设置期间定义的数据源名称。数据源选项卡包含空SQL Queryand数据预览窗格。

  4. Data Browser窗格查询数据库。

  5. 关闭数据源选项卡以关闭SQL查询。在里面连接部分,通过单击关闭数据库连接关闭连接

    笔记

    如果多个连接是打开的,请通过从“关闭连接list.

连接至mysql使用JDBC驱动程序和命令行

  1. 连接至a MySQL database using the configured JDBC data source, user name用户名和密码pwd

    dataSource =“ mysql”;用户名=“用户名”;password ="pwd";conn =数据库(数据源,用户名,密码);
  2. 关闭数据库连接。

    关闭(conn)

也可以看看

应用

功能

Related Topics