簡體   English   中英

在瀏覽器中執行jdbc applet

[英]Execute jdbc applet in browser

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命令中執行,但在任何瀏覽器中Appletviewer執行

正如其他人所評論的那樣,您真的不想這樣做。

只需在服務器端創建一個Web服務(可以是普通的servlet),然后在applet中使用java.net.URLConnection

基本Servlet示例:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String action = request.getParameter("action"); // Or request.getPathInfo() whatever you want.
    String result = someDAO.doAction(action);
    response.getWriter().write(result);
}

小程序基本示例:

URL url = new URL("http://example.com/databaseservlet?action=someaction");
URLConnection connection = url.openConnection();
InputStream result = connection.getInputStream(); // Important. This actually fires the request! 

但是要小心SQL注入 絕對不要將原始SQL查詢作為請求參數或pathinfo傳遞,並始終在DAO代碼中使用PreparedStatement

作為響應數據格式,您可以使用純凈的香草字符串(如示例中所示)或XML字符串JSON字符串 ,甚至可以使用完全有價值的Java對象,而無需序列化

出於安全原因,您不能在Applet上執行JDBC。

您必須編寫一個企業應用程序(使用Java,.NET,Python,PHP)並將其部署到應用程序服務器。 在該應用程序中,您可以發布一些Web服務,以便您的Applet最終可以訪問數據庫。

像這樣:

APPLET <->應用程序服務器(HTTP通信)<-> BACKEND(數據庫)

是一個解釋一些與安全性有關的Applet內容的網站。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM