我对java完全陌生,我想连接到远程SQL server 2008 R2数据库(如192.168.17.11)并从中加载数据。
首先,您必须有MS SQL的JDBC驱动程序,您有两个jar (jtds-1.2.5.jar或sqljdbc4-2.0.jar),它们添加到您的类路径
String password="pass";
String driver= "net.sourceforge.jtds.jdbc.Driver"; // For sqljdbc4, use: com.microsoft.sqlserver.jdbc.SQLServerDriver
String username="user";
String URL="jdbc:jtds:sqlserver://serverIP:port/dbname"; // For sqljdbc4, use: jdbc:sqlserver://serverIP:port;databaseName=dbname
Class.forName(driver);
Connection conn = DriverManager.getConnection(URL, username, password);
// Use your connection here
// Don't forget to close the connection