我下载了sql server 2000 jdbc for sp3 安装之后,代码如下
import java.sql.*;
class ConnSQL
{
public static void main(String args[])
{
String RL= "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=ck ";
String user = "sa ";
String password = " ";
String sqlStr = "select * from T1 ";
try
{
Class.forName( "com.microsoft.jdbc.sqlserver.SQLServerDriver ");
System.out.println( "类实例化成功! ");
System.out.println( "slkdfj ");
Connection con = DriverManager.getConnection(RL,user,password);
System.out.println( "创建连接对象成功! ");
Statement st = con.createStatement();
System.out.println( "创建Statement成功! ");
ResultSet rs = st.executeQuery(sqlStr);
System.out.println( "操作数据表成功! ");
System.out.println( "---------------- ");
while(rs.next())
{
System.out.println(rs.getString( "area_id ")+ " ");
System.out.println(rs.getString( "area ")+ " ");
}
rs.close();
st.close();
con.close();
}
catch(Exception err)
{
err.printStackTrace(System.out);
}
}
}
它提示说:
java.lang.ClassNotFoundException: com.microsoft.jdbc.sqlserver.SQLServerDriver
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at ConnSQL.main(ConnSQL.java:14)
我在网上查说是classpath 设置有问题。但是我的classpath如下 :.;E:\j2sdk\lib\tools.jar;E:\j2sdk\lib\dt.jar;E:\j2sdk\bin;
,;D:\Program Files\Microsoft SQL Server 2000 Driver for JDBC\lib\msbase.jar;D:\Program Files\Microsoft SQL Server 2000 Driver for JDBC\lib\mssqlserver.jar;D:\Program Files\Microsoft SQL Server 2000 Driver for JDBC\lib\msutil.jar; 三个.jar文件都包括进去了,应该没有问题的呀。也试过把 .;E:\j2sdk\lib\tools.jar;E:\j2sdk\lib\dt.jar;E:\j2sdk\bin; 这个去掉。运行还是不行? 这是怎么回事呢?
请指教。谢谢
------解决方案--------------------
把jdbc的包放在项目的lib目录下。
------解决方案--------------------