正如你所说,你的协议(TCP)被禁用,所以让我们用一些代码来激活它:)来自codeproject的源代码
--step 1: creating a login (mandatory)
create login login_to_system_after_injection with password='Thank$SQL4Registry@ccess';
GO
--step 2: enabling both windows/SQL Authentication mode
/*some server specific configurations are not stored in system (SQL)*/
--set the value to 1 for disabling the SQL Authentication Mode after . . .
exec xp_regwrite N'HKEY_LOCAL_MACHINE', N'Software\Microsoft\MSSQLServer\MSSQLServer', N'LoginMode', REG_DWORD, 2;
--step 3:getting the server instance name
declare @spath nvarchar(256);
--SQL SERVER V100 path, use SQL9 for V90
exec master..xp_regread N'HKEY_LOCAL_MACHINE',
N'Software\Microsoft\Microsoft SQL Server\Instance Names\SQL' ,N'SQL10',@spath output,no_output
--step 4:preparing registry path
declare @insRegPath nvarchar(1024)=N'Software\Microsoft\Microsoft SQL Server\' +
@spath + '\MSSQLServer\SuperSocketNetLib\Tcp';
--step 5:enabling tcp protocol
exec xp_regwrite N'HKEY_LOCAL_MACHINE', @insRegPath, N'Enabled', REG_DWORD, 1 --generally tries to enable all addresses. NOT Recommended
--step 6:enabling remote access
--EXEC sys.sp_configure N'remote access', 1
GO
RECONFIGURE WITH OVERRIDE --reconfigure is required!
GO
--step 7:a system restart is required in order to enabling remote access.
--step 7.1:shutting down the server
shutdown
--After this command you need to start the server implicitly yourself.
--or just configure the Agent in order to start the server at any shutdown or failure
运行上述代码后,您需要重新启动服务器,还需要 sysadmin 规则 :)
如果上面的代码不起作用,请转到开始 -> 所有程序 -> Microsoft SQL Server 2008 -> 配置工具 -> SQL Server 配置管理器
然后从左侧窗格中选择“SQL Server 网络配置”,然后选择所需的实例,然后从右侧窗格中启用 TCP/IP,然后重新启动服务器
然后在Java应用程序中,您需要在下载库后更改类名和连接字符串,将其添加到类路径中,现在您的代码将是这样的
public class JdbcFirstTry
{
public static void main(String args[]) throws SQLException
{
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=DatabaseName;integratedSecurity=true;");
System.out.print("you made connection");
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
在上面的integratedSecurity=true 表示使用windows 帐户进行连接,但是很简单,您可以将用户名和密码与连接字符串添加为user=MyUserName;password=*****
最后,试试把结果告诉我,希望你能运行:)