我正在尝试连接到SQL Server。
从代码连接到服务器时,它给我一个错误:
javax.servlet.ServletException: java.sql.SQLException: Network error IOException: Connection refused: connect
。
下面给出代码:
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.SQLException"%>
<%
Class.forName("net.sourceforge.jtds.jdbc.Driver");
Connection cn = DriverManager
.getConnection("jdbc:jtds:sqlserver://MSI;DatabaseName=LeadManagement;user=sa;password=123456");
Statement stmt = cn.createStatement();
ResultSet rs = stmt.executeQuery("select * from userDetail");
%>
屏幕快照-SQL Server连接实例
为什么会出现此错误?
尝试使用此更新的代码段:
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.SQLException"%>
<%
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url = "jdbc:sqlserver://MSI;databaseName=LeadManagement;user=sa;password=123456;";
Connection connection = DriverManager.getConnection(url);
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery("select * from userDetail");
%>
注意:从here下载Microsoft JDBC驱动程序jar。
要注意的另一点-请验证网址。它必须遵循以下语法-jdbc:sqlserver://<server>:<port>