البرمجة

Connect to SQL Server with JDBC from Linux

To connect to a SQL Server using JDBC and Windows authentication from a Linux machine, you can use the open-source jTDS driver. This driver supports Windows authentication on non-Windows platforms like Linux. Here’s how you can do it:

  1. Download jTDS Driver: First, download the jTDS driver JAR file from the official website or a repository. You can find it here: jTDS Downloads

  2. Add jTDS to Your Project: Add the jTDS JAR file to your Java project’s build path.

  3. Update Your Connection String: Use the following connection string format:

    java
    String connectionUrl = "jdbc:jtds:sqlserver://your_server_name:1433/your_database_name;domain=your_domain_name";

    Replace your_server_name, your_database_name, and your_domain_name with your SQL Server details.

  4. Configure jTDS for Windows Authentication:

    • You don’t need to add integratedSecurity=true; to the connection string for jTDS.
    • Ensure that sqljdbc_auth.dll is not required for jTDS.
  5. Example Code:

    java
    import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class Main { public static void main(String[] args) { try { Class.forName("net.sourceforge.jtds.jdbc.Driver"); String connectionUrl = "jdbc:jtds:sqlserver://your_server_name:1433/your_database_name;domain=your_domain_name"; Connection conn = DriverManager.getConnection(connectionUrl, "username", "password"); System.out.println("Connected successfully."); // Use the connection... conn.close(); } catch (SQLException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } }
  6. Run Your Application: Compile and run your Java application on your Linux machine. Ensure that the jTDS driver JAR is included in your classpath.

This should allow you to connect to a SQL Server using JDBC and Windows authentication from a Linux machine.

المزيد من المعلومات

To connect to a SQL Server using JDBC and Windows authentication from a Linux machine, you can follow these steps:

  1. Download the jTDS Driver: As mentioned earlier, download the jTDS driver JAR file from the jTDS Downloads page.

  2. Add jTDS to Your Project: Place the downloaded jTDS JAR file in your Java project’s classpath. You can do this by either adding it to your project’s build path in your IDE or by specifying it in your command line compilation and execution.

  3. Update Your Connection String: Modify your connection string to include the appropriate parameters for Windows authentication. The format should be:

    java
    String connectionUrl = "jdbc:jtds:sqlserver://your_server_name:1433/your_database_name;domain=your_domain_name";

    Replace your_server_name, your_database_name, and your_domain_name with your SQL Server details.

  4. Ensure Proper Configuration:

    • Make sure that your SQL Server is configured to allow Windows authentication.
    • Ensure that the Linux machine has network connectivity to the SQL Server.
    • Check that the SQL Server’s firewall allows connections from the Linux machine.
  5. Example Code:
    Here’s an example Java code snippet to connect to SQL Server using jTDS:

    java
    import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class SQLServerConnect { public static void main(String[] args) { String connectionUrl = "jdbc:jtds:sqlserver://your_server_name:1433/your_database_name;domain=your_domain_name"; try (Connection con = DriverManager.getConnection(connectionUrl, "username", "password")) { System.out.println("Connected to SQL Server successfully."); } catch (SQLException e) { System.out.println("Error connecting to SQL Server: " + e.getMessage()); } } }
  6. Run Your Application: Compile and run your Java application on the Linux machine. Ensure that the jTDS driver is included in your classpath.

By following these steps, you should be able to connect to a SQL Server using JDBC and Windows authentication from a Linux machine.

زر الذهاب إلى الأعلى