私はJavaを学んでおり、JDBC経由でMSSQLからいくつかのデータを取得するために単純なものを実行する必要があります。私の本の例は機能しませんが (ただし、数年前のものです)、以下の MS の例も機能しません。
http://msdn.microsoft.com/en-us/library/ms378956(v=sql.90).aspx
これが私のコードです:
package javasql;
import java.sql.*;
import java.util.*;
public class Program {
private static String url = "jdbc:sqlserver://localhost\\SQLExpress;database=Northwind;integratedSecurity=true;";
//private static String userName = "sa";
//private static String password = "myPassword";
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
RunDemo();
}
public static void RunDemo() {
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection connection = DriverManager.getConnection(url);
Statement statement = connection.createStatement();
ResultSet results = statement.executeQuery("SELECT ProductName, Price FROM Products ORDER BY ProductName");
while(results.next()) {
System.out.println("Product Name: " + results.getNString("ProductName") + " Price: $" + results.getFloat("UnitPrice"));
}
} catch (ClassNotFoundException | SQLException ex) {
System.out.println(ex.getMessage());
}
}
}
コードを実行しても、例外はスローされません。出力ウィンドウに次のように表示されます。
run:
com.microsoft.sqlserver.jdbc.SQLServerDriver
BUILD SUCCESSFUL (total time: 0 seconds)
NetBeans 7.2 を使用しています。誰かが私に実用的な例を教えてください。
編集:
ちなみに、 が表示されている接続文字列については\\SQLExpress
、それを削除してinstanceName=SQLExpress
代わりに使用しようとしましたが、それも効果がありませんでした。
編集2:
OK、MS から MSSQL 用の最新の JDBC ドライバーをダウンロードし、そこにある 2 つの JAR ファイルを参照しました。今、私はこの出力を得ています:
run:
The connection to the host localhost, named instance SQLExpress failed.
Error: "java.net.SocketTimeoutException: Receive timed out".
Verify the server and instance names and check that no firewall is blocking UDP traffic to port 1434.
For SQL Server 2005 or later, verify that the SQL Server Browser Service is running on the host.
BUILD SUCCESSFUL (total time: 15 seconds)
進行状況..少なくとも、接続しようとしていることがわかりますが、上記のエラーについて誰か教えてもらえますか?
編集3:
さらに 2 つの問題が修正されました。1 つは SQL Server Browser の有効化、2 つ目は SQL Server の TCP/IP の有効化です。ありがとう@Vikdor今、私はこのエラーが発生しています:
run:
The TCP/IP connection to the host localhost, port 1433 has failed. Error: "Connection refused: connect. Verify the connection properties. Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. Make sure that TCP connections to the port are not blocked by a firewall.".
BUILD SUCCESSFUL (total time: 15 seconds)
Windows ファイアウォールを確認し、そのポートを許可するインバウンド ルールを追加しましたが、まだ上記のエラーが発生します。何か案は?
編集4:
このリンクで解決策を試しました: http://www.coderanch.com/t/306316/JDBC/databases/SQLServerException-TCP-IP-connection-host
EDIT 3でエラーが発生しなくなりました。現在、別のエラーが発生しています...
run:
Sep 21, 2012 11:33:16 AM com.microsoft.sqlserver.jdbc.AuthenticationJNI <clinit>
WARNING: Failed to load the sqljdbc_auth.dll cause : no sqljdbc_auth in java.library.path
This driver is not configured for integrated authentication. ClientConnectionId:577f359e-4774-45f3-96fb-588785911817
BUILD SUCCESSFUL (total time: 14 seconds)
これにうんざりしています..なぜJava、なぜ?? 真剣に...私は主に .NET を使って仕事をしていてよかったと思っています。まあ、解決策を見つけたら、ここに投稿して、他の人が怒る前に役立つことを確認します...
編集5:
これは役に立ちました: MicrosoftSQLServer 2005に接続するJava
ディレクトリ パスを PATH 環境変数に入れました。うまくいかなかったのでsqljdbc_auth.dll
、JDKフォルダーにも入れましたC:\Program Files\Java\jdk1.7.0_04\bin
。解決しました。