Main Content

微软SQL ServerJDBCLinux

This tutorial shows how to set up a data source and connect to a微软®SQL Server®使用数据库Explorer应用程序或命令行的数据库。本教程使用Microsoft JDBC驱动程序4.0微软SQL Serverto connect to a微软SQL Server2016 Express database.

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

如果JDBC驱动程序微软SQL Server未安装在计算机上,在驱动程序安装页面以安装驱动程序。请按照说明下载并在计算机上安装此驱动程序。

步骤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数据源配置数据源选择

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

  3. 在里面姓名box, enter a name for your data source. You use this name to establish a connection to your database.

  4. 来自Vendor列表,选择微软SQL Server

    JDBC Data Source Configuration dialog box with the selected Microsoft SQL Server vendor

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

    If your connection succeeds, the Database Explorer dialog 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. Create a JDBC data source for an SQL Server database.

    vendor ="Microsoft SQL Server";opts = databaseconnectionoptions("jdbc",,,,vendor);
  2. Set the JDBC connection options. For example, this code assumes that you are connecting to a JDBC data source namedMSSQLServer,JDBC驱动程序文件的完整路径/home/user/db_drivers/sqljdbc4.jar,,,,database nametoystore_doc,数据库服务器DBTB04和端口号54317

    opts = setOptions(opts,...'DataSourceName',,,,"MSSQLServer",,,,...'JDBCDriverLocation',,,,"/home/user/DB_Drivers/sqljdbc4.jar",,,,...'DatabaseName',,,,“ toystore_doc”,,,,'Server',,,,“ DBTB04”,,,,...'portnumber',54317);
  3. 通过指定用户名来测试数据库连接用户名和密码pwd,,,,or leave these arguments blank if your database does not require them.

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

    saveasdatasource(opts)

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

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

连接至SQL ServerUsing Database Explorer App

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

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

    The Catalog and Schema dialog box opens.

  3. Select the catalog and schema from theCatalogand模式列表。点击OK

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

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

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

    笔记

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

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

  1. 连接至an SQL Server database using the configured JDBC data source, user name用户名和密码pwd。例如,此代码假设您正在连接到name namy natureMSSQLServer

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

    关闭(conn)

也可以看看

应用

功能

Related Topics