在Applet中使用微软的JDBC数据库驱动时报错
在Applet中使用微软的JDBC数据库驱动时报错
在Applet中使用微软的JDBC数据库驱动时报错,说找不到数据库驱动。
代码如下:
/*
* @(#)HelloWorld.java 1.0 04/04/21
*
* You can modify the template of this file in the
* directory ..\JCreator\Templates\Template_2\Project_Name.java
*
* You can also create your own project template by making a new
* folder in the directory ..\JCreator\Template\. Use the other
* templates as examples.
*
*/
import java.awt.*;
import java.applet.*;
import java.sql.*;
public class HelloWorld extends Applet {
public void init() {
resize(400,600);
}
public void paint(Graphics g) {
this.setBackground(Color.lightGray); //定义背景颜色
this.setForeground(Color.blue); //定义前景颜色
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=WaterSys";
String sDBDriver="sun.jdbc.odbc.JdbcOdbcDriver";
// String url="jdbc:odbc:WaterSys";
String sql="select * from PortBaseInfor";
try //异常处理模块
{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
//Class.forName(sDBDriver);
Connection con= DriverManager.getConnection(url,"sa","dean2004");
Statement stmt=con.createStatement();
ResultSet result=stmt.executeQuery(sql); //返回结果
g.drawString("厂站编号",40,40);
g.drawString("厂站名称",80,40);
g.drawString("厂站地址",160,40);
g.drawString("警戒水位",240,40);
int i=10;
while(result.next())
{
g.drawString(result.getString(1),40,60+i);
g.drawString(result.getString(2),80,60+i);
g.drawString(result.getString(3),160,60+i);
g.drawString(result.getString(4),240,60+i);
i+=20;
}
result.close();
stmt.close();
con.close();
}
// catch(SQLException ex){}
//catch(java.lang.Exception ex){}
catch(Exception e)
{
g.drawString(e.toString(),20,20);
}
}
/**public static void main(String s[]) {
Frame f = new Frame("AppletJDBC");
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
});
Applet applet = new HelloWorld();
f.add("Center", applet);
applet.init();
f.pack();
f.setSize(new Dimension(550,250));
f.show();
}
*/
}
===================================================================================
<HTML>
<HEAD>
</HEAD>
<BODY BGCOLOR="000000">
<CENTER>
<APPLET
code = "HelloWorld.class"
width = "500"
height = "300"
>
</APPLET>
</CENTER>
</BODY>
</HTML>
=======================================================================================
错误如下:java.lang.ClassNotFoundException:com.microsoft.jdbc.sqlserver.SQLServerDriver
我是把msbase.jar, mssqlserver.jar, msutil.jar三个驱动文件放在%JAVA_HOME%/lib 下。
我在环境变量中这样设置:
@echo off
set JAVA_HOME=C:\j2sdk1.4.2_01
set PATH=.;C:\j2sdk1.4.2_01\bin
set classpath=C:\j2sdk1.4.2_01\lib\dt.jar;C:\j2sdk1.4.2_01\lib\tools.jar;C:\j2sdk1.4.2_01\lib\msbase.jar;C:\j2sdk1.4.2_01\lib\mssqlserver.jar;C:\j2sdk1.4.2_01\lib\msutil.jar
但是说找不到驱动路径啊。我在JSP中把驱动类放在Resin的lib下就可以了。可是在APPLET中一直没有解决。
很急,希望知道的大侠解决,不知道的不要乱发帖子。