applet连接SQL SERVER失败

rocketliu8899 2003-11-25 10:02:29
各位大虾,我写了一个APPLET来连接本机的SQL SERVER2000,代码如下:
编译可以通过,但用APPLETVIEWER看时,总提示:Caused by: java.io.FileNotFoundException: D:\Inetpub\wwwroot\Sun\jdbc\odbc\JdbcO
dbcDriver.class (系统找不到指定的路径。),不知道是和原因,

我的环境变量配置为:
JAVA_HOME="D:\JDK1.4.2";
PATH=%JAVA_HOME%\BIN;
CLASSPATH=".;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar;%JAVA_HOME%\lib

import java.awt.*;
import java.applet.*;
import java .sql.*;
public class Applet1 extends Applet
{
TextArea theVisits=new TextArea (30,120);
//显示数据库的内容
TextField theStatus=new TextField ("",80);
//显示打开数据库的信息。相当于程序的状态栏
Connection theConnection; //数据库的连接方法
Statement theStatement; //代表一个发送到数据库来执行的数据库命令
ResultSet theResult; //读取的数据结果,也就是数据库返回的结果
ResultSetMetaData theMetaData;
//包含了有关数据库命令执行后 返回结果的有用信息。
String theDataSource; //包含了被访问数据库或者数据源的名称,用URL形式表示 .
String theUser; //数据库的用户名
String thePassword; //数据库的密码

public void init()
{
add(theVisits);
add(theStatus);
theVisits.setEditable (false); //设置文本区域不可以被用户写入
theStatus.setEditable (false); //设置文本区域不可以被用户写入
openConnection(); //打开数据库的连接
execSQLCommand("select * from score"); //从数据库中读取内容
closeConnection(); //关闭已经打开的数据库
}

public void openConnection()
{
theDataSource="jdbc:odbc:rocket1";
theUser="rocket";
thePassword="rocket";
try
{
Class.forName("Sun.jdbc.odbc.JdbcOdbcDriver");
theConnection=DriverManager.getConnection(theDataSource,theUser,thePassword);
theStatus.setText("Status:OK");
}
catch (Exception e)
{
handleException(e);
}
}


public void execSQLCommand(String command)
{
try{
theStatement=theConnection.createStatement();
theResult=theStatement.executeQuery (command);

theMetaData=theResult.getMetaData ();
int columnCount=theMetaData.getColumnCount ();
theVisits.setText("");
while(theResult.next ())
{
for(int i =1;i<=columnCount;i++)
{
String colValue=theResult.getString(i);
if(colValue==null)colValue="";
theVisits.append (colValue+";");
}
theVisits.append ("\n");
}
}catch(Exception e)
{
handleException(e);
}
}


public void closeConnection()
{
try{
theConnection.close ();
}
catch(Exception e)
{
handleException(e);
}
}


public void handleException(Exception e)
{
theStatus.setText("Error1:"+e.getMessage ());
e.printStackTrace ();
if(e instanceof SQLException)
{
while((e=((SQLException)e).getNextException ())!=null)
{
System.out.println(e);
}
}
}
}
...全文
31 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
jaddy 2003-11-26
  • 打赏
  • 举报
回复
大家看看我的一个程序把
APPLET程序在本地读写文件/执行数据库操作无本地授权,无须安装证书,数据库使用的是MS SQL SERVER 2000
演示地址为
http://www.kaowang.com/pub/ice/ice.html
准考证号为
240199990001
开始朋友测试的时候没有修改本地的授权文件,居然可以很好的运行,后来又过了一段时间又发现,居然连证书都不装了,居然还可以正常运行。
满天飞舞rose 2003-11-26
  • 打赏
  • 举报
回复
看看有多麻烦,我给你的两个建议为什么不采纳呢

如果用RMI,你的程序都不用改
hlding 2003-11-26
  • 打赏
  • 举报
回复
将驱动程序放入你的环境变量中就好了
hlding 2003-11-26
  • 打赏
  • 举报
回复
SQL Server驱动程序没有加载
pxboy 2003-11-26
  • 打赏
  • 举报
回复
用数字签名吧,搜一下
wobelisk 2003-11-26
  • 打赏
  • 举报
回复
add this line to the policy file:

permission java.net.SocketPermission "*", "connect, listen";
wobelisk 2003-11-25
  • 打赏
  • 举报
回复
save the following (between the two lines) to a file (test.policy)
------------------------------------------------
grant {
permission java.lang.RuntimePermission "accessClassInPackage.sun.jdbc.odbc";
permission java.util.PropertyPermission "file.encoding", "read";
};
--------------------------------------------------

appletviewer -J-Djava.security.policy=test.policy applet.html
满天飞舞rose 2003-11-25
  • 打赏
  • 举报
回复
用APPLET直接连的方式是不行的
我建议你用几种方法

一种是用APPLET和SERVERLET通讯读写和操作数据库。

还有一种是我经常用的,用RMI远程调用的方式。

rocketliu8899 2003-11-25
  • 打赏
  • 举报
回复
谢谢了,是我错把sun写成Sun,

但现在,好象是没有权限,我该怎样设定呢?
提示如下:
java.security.AccessControlException: access denied (java.lang.RuntimePermission
accessClassInPackage.sun.jdbc.odbc)
at java.security.AccessControlContext.checkPermission(AccessControlConte
xt.java:269)
at java.security.AccessController.checkPermission(AccessController.java:
401)
at java.lang.SecurityManager.checkPermission(SecurityManager.java:524)
at java.lang.SecurityManager.checkPackageAccess(SecurityManager.java:149
1)
at sun.applet.AppletSecurity.checkPackageAccess(AppletSecurity.java:190)
wobelisk 2003-11-25
  • 打赏
  • 举报
回复

sun.jdbc.odbc.JdbcOdbcDriver //sun Not Sun
lovelx 2003-11-25
  • 打赏
  • 举报
回复
不是程序的问题
是你安装时少了文件吧
它提示你是少了一个驱动
rocketliu8899 2003-11-25
  • 打赏
  • 举报
回复
to:wobelisk()

用这种方法是可以在appletviewer中通过,但在IE中就没有办法运行啊
满天飞舞rose 2003-11-25
  • 打赏
  • 举报
回复
楼上的,你这样的方法好象不能用在客户端的用户吧

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧