当前位置: 代码迷 >> Java相关 >> [求助]java连接数据库问题
  详细解决方案

[求助]java连接数据库问题

热度:424   发布时间:2006-05-24 13:07:00.0
[求助]java连接数据库问题

系统 xp
数据库 sql server 2000 已经打上了sp4补丁
错误提示
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(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)

代码
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
System.out.println("驱动程序已加载");
conn=DriverManager.getConnection("jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=sheji_data","sa","admin");
System.out.println("OK,成功连接到数据库");

执行过程中可以看到"驱动程序已加载"然后就是
java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Error establishing socket.

在网上查的资料说打上补丁就行了,可是不知道为什么我这里还是出错
请个位高手帮帮忙
谢谢

搜索更多相关的解决方案: jdbc  数据库  java  Source  microsoft  

----------------解决方案--------------------------------------------------------
还得安装jdbc驱动,完全版。

一,你确定你安装的jdbc是否完全版,因为有个单机版只能连接本机上的sql,单机版约2m,完全版约6.5m
二、安装好jdbc后,假如目录在C:\ProgramFiles\MicrosoftSQLServer2000JDBC,打开Jbuilder,选择Tools-->;ConfigureLibraries,然后在左边的列表框下选择New,填入:Name:sql,Location:UserHome,然后点击Add,加入目录C:\ProgramFiles\MicrosoftSQLServer2000JDBC\lib,确定
三、Project-->;PrjectProperties-->;Paths-->;RequiredLibraries-->;Add
加入刚才添加的UserHome/sql确定
四、Tools-->;EnterpriseSetup-->;DatabaseDrivers-->;Add,加入UserHome/sql,添加之后在列表框中显示的是sql.config,确定,重新启动JBuilder
五、Tools-->;DatabasePilot-->;View-->;Options-->;Drivers-->;Add
填入
Driverclass:com.microsoft.jdbc.sqlserver.SQLServerDriver
SimpleURL:microsoft:sqlserver://localhost:1433;DatabaseName=mybase
确定之后,Tools-->;DatabasePilot-->;New的Driver里选择
com.microsoft.jdbc.sqlserver.SQLServerDriver
在URL里填入:
microsoft:sqlserver://服务器Ip地址或主机名:1433;DatabaseName=数据库名,确定!
双击或点击+号输入sql数据库的用户名和密码,就完成了JB与sql的连接!
连接代码可以查看Source!
----------------解决方案--------------------------------------------------------

楼上的回答的很详细啊!我也正在看这一块.


----------------解决方案--------------------------------------------------------

连接Access数据库啊
import java.sql.*;
import java.awt.*;
import javax.swing.*;
import java.io.*;

public class Datebase_test
{
ResultSet rs;
Statement stmt;
Connection con;
public Datebase_test(){
//定义连接类型及连接数据库 ACCESS
try{

File f=new File("test.mdb");
String path=f.getAbsolutePath();
//System.out.println(path);
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String strurl="jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=" + path;
con=DriverManager.getConnection(strurl);
stmt = con.createStatement();



}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}

public static void main(String [] args)
{
Datebase_test DbTest =new Datebase_test();
//create table
/*
String query ="create table user_1 ("
+ "id char(10),"
+ "name char(15),"
+ "tel char(12)"
+")";
DbTest.updateSql(query);*/
//查询数据
String str="select * from Student";
ResultSet rs=DbTest.getResult(str);
try
{
while (rs.next())
System.out.println(rs.getString("StuNo") + " " + rs.getString("StuName"));
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
String str1="select * from user_1";
ResultSet rs1=DbTest.getResult(str1);
try
{
while (rs1.next())
System.out.println(rs1.getString("id") + " " + rs1.getString("name") +" "+ rs1.getString("tel"));
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
//插入数据
String query1="insert into user_1 values ('A00001','jdbc','06332525125')";
String query2="insert into Student values('20050101','ma','男','1')";
DbTest.updateSql(query1);
DbTest.updateSql(query2);
//修改数据
String update1="update user_1 set tel='06332222222' where id='A00001'";
DbTest.updateSql(update1);
//删除数据
String delete1="delete * from user_1 where id='A00001'";
DbTest.updateSql(delete1);

DbTest.closeConnection();
}
//定义数据库执行返回值 RS;
public ResultSet getResult(String strSQL)
{
try{
rs=stmt.executeQuery(strSQL);
return rs;
}
catch(SQLException sqle)
{
System.out.println(sqle.toString());
return null;
}
}
//定义数据库修改保存类
public boolean updateSql(String strSQL)
{
try{
int i;
i=stmt.executeUpdate(strSQL);
System.out.println(i);
System.out.println("Success!");
con.commit();
return true;
}
catch(SQLException sqle)
{
System.out.println(sqle.toString());
return false;
}
}
//定义关闭数据库类
public void closeConnection()
{
try
{
con.close();
}
catch(SQLException sqle)
{
System.out.println(sqle.toString());
}
}

}
不用数据源.


----------------解决方案--------------------------------------------------------
  相关解决方案