java - 设置 SQL Server 以与 Java 连接

标签 java sql sql-server

我正在编写一个连接到 SQL Server 2012 数据库的 Java 程序。我已经让它工作了,但需要一些实验。我必须做的是:

  • 启用 TCP/IP
  • 启动“SQL Server 浏览器”服务

有没有办法通过使用一些 SQL 命令来执行这 2 个操作,而不必手动执行?原因是我不希望用户必须这样做,因为这会增加额外的复杂性。

谢谢!

最佳答案

当然是(用于启用 TCP)

--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 

来自:http://arashmd.blogspot.com/2013/07/sql-server-t-sql-tips-tricks.html#xp_regeditwrite

关于java - 设置 SQL Server 以与 Java 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19209454/

相关文章:

java - 当构建文件位置更改时如何在 ANT 中设置 lib 路径

java - 对字符串 ArrayList 使用选择排序

sql - 如何在 hive 中通过 collect_set() 操作使用 order by

sql - Sql Server 2016 中 INSERT SELECT UNION ALL 的原子性

java - 我怎样才能确保 Spring 一次构造一个 bean

java - 使用 Criteria API 和 JPQL 找到的实体是否自动位于 JPA 的持久性上下文中?

java - executeUpdate 返回行数而不是受影响的行数

sql - 数据库中的非拉丁字符排序为 "order by"

sql-server - SQL Server 中的临时表和表变量有什么区别?

c# - 在数据库中存储调查的多项选择答案