在浏览器中执行jdbc小程序
import java.sql.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="A0" width=250 height=200>
</applet>
*/
public class A0 extends Applet implements ActionListener,ItemListener
{
String msg="";
Button view,delete,create,edit,reapp,exit;
TextField M_head;
int x,i,ans=0,flag;
public void init()
{
setLayout(new FlowLayout(FlowLayout.CENTER,50,3));
view = new Button("view");
delete = new Button("delete");
create = new Button("create");
edit = new Button("edit");
reapp = new Button("reapp");
exit= new Button("exit");
M_head = new TextField(15);
add(view);
add(delete);
add(create);
System.out.println("vikram");
add(edit);
add(reapp);
add(exit);
System.out.println("phaneendra");
add(M_head);
view.addActionListener(this);
delete.addActionListener(this);
create.addActionListener(this);
edit.addActionListener(this);
reapp.addActionListener(this);
exit.addActionListener(this);
M_head.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
String str=ae.getActionCommand();
if(str.equals("view"))
{msg ="1";}
if(str.equals("delete"))
{msg ="2";}
if(str.equals("create"))
{msg ="3";}
if(str.equals("edit"))
{msg ="4";}
if(str.equals("reapp"))
{msg ="5";}
if(str.equals("exit"))
{msg ="6";}
if(msg=="3")
{
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//String filename = "E:/vikram/conn/new/db/north.mdb";
String filename = "./db/north.mdb";
String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=";
//String url ="jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=D:\\cheminDeMaBaseEtNomdeLaBdd";
database+=filename.trim();
String head = M_head.getText();
String head1 = head.trim();
Connection con = DriverManager.getConnection(database,"","");
Statement doo = con.createStatement();
//String vi ="create table head1 (Reapporder integer, Amount integer)";
String vi="insert into head1 values(1,2);";
boolean i=false;
i=doo.execute(vi);
if(i)
M_head.setText("Failed to insert");
else
M_head.setText("record inserted");
}
catch(Exception err)
{
System.out.println("Error :"+err);
}
}
}
public void itemStateChanged(ItemEvent ie)
{
repaint();
}
public void paint(Graphics g)
{
g.drawString(msg,70,200); //No use
g.drawString("ANSWER=",6,200); // No use
}
}
这是 A0.txt
grant {
permission java.lang.RuntimePermission
"accessClassInPackage.sun.jdbc.odbc";
permission java.util.PropertyPermission
"file.encoding", "read";
};
A0.html 文件
<html>
<head>
</head>
<body>
<applet code=A0 width=250 height=200></applet>
</body>
</html>
此代码在 Appletviewer 命令中执行,但不在任何浏览器中执行
import java.sql.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="A0" width=250 height=200>
</applet>
*/
public class A0 extends Applet implements ActionListener,ItemListener
{
String msg="";
Button view,delete,create,edit,reapp,exit;
TextField M_head;
int x,i,ans=0,flag;
public void init()
{
setLayout(new FlowLayout(FlowLayout.CENTER,50,3));
view = new Button("view");
delete = new Button("delete");
create = new Button("create");
edit = new Button("edit");
reapp = new Button("reapp");
exit= new Button("exit");
M_head = new TextField(15);
add(view);
add(delete);
add(create);
System.out.println("vikram");
add(edit);
add(reapp);
add(exit);
System.out.println("phaneendra");
add(M_head);
view.addActionListener(this);
delete.addActionListener(this);
create.addActionListener(this);
edit.addActionListener(this);
reapp.addActionListener(this);
exit.addActionListener(this);
M_head.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
String str=ae.getActionCommand();
if(str.equals("view"))
{msg ="1";}
if(str.equals("delete"))
{msg ="2";}
if(str.equals("create"))
{msg ="3";}
if(str.equals("edit"))
{msg ="4";}
if(str.equals("reapp"))
{msg ="5";}
if(str.equals("exit"))
{msg ="6";}
if(msg=="3")
{
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//String filename = "E:/vikram/conn/new/db/north.mdb";
String filename = "./db/north.mdb";
String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=";
//String url ="jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=D:\\cheminDeMaBaseEtNomdeLaBdd";
database+=filename.trim();
String head = M_head.getText();
String head1 = head.trim();
Connection con = DriverManager.getConnection(database,"","");
Statement doo = con.createStatement();
//String vi ="create table head1 (Reapporder integer, Amount integer)";
String vi="insert into head1 values(1,2);";
boolean i=false;
i=doo.execute(vi);
if(i)
M_head.setText("Failed to insert");
else
M_head.setText("record inserted");
}
catch(Exception err)
{
System.out.println("Error :"+err);
}
}
}
public void itemStateChanged(ItemEvent ie)
{
repaint();
}
public void paint(Graphics g)
{
g.drawString(msg,70,200); //No use
g.drawString("ANSWER=",6,200); // No use
}
}
This is A0.txt
grant {
permission java.lang.RuntimePermission
"accessClassInPackage.sun.jdbc.odbc";
permission java.util.PropertyPermission
"file.encoding", "read";
};
A0.html file
<html>
<head>
</head>
<body>
<applet code=A0 width=250 height=200></applet>
</body>
</html>
This code is executed in Appletviewer
command, but not in any browser
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如其他人评论的那样,您真的不想这样做。
只需在服务器端创建一个 Web 服务(可以是普通的 servlet)并使用
java.net.URLConnection
在小程序中。基本 Servlet 示例:
基本 Applet 示例:
但是要小心 SQL 注入。切勿将原始 SQL 查询作为请求参数或路径信息传递,并使用
PreparedStatement
始终存在于 DAO 代码中。作为响应数据格式,您可以使用普通字符串(如示例中所示)或 XML 字符串< /a> 或 JSON 字符串 甚至可能是一个完整的 Java 对象,带有一点序列化的帮助。
As commented by others, you really don't want to do this.
Just create a webservice in the server side (which can be a plain vanilla servlet) and make use of
java.net.URLConnection
in the applet.Basic Servlet example:
Basic Applet example:
Be careful with SQL injections however. Do in no way pass raw SQL queries as request parameters or pathinfo and use
PreparedStatement
all the time in the DAO code.As response data format you can use a plain vanilla String (as given in example) or a XML string or a JSON string or maybe even a fullworthy Java object with a little help of Serialization.
出于安全原因,您不能在 Applet 上执行 JDBC。
您必须编写企业应用程序(使用 Java、.NET、Python、PHP)并将其部署到应用程序服务器。在该应用程序中,您可以发布一些 Web 服务,以便您的 Applet 最终可以访问您的数据库。
像这样的东西:
APPLET <->应用程序服务器(HTTP 通信)<->后端(数据库)
这里是一个解释一些安全相关Applet的网站东西。
You can't do JDBC on an Applet for security reasons.
You must write an Enterprise Application (in Java, .NET, Python, PHP) and deploy it to an application server. In that application you can publish some WebServices so your Applet can finally access your database.
Something like this:
APPLET <-> APPLICATION SERVER (HTTP communication) <-> BACKEND (database)
Here is a Web Site explaining some security related Applet stuff.