如何使 Java 与 SQL Server 一起工作?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1310536/
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 to make Java work with SQL Server?
提问by doro
I know this is a basic question, but I can't seem to find an answer and I apologize, if this question is way too stupid, but here we go:
我知道这是一个基本问题,但我似乎无法找到答案,如果这个问题太愚蠢,我深表歉意,但我们继续:
I am supposed to work with SQL Server (no problem so far) and with Java (love java, so no problem here either), but now: What am I supposed to do to make the combination work? I got: JRE 1.6and the sqljdbc4.jar... Before I put sqljdbc4.jar into my classpath I had sqljdbc.jar in it and with a test-program I got this exception:
我应该使用 SQL Server(到目前为止没有问题)和 Java(喜欢 Java,所以这里也没有问题),但是现在:我应该怎么做才能使组合工作?我得到了:JRE 1.6和sqljdbc4.jar......在我将 sqljdbc4.jar 放入我的类路径之前,我有 sqljdbc.jar 并且使用测试程序我得到了这个异常:
21.08.2009 09:26:59 com.microsoft.sqlserver.jdbc.SQLServerConnection <init>
SCHWERWIEGEND: Die Java-Laufzeitumgebung (Java Runtime Environment, JRE), Version 1.6,
wird von diesem Treiber nicht unterstützt. Verwenden Sie die Klassenbibliothek
'sqljdbc4.jar', die Unterstützung für JDBC 4.0 bietet.
java.lang.UnsupportedOperationException: Die Java-Laufzeitumgebung (Java Runtime
Environment, JRE), Version 1.6, wird von diesem Treiber nicht unterstützt. Verwenden
Sie die Klassenbibliothek 'sqljdbc4.jar', die Unterstützung für JDBC 4.0 bietet.
at com.microsoft.sqlserver.jdbc.SQLServerConnection.<init>(SQLServerConnection.java:223)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:840)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at msSqlTest.DB.dbConnect(DB.java:13)
at msSqlTest.TestConnection.main(TestConnection.java:7)
Sorry for the German ... It basically means, that I should use sqljdbc4.jar, b/c the JRE I am using is not supported by the driver. So I put sqljdbc4.jar into my classpath, but it didn't work, so I am kinda lost, what I could do.
对不起德国人......这基本上意味着,我应该使用sqljdbc4.jar,b/c驱动程序不支持我正在使用的JRE。所以我将 sqljdbc4.jar 放入我的类路径中,但它不起作用,所以我有点迷失了,我能做什么。
Maybe someone could tell be in an idiot-proof way what I should do :(
也许有人可以以一种防白痴的方式告诉我我应该做什么:(
Oh yeah, here is the test appI use:
哦,是的,这是我使用的测试应用程序:
import java.sql.*;
public class TestConnection{
public static void main(String[] args){
// Neue DB und los geht's :)
DB db = new DB();
db.dbConnect("jdbc:sqlserver://localhost:1433/muff", "user", "pw" );
}
}
class DB{
public void dbConnect( String db_connect_string,
String db_userid,
String db_password){
try{
Class.forName( "com.microsoft.sqlserver.jdbc.SQLServerDriver" );
Connection conn = DriverManager.getConnection(
db_connect_string,
db_userid,
db_password);
System.out.println( "connected" );
}
catch( Exception e ){
e.printStackTrace();
}
}
};
回答by oxbow_lakes
回答by Jesper
Do not put both the old sqljdbc.jar and the new sqljdbc4.jar in your classpath - this will make it (more or less) unpredictable which classes are being used, if both of those JARs contain classes with the same qualified names.
不要将旧的 sqljdbc.jar 和新的 sqljdbc4.jar 都放在您的类路径中 - 如果这两个 JAR 包含具有相同限定名称的类,这将使(或多或少)无法预测正在使用哪些类。
You said you put sqljdbc4.jar in your classpath - did you remove the old sqljdbc.jar from the classpath? You said "it didn't work", what does that mean exactly? Are you sure you don't still have the old JAR in your classpath somewhere (maybe not explicitly)?
你说你把 sqljdbc4.jar 放在你的类路径中 - 你是否从类路径中删除了旧的 sqljdbc.jar ?你说“它不起作用”,这到底是什么意思?你确定你的类路径中没有旧的 JAR(可能不是明确的)?
回答by pjp
The driver you are using is the MS SQL server 2008 driver (sqljdbc4.jar). As stated in the MSDN page it requires Java 6+ to work.
您使用的驱动程序是 MS SQL Server 2008 驱动程序 (sqljdbc4.jar)。正如 MSDN 页面中所述,它需要 Java 6+ 才能工作。
http://msdn.microsoft.com/en-us/library/ms378526.aspx
http://msdn.microsoft.com/en-us/library/ms378526.aspx
sqljdbc4.jar class library requires a Java Runtime Environment (JRE) of version 6.0 or later.
sqljdbc4.jar 类库需要 6.0 或更高版本的 Java 运行时环境 (JRE)。
I'd suggest using the 2005 driver which I beleive is in (sqljdbc.jar) or as Oxbow_Lakes says try the jTDS driver (http://jtds.sourceforge.net/).
我建议使用我相信在 (sqljdbc.jar) 中的 2005 驱动程序,或者像 Oxbow_Lakes 所说的那样尝试 jTDS 驱动程序 ( http://jtds.sourceforge.net/)。
回答by anandan
If you are use sqljdbc4.jar, use the following code
如果你使用的是sqljdbc4.jar,使用下面的代码
ResultSet objResultSet = objPreparedStatement.getResultSet();
if (objResultSet == null) {
boolean bResult = false;
while (!bResult){
if (objPreparedStatement.getMoreResults()){
objResultSet = objPreparedStatement.getResultSet();
bResult = true;
}
}
}
objCachedRowSet = new CachedRowSetImpl();
objCachedRowSet.populate(objResultSet);
if (CommonUtility.isValidObject(objResultSet)) objResultSet.close();
objResultSet = null;
回答by Pedro Guimr?es
Indeed. The thing is that the 2008 R2 version is very tricky. The JTDs driver seems to work on some cases. In a certain server, the jTDS worked fine for an 2008 R2 instance. In another server, though, I had to use Microsoft's JBDC driver sqljdbc4.jar. But then, it would only work after setting the JRE environment to 1.6(or higher).
的确。问题是 2008 R2 版本非常棘手。JTD 驱动程序似乎适用于某些情况。在某台服务器上,jTDS 在 2008 R2 实例上运行良好。但是,在另一台服务器上,我不得不使用 Microsoft 的 JBDC 驱动程序 sqljdbc4.jar。但是,它只有在将 JRE 环境设置为 1.6(或更高)后才能工作。
I used 1.5 for the other server, so I waisted a lot of time on this.
我在另一台服务器上使用了 1.5,所以我在这上面浪费了很多时间。
Tricky issue.
棘手的问题。
回答by Bart de Kort
Maybe a little late, but using different drivers altogether is overkill for a case of user error:
也许有点晚了,但是对于用户错误的情况,完全使用不同的驱动程序是过度的:
db.dbConnect("jdbc:sqlserver://localhost:1433/muff", "user", "pw" );
should be either one of these:
应该是其中之一:
db.dbConnect("jdbc:sqlserver://localhost\muff", "user", "pw" );
(using named pipe) or:
(使用命名管道)或:
db.dbConnect("jdbc:sqlserver://localhost:1433", "user", "pw" );
using port number directly; you can leave out 1433 because it's the default port, leaving:
直接使用端口号;你可以省略 1433 因为它是默认端口,留下:
db.dbConnect("jdbc:sqlserver://localhost", "user", "pw" );
回答by Jorge Sanchez
I had the same problem with a client of my company, the problem was that the driver sqljdbc4.jar, tries a convertion of character between the database and the driver. Each time that it did a request to the database, now you can imagine 650 connections concurrently, this did my sistem very very slow, for avoid this situation i add at the String of connection the following parameter:
我公司的一个客户遇到了同样的问题,问题是驱动程序 sqljdbc4.jar 尝试在数据库和驱动程序之间进行字符转换。每次它向数据库发出请求时,现在您可以想象同时有 650 个连接,这使我的系统非常非常慢,为了避免这种情况,我在连接字符串中添加了以下参数:
SendStringParametersAsUnicode=false, then te connection must be something like url="jdbc:sqlserver://IP:PORT;DatabaseName=DBNAME;SendStringParametersAsUnicode=false"
After that, the system is very very fast, as the users are very happy with the change, i hope my input be of same.
之后,系统非常非常快,因为用户对更改非常满意,我希望我的输入是相同的。
回答by Andrew McDonald
For anyone still googling this, go to \blackboard\config\tomcat\conf and in wrapper.conf put an extra line in wrapper.java.classpath pointing to the sqljdbc4.jar and then update the wrapper.conf.bb as well
对于仍在使用谷歌搜索的任何人,请转到 \blackboard\config\tomcat\conf 并在 wrapper.conf 中在 wrapper.java.classpath 中添加一行指向 sqljdbc4.jar 的额外行,然后也更新 wrapper.conf.bb
Then restart the blackboard services and tomcat and it should work
然后重启黑板服务和tomcat就可以了
It won't work by simply setting your java classpath, you have to set it up in the blackboard config files to point to your jar file with the jdbc library
它不会通过简单地设置您的 java 类路径来工作,您必须在黑板配置文件中将其设置为使用 jdbc 库指向您的 jar 文件