Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Database/JDBC/SQL/JDO/Hibernate » MS SQL  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 各位哥哥姐姐帮个忙啦!!(关于JDBC)
193kuanglv





发贴: 6
积分: 0
于 2006-04-06 08:54 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
快一个星期了 我真的搞不通啊!!!
就是连不上SQL SERVER

import java.util.*;
import java.sql.*;

public class TestDB
{
public static void main(String[] args)
{
try {
runTest();
}
catch (SQLException ex) {
while (ex != null)
{
ex.printStackTrace();
ex.getNextException();
}
}
}
public static void runTest() throws SQLException
{
Connection con = TestDB.getConnection();
try
{
Statement state = con.createStatement();

state.execute("CREATE TABLE Greetings (Message CHAR(20))");
state.execute("INSERT INTO Greetings VALUES ('hello world!')");

ResultSet result = state.executeQuery("SELECT * FROM Greetings");
result.next();
System.out.print(result.getString(1));
//state.execute("DROP TABLE Greetings");
}
finally
{
con.close();
}
}
public static Connection getConnection() throws SQLException
{
String drivers = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
System.setProperty("jdbc.drivers", drivers);
String url = "jdbc:microsoft:sqlserver://192.168.0.3:1433;databasename=COREJAVA";
String username = "sa";
String password = "awdz123";

return DriverManager.getConnection(url,username,password);
}
}
使用系统版本: windows 2003 server enterprise edition
使用编译工具: JBuilder 2006
使用数据库版本 SQL Server 2000
连接时出现的异常:
java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Error establishing socket.
  at com.microsoft.jdbc.base.BaseExceptions.createException(Unknown Source)
  at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
  at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
  at com.microsoft.jdbc.sqlserver.tds.TDSConnection.<init>(Unknown Source)
  at com.microsoft.jdbc.sqlserver.SQLServerImplConnection.open(Unknown Source)
  at com.microsoft.jdbc.base.BaseConnection.getNewImplConnection(Unknown Source)
  at com.microsoft.jdbc.base.BaseConnection.open(Unknown Source)
  at com.microsoft.jdbc.base.BaseDriver.connect(Unknown Source)
  at java.sql.DriverManager.getConnection(DriverManager.java:525)
  at java.sql.DriverManager.getConnection(DriverManager.java:171)
  at testdb.TestDB.getConnection(TestDB.java:49)
  at testdb.TestDB.runTest(TestDB.java:23)
  at testdb.TestDB.main(TestDB.java:11)
java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Error establishing socket.
  at com.microsoft.jdbc.base.BaseExceptions.createException(Unknown Source)
  at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
  at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
  at com.microsoft.jdbc.sqlserver.tds.TDSConnection.<init>(Unknown Source)
  at com.microsoft.jdbc.sqlserver.SQLServerImplConnection.open(Unknown Source)
  at com.microsoft.jdbc.base.BaseConnection.getNewImplConnection(Unknown Source)
  at com.microsoft.jdbc.base.BaseConnection.open(Unknown Source)
环境变量已设置,也将3个.jar文件拷到目标文件夹了,SQL Server 使用Ado.net 连接时没有问题。
一开始以为各软件有问题,全部重新安装(包括系统),搞了一个礼拜,快崩溃了!!!
求你们了,一定得帮个忙, 因为这个问题每天吃不好,睡不着,
请帮我一下,一定,请帮我!!! QQ357037014



作者 Re:各位哥哥姐姐帮个忙啦!!(关于JDBC) [Re:193kuanglv]
Jcat

熊猫的猫



发贴: 266
积分: 16
于 2006-04-06 14:03 user profilesend a private message to usersend email to Jcatsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
你的代码太多了,我来不及看了,而且我现在机子上装的不是MS SQL,所以也没办法验证。不过我以前连过MS SQL(步骤见下图),你试试。



编程,游泳,睡觉--SleepingCat
作者 Re:各位哥哥姐姐帮个忙啦!!(关于JDBC) [Re:193kuanglv]
java8964





发贴: 49
积分: 24
于 2006-04-12 01:22 user profilesend a private message to usersend email to java8964search all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
He is using pure JDBC driver, not the ODBC, so the above help is not very useful.

This error means that the MS SQL server JDBC driver can't make the connection to the DB server.
Please check your system as follows:

1) Is the sql server running? Can you login into your sql server using MS tools (Like Query Analyzer)?
2) Are the java code running remote to the sql server host? Means if the java code running in the same machine as your MS sql server?
3) If java code and sql server running on differnect machines, any firewall on DB server stop the connection?
4) Is your MS SQL server supporting TCP/IP connection enabled?

Most case, you made mistake in Step 3 or 4. How can you check? Do this test in DOS command promopt:

C:\> telnet db_server_host 1433

What's the response? If you get response like 'Connection failed', then of course the JDBC can't connect either. If you find out some problems, analyze the problem step by step. 'Divide-and-Conquer", is the solultion for all problems.

BTW, NEVER post your sa password on the web. Smile




flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923