jdbc连接SQL 2000数据库的问题,怎么也连不上,请高手看看!
我试用的操作系统是 XP Home,数据库是 sql 2000,编写了个测试数据连接的程序,但是不管怎么也执行不了,编译可以通过,以下是所有源代码,请高手指点:
import
java.sql.SQLException;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.DriverManager;
import java.sql.Date;
import java.util.Properties;
import java.io.FileInputStream;
import
java.io.IOException;
import
java.io.FileNotFoundException;
public class GetConn
{
private Connection con;
private String url;//数据库URL
private String userName;//登录数据库用户名
private String password;//用户密码
private String strDriver;
public static void main(String[] args)
{
GetConn dbConn = new GetConn();
dbConn.getProperty();
Connection cnn= dbConn.getConnection();
}
/**
* 返回到数据库的一个连接,在一个系统或类中,如果经常进行数据库的相关操作
* 会把建立数据库的连接作为一个单独的方法。
*/
public Connection getConnection()
{
try
{
con = DriverManager.getConnection(url, userName, password);
}
catch(
SQLException e)
{
e.printStackTrace();
}
return con;
}
/**
* 读取属性配置文件
*/
public void getProperty()
{
Properties prop = new Properties();
try
{
FileInputStream in = new FileInputStream( "Drivers.properties ");
prop.load(in);
String driver = prop.getProperty( "drivers ");
if(driver != null)
System.setProperty( "jdbc.drivers ", driver);
url = prop.getProperty( "url ");
userName = prop.getProperty( "user ");
password = prop.getProperty( "password ");
strDriver=driver;
}
catch(
FileNotFoundException e)
{
e.printStackTrace();
}
catch(
IOException e)
{
e.printStackTrace();
}
}
}
//属性文件
drivers=com.microsoft.jdbc.sqlserver.SQLServerDriver
url=jdbc:microsoft:sqlserver://xyz:1433;DatabaseName=java
user=sa
password=123
//编译后通过java GetConn 执行提示以下错误:
Exception in thread "main "
java.lang.NoClassDefFoundError:com/microsoft/util/utilDummyPrintWriter
at java.lang.Class.forName0(Native Method)
at java.lang.class.forName(Unknown Source)
at java.sql.DriverManager.loadInitialDrivers(unknown Source)
at java.sql.DriverManager.getConnection(unknown Source)
at GetConn.getConnection(GetConn.java:37)
搞不懂是哪里出了问题?
------解决方案--------------------public Connection getConnection() throws Exception
{
Class.forName(drivers);
return DriverManager.getConnection(url, userName, password);
}
------解决方案--------------------at java.sql.DriverManager.loadInitialDrivers(unknown Source)
at java.sql.DriverManager.getConnection(unknown Source)
at GetConn.getConnection(GetConn.java:37)