Class SqlServer
A SQL Server client speaking TDS 7.4, the Tabular Data Stream, written from Microsoft's published MS-TDS specification. SQL Server 2017 and later, and Azure SQL.
SqlServer db = SqlServer.connect("db.internal", 1433, "app", "user", "secret",
"require", null, 10000, 0);
List rows = db.query("SELECT id, name FROM users WHERE id = @p1", new Object[] {7L});
Statements
A statement with parameters is an RPC call to sp_executesql, with the
parameters named @p1, @p2 and so on and typed from the Java values. One
without is a plain batch. Values never travel inside the statement text.
Encryption
The TLS handshake is carried inside PRELOGIN packets, which is how every
SQL Server before TDS 8.0 negotiates it. The client asks for the whole
session to be encrypted, never the login alone, for every sslmode but
disable. A login sends the password scrambled, not hashed, so a server
without TLS is refused unless sslmode=disable says the plaintext is
deliberate.
sslmode=strict is TDS 8.0: TLS first, before any TDS byte, with PRELOGIN
and the login inside it, the certificate verified for the host and ALPN
tds/8.0 offered. It is what a server or Azure SQL set to strict encryption
requires. SQL Server on Linux serves it from 2025; 2022 on Linux refuses
every strict connection (error 17821) whatever its certificate.
Sign-in
A SQL login by name and password, or a Windows account over NTLMv2 when the
user is written DOMAIN\\user. Kerberos and Microsoft Entra ID are not
implemented.
Values
Integers and bits arrive as Long, floats as Double, binary as
byte[], and everything else as a String in the form the other engines
use: decimals and money as digits, dates and times as
yyyy-MM-dd HH:mm:ss.fff.
-
Nested Class Summary
Nested classes/interfaces inherited from class ServerSession
ServerSession.Connector -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final ServerSession.ConnectorOpens SqlServer sessions; naming this constant is what puts the engine in an application. -
Method Summary
Modifier and TypeMethodDescriptionvoidbegin()Opens a transaction.voidbegin(boolean readOnly, TransactionIsolation isolation) Opens a transaction at an isolation level.voidOpens a transaction.voidclose()Closes the connection.voidcommit()Commits the open transaction.static SqlServerconnect(String host, int port, String database, String user, String password, String sslMode, String caFile, int timeoutMillis, int socketTimeoutMillis) Opens a session.dialect()The SQL this session's server speaks.intRuns a statement that returns no rows and answers with the number of rows it affected.booleanisClosed()True once the session is closed or its connection was lost.longThe key the last INSERT on this session generated, or 0 when it generated none.booleanTrue when beginning, committing and rolling back are protocol requests or session state of this client's own, rather than statements a caller could equally run.Runs a statement and answers with its rows, each aMapof column name to value in projection order.voidrollback()Rolls the open transaction back.voidSavepoints, under the verbs the other engines use.
-
Field Details
-
CONNECTOR
Opens SqlServer sessions; naming this constant is what puts the engine in an application. SeeServerSession.Connector.
-
-
Method Details
-
dialect
Description copied from class:ServerSessionThe SQL this session's server speaks.- Specified by:
dialectin classServerSession
-
ownsTransactionControl
public boolean ownsTransactionControl()Description copied from class:ServerSessionTrue when beginning, committing and rolling back are protocol requests or session state of this client's own, rather than statements a caller could equally run.- Specified by:
ownsTransactionControlin classServerSession
-
connect
public static SqlServer connect(String host, int port, String database, String user, String password, String sslMode, String caFile, int timeoutMillis, int socketTimeoutMillis) throws IOException Opens a session.
sslModeis what the other engines take:require(andverify-full) demand TLS to a certificate that verifies forhost,verify-caa chain tocaFilewithout the name,preferuses TLS and fails when the server has none, since a login cannot be protected without it, anddisableis plaintext, deliberately.strictis SQL Server's own: TDS 8.0, TLS before the first packet, verified asrequireis.A user written
DOMAIN\\usersigns in as that Windows account over NTLMv2; any other is a SQL login.An Azure SQL gateway that answers the login with a redirect is followed, once.
- Throws:
IOException
-
execute
Runs a statement that returns no rows and answers with the number of rows it affected.- Specified by:
executein classServerSession- Throws:
IOException
-
query
Runs a statement and answers with its rows, each aMapof column name to value in projection order. A statement that produces several result sets answers with the first.- Specified by:
queryin classServerSession- Throws:
IOException
-
lastInsertId
public long lastInsertId()The key the last INSERT on this session generated, or 0 when it generated none.- Specified by:
lastInsertIdin classServerSession
-
begin
Opens a transaction.- Specified by:
beginin classServerSession- Throws:
IOException
-
beginReadOnly
Opens a transaction. SQL Server has no read-only transaction, so this isbegin().- Specified by:
beginReadOnlyin classServerSession- Throws:
IOException
-
begin
Opens a transaction at an isolation level. The level is a session setting on SQL Server, so it is put back to READ COMMITTED when the transaction ends.- Specified by:
beginin classServerSession- Throws:
IOException
-
commit
Commits the open transaction.- Specified by:
commitin classServerSession- Throws:
IOException
-
rollback
Rolls the open transaction back. A transaction the server already ended, as it does for the victim of a deadlock, is not an error here.- Specified by:
rollbackin classServerSession- Throws:
IOException
-
savepoint
Savepoints, under the verbs the other engines use. SQL Server spells them SAVE TRANSACTION and ROLLBACK TRANSACTION, and has nothing to release: a savepoint lasts until its transaction ends.- Specified by:
savepointin classServerSession- Throws:
IOException
-
close
public void close()Closes the connection. An open transaction is rolled back by the server.- Specified by:
closein classServerSession
-
isClosed
public boolean isClosed()Description copied from class:ServerSessionTrue once the session is closed or its connection was lost.- Specified by:
isClosedin classServerSession
-