この記事では、Java データベース接続 (JDBC) と、構成中に発生するトラブルシューティング手順について説明します。 JDBC for SQL Server に焦点を当てています。
Note
- この記事は、C ドライブのルートにインストールされている最新の JDBC ドライバー (version 12.4) に基づいています。
- Microsoft では、サードパーティの接続プール マネージャーが存在する JDBC 接続のトラブルシューティングを行いません。 サードパーティの接続プール マネージャーを使用したトラブルシューティングでは、知的財産情報が公開される可能性があります。
SQL Server 用 Microsoft JDBC ドライバー
この記事では、ドライバーとサポート ドキュメント、さまざまなオペレーティング システム (OS) のインストール手順、SQL Server 接続の問題のトラブルシューティングなど、JDBC のリファレンス ガイドを提供します。
JDBC ドライバーのバージョンの変更
JDBC の要件
Java ランタイム環境 (JRE) バージョンは、ドライバーと、名前に指定された JRE バージョンと一致している必要があります。 たとえば、 mssql-jdbc-9.4.1.jre8.jar には JRE 1.8 が必要であり、 mssql-jdbc-9.4.1.jre11.jar には JRE 11.0 が必要です。
CLASSPATH は、ディレクトリ パスとバイナリ jar ファイルを含む Java 環境変数です。 Java では、目的のアプリケーションを実行する必要があります。 Java を実行する必要があるドライバーと依存関係のバイナリ jar ファイルを指定する必要があります。 最小
CLASSPATHには、現在の作業ディレクトリ.;と JDBC ドライバー jar ファイルの場所が含まれます。
JDBC の構成とトラブルシューティングの手順
CLASSPATH 変数を設定する
CLASSPATHs は、OS 環境変数または Tomcat などのアプリケーション環境自体で定義できます。 CLASSPATHがアプリケーション環境で定義されている場合、アプリケーション ベンダーまたは開発者は、適切なCLASSPATH構成を行うために関与する必要があります。
CLASSPATHを設定するには、次のいずれかの方法を使用します。
Note
コマンド プロンプトの設定は一時的であり、コマンド プロンプト ウィンドウを閉じると削除されます。 グラフィカル ユーザー インターフェイス (GUI) は永続的な設定であり、再起動が必要です。
コマンド プロンプトの例
Set CLASSPATH=.;C:\sqljdbc_12.4\enu\mssql-jdbc-12.4.0.jre8.jar
GUI の例
GUI を使用して CLASSPATH を設定するには、次の手順に従います。
コントロール パネルを開き、システムとセキュリティを選択します。
System>Advanced システム設定を選択します。
Environment Variables>New を選択し、変数名として「CLASSPATH」と入力します。
編集を選択し、「」と入力します。変数値として C:\sqljdbc_12.4\enu\mssql-jdbc-12.4.0.jre8.jar。
[OK] を選択します。
渡された資格情報を持つ接続文字列
渡された資格情報を持つ接続文字列は、認証資格情報 (ユーザー名やパスワードなど) をパラメーターまたは文字列内の値として含む接続文字列を参照します。 プログラムは、データベースまたは他のサービスに接続するときに、セキュリティで保護された接続を確立するために認証情報を提供する必要があります。
次の接続文字列は、使用する認証モードに基づいて SQL Server データベースに接続する方法の例を示しています。
SQL Server 認証
接続文字列は次の値です。 String connectionUrl = "jdbc:sqlserver://<ServerName>:<PortNum>;user=<MySQLAuthAccount>;password=<MyPassword>;trustServerCertificate=true;"
統合セキュリティのない Windows AD 認証
接続文字列は次の値です。 String connectionUrl = "jdbc:sqlserver://<ServerName>:<PortNum>;user=<MyADAuthAccount>;password=<MyPassword>;Domain=<MyDomain>;trustServerCertificate=true;javaAuthentication=NTLM"
Kerberos を使用した統合セキュリティのない Windows AD 認証
接続文字列は次の値です。 String connectionUrl = "jdbc:sqlserver://<ServerName>:<PortNum>;user=<MyADAuthAccount>;password=<MyPassword>;Domain=<MyDomain>;trustServerCertificate=true;javaAuthentication=JavaKerberos"
統合 NTLM 接続
この種の接続では、クライアント コンピューターが Windows ドメインに存在する必要があります。
mssql-jdbc_auth-<version>-<arch>.dll ファイルは、次のパスに存在する必要があります。
64 ビット DLL
%Path%;C:\sqljdbc_12.4.1.0_enu\sqljdbc_12.4\enu\auth\x64\mssql-jdbc_auth-12.4.1.x64.dll32 ビット DLL
%Path%;C:\sqljdbc_12.4.1.0_enu\sqljdbc_12.4\enu\auth\x86\mssql-jdbc_auth-12.4.1.x86.dll
パスを変更して追加するか、既に確立されているパスにファイルをコピーできます。 詳細については、「 Windows での統合認証を使用した接続」を参照してください。
接続文字列は次の値です。 String connectionUrl = "jdbc:sqlserver://<ServerName>:<PortNum>;integratedSecurity=true;Domain=<MyDomain>;trustServerCertificate=true;javaAuthentication=NTLM"
統合 Kerberos 接続
この種類の接続の前提条件は次のとおりです。
- ドメインの一部である必要があります。
- Linux OS に SSSD がインストールされ、構成されている必要があります。
- Linux OS に Klist がインストールされ、構成されている必要があります。
mssql-jdbc_auth-<version>-<arch>.dll ファイルは、次のパスに存在する必要があります。 パスを変更して追加するか、既に確立されているパスにファイルをコピーできます。
64 ビット DLL
%Path%;C:\sqljdbc_12.4.1.0_enu\sqljdbc_12.4\enu\auth\x64\mssql-jdbc_auth-12.4.1.x64.dll32 ビット DLL
%Path%;C:\sqljdbc_12.4.1.0_enu\sqljdbc_12.4\enu\auth\x86\mssql-jdbc_auth-12.4.1.x86.dll
また、 Jaas.conf ファイルを作成する必要があります。 既定では、このファイルはドライバーに付属していないため、Java ではインストールされません。 環境でこのファイルを見つけるのに役立つには、次のいずれかの方法を使用します。
Note
Jaas.conf ファイルを使用すると、Java はログインユーザーの現在のコンテキストを使用できます。 また、現在キャッシュされている Kerberos チケットを使用するように Java に指示します。
Java.Security ファイルの次の行を変更します。
# Default login configuration file login.config.url.1=C:=\<Path to the File>\jaas.confまたは、環境またはアプリケーションを読み込むときに、パラメーターを使用して Jaas.conf ファイルを追加します。 Java ファイルをコンパイルするときは、必ず同じパラメーターを使用してください。
javac -Djava.security.auth.login.config=c:\myDirectory\Jaas.conf myapp.java java -Djava.security.auth.login.config=c:\myDirectory\Jaas.conf myapp
Kerberos 統合認証を使用して SQL Server への接続を確立するには、 Jaas.conf ファイルを構成します。
SQLJDBCDriver {
com.sun.security.auth.module.Krb5LoginModule required
useTicketCache=true;
};
接続文字列は次の値です。 String connectionUrl = "jdbc:sqlserver://<ServerName>:<PortNum>;integratedSecurity=true;Domain=<MyyDomain>;trustServerCertificate=true;javaAuthentication=JavaKerberos;"
サンプル コード
すべての JDBC ドライバーには、 \sqljdbc_12.4\enu\samples ディレクトリにサンプル コードが含まれています。 最も一般的に使用されるのは、 \sqljdbc_12.4\enu\samples\connections\ConnectURR.java です。 ConnectURL.javaという名前のファイルを作成するか、ドライバーに付属のサンプルのConnectURL.javaを使用します。
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class ConnectURL {
public static void main(String[] args) {
// Create a variable for the connection string. Base the connection string on the previous examples supplied in the above documentation.
String connectionUrl = "jdbc:sqlserver://ServerName:Port;user=SQLAuthAccount;password=SomePassword;trustServerCertificate=true;";
try (Connection con = DriverManager.getConnection(connectionUrl); Statement stmt = con.createStatement();)
{
String SQL = "SELECT @@version";
ResultSet rs = stmt.executeQuery(SQL);
// Iterate through the data in the result set and display it.
while (rs.next())
{
System.out.println(rs.getString(1));
}
}
// Handle any errors that may have occurred.
catch (SQLException e)
{e.printStackTrace(); }
}
}
JDBC ドライバーのトレース
一般に、トレースを常に FINEST に設定して詳細を確認します。 ドライバー トレースには 2 つの方法があります。 プログラムによるトレースの有効化と logging.properties ファイルを使用したトレースの の有効化です。
logging.properties ファイルを使用する場合は、logging.properties ファイルの適切な環境を見つける必要があります。 $JAVA_HOME\conf\ と $JAVA_HOME\jre\lib には 2 つの場所があります。
このファイルを構成するには、次の手順に従います。
logging.properties ファイルを変更して、次のグローバル プロパティに似ているようにします。
############################################################ # Global properties ############################################################ # "handlers" specifies a comma-separated list of log Handler # classes. These handlers will be installed during VM startup. # Note that these classes must be on the system classpath. # By default, we only configure a ConsoleHandler, which will only # show messages at the INFO and above levels. handlers= java.util.logging.ConsoleHandler # To also add the FileHandler, use the following line instead. #handlers= java.util.logging.FileHandler # Default global logging level. # This specifies which kinds of events are logged across # all loggers. For any given facility this global level # can be overridden by a facility-specific level # Note that the ConsoleHandler also has a separate level # setting to limit messages printed to the console. .level= INFOハンドラーは、出力をエクスポートする場所を Java に指示します。 FileHandler がファイルに書き込み、ConsoleHandler がコンソール ウィンドウに書き込む場所が 2 つあります。 出力によって大量のデータが生成されるため、ファイルに書き込む必要があります。
コメント行
#handlers= java.util.logging.ConsoleHandlerコメント解除行
handlers= java.util.logging.FileHandler
Note
.levelをOFFに設定すると、コンソール ウィンドウにメッセージが表示されません。.level=OFF特定の FileHandler ログを設定します。
############################################################ # Handler specific properties. # Describes specific configuration info for Handlers. ############################################################ # default file output is in user's home directory. java.util.logging.FileHandler.pattern = %h/java%u.log java.util.logging.FileHandler.limit = 50000 java.util.logging.FileHandler.count = 1 # Default number of locks FileHandler can obtain synchronously. # This specifies maximum number of attempts to obtain lock file by FileHandler # implemented by incrementing the unique field %u as per FileHandler API documentation. java.util.logging.FileHandler.maxLocks = 100 java.util.logging.FileHandler.formatter = java.util.logging.XMLFormatter # Limit the messages that are printed on the console to INFO and above. java.util.logging.ConsoleHandler.level = INFO java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter # Example to customize the SimpleFormatter output format # to print one-line log message like this: # <level>: <log message> [<date/time>] # # java.util.logging.SimpleFormatter.format=%4$s: %5$s [%1$tc]%nこの部分を変更して、次の行に似ているか含まれるようにします。
java.util.logging.FileHandler.pattern = /Path/java%u.log java.util.logging.FileHandler.limit = 5000000 java.util.logging.FileHandler.count = 20 java.util.logging.FileHandler.formatter = java.util.logging.SimpleFormatter java.util.logging.FileHandler.level = FINESTjava.util.logging.FileHandler.pattern = %h/java%u.log行を変更し、%h/ファイルを保存するパスに置き換えます。 例えば次が挙げられます。java.util.logging.FileHandler.pattern = c:/Temp/java%u.logドライバーのログ 記録レベルを設定します。
次のセクションの下部に
com.microsoft.sqlserver.jdbc.level=FINESTを追加します。############################################################ # Facility-specific properties. # Provides extra control for each logger. ############################################################ # For example, set the com.xyz.foo logger to only log SEVERE # messages: # com.xyz.foo.level = SEVERE変更を保存します。
ファイルは次のようになります。
############################################################ # Default Logging Configuration File # # You can use a different file by specifying a filename # with the java.util.logging.config.file system property. # For example, java -Djava.util.logging.config.file=myfile ############################################################ ############################################################ # Global properties ############################################################ # "handlers" specifies a comma-separated list of log Handler # classes. These handlers will be installed during VM startup. # Note that these classes must be on the system classpath. # By default we only configure a ConsoleHandler, which will only # show messages at the INFO and above levels. #handlers= java.util.logging.ConsoleHandler # To also add the FileHandler, use the following line instead. handlers= java.util.logging.FileHandler # Default global logging level. # This specifies which kinds of events are logged across # all loggers. For any given facility this global level # can be overridden by a facility-specific level # Note that the ConsoleHandler also has a separate level # setting to limit messages printed to the console. .level= OFF ############################################################ # Handler specific properties. # Describes specific configuration info for Handlers. ############################################################ # default file output is in user's home directory. java.util.logging.FileHandler.pattern = c:/Temp/java%u.log java.util.logging.FileHandler.limit = 50000 java.util.logging.FileHandler.count = 1 # Default number of locks FileHandler can obtain synchronously. # This specifies maximum number of attempts to obtain lock file by FileHandler # implemented by incrementing the unique field %u as per FileHandler API documentation. java.util.logging.FileHandler.maxLocks = 100 java.util.logging.FileHandler.formatter = java.util.logging.SimpleFormatter # Limit the messages that are printed on the console to INFO and above. #java.util.logging.ConsoleHandler.level = INFO #java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter # Example to customize the SimpleFormatter output format # to print one-line log message like this: # <level>: <log message> [<date/time>] # # java.util.logging.SimpleFormatter.format=%4$s: %5$s [%1$tc]%n ############################################################ # Facility-specific properties. # Provides extra control for each logger. ############################################################ # For example, set the com.xyz.foo logger to only log SEVERE # messages: # com.xyz.foo.level = SEVERE com.microsoft.sqlserver.jdbc.level=FINEST
エラーを再現した後、変更を元に戻して、ロガーによるファイルの作成を停止します。
または、上記のテキストを作成またはコピーしてファイルに保存し、アプリケーションの読み込み時にファイルをスタートアップ コマンドに追加することもできます。
java -Djava.util.logging.config.file=c:\<Path to the file>\logging.properties myapp
これにより、 logging.properties コマンド ラインを使用して $JAVA_HOME\conf\ および $JAVA_HOME\jre\lib 既定のディレクトリで指定されていないファイルを識別できます。
サードパーティの情報に関する免責事項
この資料に記載されているサードパーティ製品は、マイクロソフトと関連のない他社の製品です。 明示的か黙示的かにかかわらず、これらの製品のパフォーマンスや信頼性についてマイクロソフトはいかなる責任も負わないものとします。