如何在Java中配置数据源以连接到MS SQL Server? [英] How do you configure a DataSource in Java to connect to MS SQL Server?

查看:125
本文介绍了如何在Java中配置数据源以连接到MS SQL Server?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图按照Java的JDBC教程来编写可以连接到SQL Server 2008的Java程序。在建立连接时我迷路了。

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.

以下代码段来自本教程:

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(); 

没有解释comp / env / jdbc / myDB应该指向什么,我也没有知道如何选择端口。此外,对象ds似乎定义了两次。

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.

我正在使用 JSQLDataSource 驱动程序进行记录。有人可以在这里向我指出正确的方向吗?

I'm using the JSQLDataSource driver, for the record. Can anyone point me in the right direction here?

http://java.sun.com/docs/books/tutorial/jdbc/basics/connecting.html

推荐答案

JDBC教程开始 Microsoft文档

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");

填写主机,数据库,用户名和密码的值。 SQL Server的默认端口为1433。

Fill in your values for host, database, username, and password. The default port for SQL server is 1433.

更新:下面的好处。 JDBC驱动程序可以从Microsoft和jTDS获得。我更喜欢后者。

UPDATE: Good point below. JDBC drivers can be had from both Microsoft and jTDS. I prefer the latter.

JNDI查找与支持连接池的Java EE应用服务器有关。您可以要求应用服务器创建一个连接池,这可能是一件很昂贵的事情,然后根据需要将它们借给诸如图书馆书籍之类的客户端。

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.

如果您没有使用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.

解释:关于为什么Sun教程两次显示DataSource的情况,我想说这是编辑不佳的情况。如果您在代码示例上方查看,则表示您可以通过查找或手动获取数据源。下面的代码段同时显示了两者的位置。

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.

您知道这是一个无意中的错误,因为编写的代码无法编译。您已经两次声明了 ds。

You know it's an inadvertent error because there's no way the code as written could compile. You have "ds" declared twice.

因此,它应该读为 ... lookup,然后是代码段,然后是 ... manually按其代码段。

So it should read "...lookup", followed by its code snippet, and then "...manually", followed by its code snippet.

这篇关于如何在Java中配置数据源以连接到MS SQL Server?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆