寻求建议或一点帮助,为我指出正确的方向。
我需要从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
它是一个type 4 jdbc驱动程序,我相信它可以通过maven存储库获得。