Java/SQL Server 2008 R2 Express连接问题
我正在尝试使用以下代码连接到本地计算机上的SQL Server db:Java/SQL Server 2008 R2 Express连接问题,java,sql-server,sql-server-2008,jdbc,Java,Sql Server,Sql Server 2008,Jdbc,我正在尝试使用以下代码连接到本地计算机上的SQL Server db: import java.sql.*; import com.microsoft.sqlserver.jdbc.*; import java.sql.*; public class JDBConn { //public class connectURL { public static void main(String[] args) { // Declare the JD
import java.sql.*;
import com.microsoft.sqlserver.jdbc.*;
import java.sql.*;
public class JDBConn {
//public class connectURL {
public static void main(String[] args) {
// Declare the JDBC objects.
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
// Create a variable for the connection string.
//String connectionUrl = "jdbc:sqlserver://localhost;database=optRes1;integratedSecurity=true;";
//con = DriverManager.getConnection(connectionUrl);
try {
// Establish the connection.
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionUrl = "jdbc:sqlserver://localhost";
con = DriverManager.getConnection(connectionUrl);
Statement s = con.createStatement();
ResultSet r = s.executeQuery("SELECT * FROM some_table_name");
while (r.next()) {
System.out.println(r.getString(1));
}
// Create and execute an SQL statement that returns some data.
String SQL = "SELECT * from [optRes1].[dbo].[unEmpDat]";
stmt = con.createStatement();
rs = stmt.executeQuery(SQL);
// Iterate through the data in the result set and display it.
while (rs.next()) {
System.out.println(rs.getString(4) + " " + rs.getString(6));
}
}
// Handle any errors that may have occurred.
catch (Exception e) {
e.printStackTrace();
}
finally {
if (rs != null) try { rs.close(); } catch(Exception e) {}
if (stmt != null) try { stmt.close(); } catch(Exception e) {}
if (con != null) try { con.close(); } catch(Exception e) {}
}
}
}
当我尝试连接时(我使用Eclipse作为IDE),我收到以下消息:
到目前为止,我已经进入配置管理器并激活和启用了TCP/IP
我还进入并在Properties>IP Addresses选项卡下将每个IP地址设置为on和active
一些潜在的问题
当我查看SQL Server浏览器的状态时,它是“已停止”。我不确定这是否是一个问题,如果是,如何解决它
我正在使用sqljdbc4.jar
文件,并将其放在项目的参考库部分
如果你有任何建议,请告诉我
编辑:
我用的是Windows7的盒子
更新:
>java -version
java version "1.6.0_14"
Java(TM) SE Runtime Environment (build 1.6.0_14-b08)
Java HotSpot(TM) 64-Bit Server VM (build 14.0-b16, mixed mode)
您没有包括正在使用的JRE版本,但1.6.0_29和SQL Server 2008之间存在一个已知问题,在Microsoft论坛中也有一个问题,建议降级到1.6.0_27
您也可以将29的
jsse.jar
替换为27中的一个。这对于很难降级的Mac OS客户端非常有用。目标文件是machd->System->Library->Java->JavaVirtualMachines->1.6.0.jdk->Contents->Classes->jsse.jar
不需要运行SQL Server浏览器。但对于JDBC连接,如果关闭SQL Server动态端口并通过SQL Server配置应用程序强制所有网络接口使用端口1433,则更容易。我需要重新启动服务器并打开错误日志记录。重新启动服务器的窗口隐藏在其他一些窗口后面,没有看到它。
>java -version
java version "1.6.0_14"
Java(TM) SE Runtime Environment (build 1.6.0_14-b08)
Java HotSpot(TM) 64-Bit Server VM (build 14.0-b16, mixed mode)