Topic: 各位哥哥姐姐帮个忙啦!!(关于JDBC)

  Print this page

1.各位哥哥姐姐帮个忙啦!!(关于JDBC) Copy to clipboard
Posted by: 193kuanglv
Posted on: 2006-04-06 08:54

快一个星期了 我真的搞不通啊!!!
就是连不上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

2.Re:各位哥哥姐姐帮个忙啦!!(关于JDBC) [Re: 193kuanglv] Copy to clipboard
Posted by: Jcat
Posted on: 2006-04-06 14:03

你的代码太多了,我来不及看了,而且我现在机子上装的不是MS SQL,所以也没办法验证。不过我以前连过MS SQL(步骤见下图),你试试。

3.Re:各位哥哥姐姐帮个忙啦!!(关于JDBC) [Re: 193kuanglv] Copy to clipboard
Posted by: java8964
Posted on: 2006-04-12 01:22

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


   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