gpt4 book ai didi

java - 在(Mac 或 Linux)上将 MS-SQL 与 Maven2、Netbeans 结合使用

转载 作者:行者123 更新时间:2023-12-02 08:06:00 25 4
gpt4 key购买 nike

寻求建议或帮助,为我指明正确的方向。

我需要从 Java 程序连接到 Microsoft SQL Server,但是驱动程序必须在 maven2 中可用并且可以与 NetBeans 配合使用。

有什么建议吗? (指向一个例子就太好了)(自杀不再是一种选择)

编辑:我找到了 JTDS - 这是一个好的解决方案吗?

编辑 2:看起来可行...这是我的配置方式...

Pom文件

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.steward.ccd</groupId>
<artifactId>amalgainterface</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>amalgainterface</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>net.sourceforge.jtds</groupId>
<artifactId>jtds</artifactId>
<version>1.2.4</version>
</dependency>

<dependency>
<groupId>org.ini4j</groupId>
<artifactId>ini4j</artifactId>
<version>0.5.2</version>
</dependency>

<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>

</dependencies>
</project>

Java 文件

import java.sql.*;
import org.ini4j.*;
import java.util.*;
import java.io.File;

public class AmalgaInterface {

public static void main(String[] args) {

// MS-SQL Parameters
String db_name = "xxxx";
String db_hostname = "xxxx";
String db_port = "1433";
String db_userid = "xxxx";
String db_password = "xxxx";
String db_timeout = "10";

// Check the Configuration file, and replace all service reference as required.
// Get configuration
String configFile = "/etc/test.conf";

// Load data from INI files
Ini ini = null;
try {
ini = new Ini(new File(configFile));
db_name = ini.get("database", "name");
db_hostname = ini.get("database", "host");
db_userid = ini.get("database", "user");
db_password = ini.get("database", "pass");
db_port = ini.get("database", "port");
db_timeout = ini.get("database", "dbtimeout");
} catch (Exception ex) {
System.out.println("Cannot load the configuration file");
}

// Create the connection string
String db_connect_string = "jdbc:jtds:sqlserver://" + db_hostname + ":" + db_port + "/" + db_name + ";socketTimeout=" + db_timeout;

// setup connection
Connection connection = null;

try {
Class.forName("net.sourceforge.jtds.jdbc.Driver");
connection = DriverManager.getConnection(db_connect_string, db_userid, db_password);
} catch (Exception ex) {
System.out.println(ex.getMessage());
}


// clean up
if (connection != null) {
try {
connection.close();
} catch (Exception ex) {
}
}

}
}

最佳答案

尝试JTDS

这是一个类型 4 jdbc 驱动程序,我相信它可以通过 maven repository 获得。 .

关于java - 在(Mac 或 Linux)上将 MS-SQL 与 Maven2、Netbeans 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8159869/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com