如何在 Java 中配置数据源以连接到 MS SQL Server?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1080354/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you configure a DataSource in Java to connect to MS SQL Server?
提问by Eric Wilson
I'm trying to follow Java's JDBC tutorials to write a Java program that can connect to SQL Server 2008. I'm getting lost at the point of making a connection.
我正在尝试按照 Java 的 JDBC 教程编写一个可以连接到 SQL Server 2008 的 Java 程序。我在建立连接时迷路了。
The following snippet is from the tutorial:
以下片段来自教程:
InitialContext ic = new InitialContext();
DataSource ds = ic.lookup("java:comp/env/jdbc/myDB");
Connection con = ds.getConnection();
DataSource ds = (DataSource) org.apache.derby.jdbc.ClientDataSource()
ds.setPort(1527);
ds.setHost("localhost");
ds.setUser("APP")
ds.setPassword("APP");
Connection con = ds.getConnection();
There's no explanation of what comp/env/jdbc/myDB should point to, and I don't know how I should choose a port. Also, the object ds seems to be defined twice.
没有解释 comp/env/jdbc/myDB 应该指向什么,我不知道我应该如何选择端口。此外,对象 ds 似乎被定义了两次。
I'm using the JSQLDataSource
driver, for the record. Can anyone point me in the right direction here?
我正在使用JSQLDataSource
驱动程序,作为记录。任何人都可以指出我正确的方向吗?
http://java.sun.com/docs/books/tutorial/jdbc/basics/connecting.html
http://java.sun.com/docs/books/tutorial/jdbc/basics/connecting.html
采纳答案by duffymo
Start with the JDBC tutorialor the Microsoft docs.
and this:
和这个:
String driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
Class.forName(driver);
String url = "jdbc:microsoft:sqlserver://host:1433/database";
Connection conn = DriverManager.getConnection(url, "username", "password");
Fill in your values for host, database, username, and password. The default port for SQL server is 1433.
填写主机、数据库、用户名和密码的值。SQL 服务器的默认端口是 1433。
UPDATE: Good point below. JDBC drivers can be had from both Microsoft and jTDS. I prefer the latter.
更新:下面的好点。JDBC 驱动程序可以从 Microsoft 和 jTDS 获得。我更喜欢后者。
JNDI lookups have to do with Java EE app servers that support connection pooling. You can ask the app server to create a pool of connections, which can be an expensive thing to do, and loan them out to clients like library books as needed.
JNDI 查找与支持连接池的 Java EE 应用服务器有关。您可以要求应用程序服务器创建一个连接池,这可能是一件昂贵的事情,并根据需要将它们借给客户,例如图书馆书籍。
If you aren't using a Java EE app server or connection pooling, you have to create the connection on your own. That's where manual processes and DriverManager come in.
如果您不使用 Java EE 应用程序服务器或连接池,则必须自行创建连接。这就是手动流程和 DriverManager 的用武之地。
EXPLANATION: As for why the Sun tutorial shows DataSource twice, I'd say it's a case of poor editing. If you look above the code sample it says you can get a DataSource "by lookup or manually". The code snippet below shows both together, when it should be one or the other.
解释:至于为什么 Sun 教程显示 DataSource 两次,我会说这是一个糟糕的编辑案例。如果您查看代码示例上方的内容,它会说您可以“通过查找或手动”获取数据源。下面的代码片段同时显示了两者,何时应该是其中之一。
You know it's an inadvertent error because there's no way the code as written could compile. You have "ds" declared twice.
您知道这是一个无意的错误,因为编写的代码无法编译。你已经声明了两次“ds”。
So it should read "...lookup", followed by its code snippet, and then "...manually", followed by its code snippet.
所以它应该读“...lookup”,然后是它的代码片段,然后是“...手动”,然后是它的代码片段。
回答by eric.christensen
I like the jTDSdriver for connecting to SQL Server.
A URL will look like this:
我喜欢用于连接 SQL Server的jTDS驱动程序。
URL 将如下所示:
jdbc:jtds:sqlserver://localhost/Finance;instance=sqlexpress
Check thisfor jTDS Url Info.
检查这对JTDS地址信息。
Thisalso has some interesting information to help troubleshoot jtds to sql express sorts of problems.
这也有一些有趣的信息可以帮助解决 jtds 到 sql express 的各种问题。
回答by Mukus
This question has already been answered long time ago. The question was asked about JNDI lookup. With lookup you have to see the application server log to see what the connection is bound to. For example in Jboss startup, I can see:
这个问题很久以前就有答案了。问了有关 JNDI 查找的问题。通过查找,您必须查看应用程序服务器日志以查看连接绑定到的内容。例如在 Jboss 启动中,我可以看到:
[ConnectionFactoryBindingService] Bound ConnectionManager 'jboss.jca:service=DataSourceBinding,name=myDB' to JNDI name 'java:myDB'
Now that is the clue. That is exactly how I should do the lookup -
现在这就是线索。这正是我应该进行查找的方式 -
InitialContext ic = new InitialContext();
DataSource ds = ic.lookup("java:myDB");
Notice how the server log and the code both point to the JNDI name java:myDB.
请注意服务器日志和代码如何都指向 JNDI 名称java:myDB。
回答by user3203060
DataSource ds = new SimpleDriverDataSource(new com.mysql.jdbc.Driver(),
"jdbc:mysql://database:1433;databaseName=name", "username", "password");
JdbcTemplate jdbc = new JdbcTemplate(ds);
回答by JonnyRaa
I'm not sure anyone above has really answered the question.
我不确定上面有人真的回答了这个问题。
I found this microsoft sample useful.
我发现这个微软示例很有用。
The key information in there is really that the class you need is SQLServerDataSourcethat is basically a configuration object - you use it something like this:
那里的关键信息实际上是您需要的类是SQLServerDataSource,它基本上是一个配置对象 - 您可以像这样使用它:
SQLServerDataSource dataSource = new SQLServerDataSource();
dataSource.setUser("aUser");
dataSource.setPassword("password");
dataSource.setServerName("hostname");
dataSource.setDatabaseName("db");
You would then call
然后你会打电话
dataSource.getConnection();
to get a connection object which is basically the thing you use to talk to the database.
获得一个连接对象,它基本上是你用来与数据库交谈的东西。
Use
用
connection.prepareStatement("some sql with ? substitutions");
to make something for firing off sql and:
做一些事情来触发 sql 和:
connection.prepareCall
for calling stored procedures.
用于调用存储过程。