执行浏览器中的小应用程序的jdbc [英] Execute jdbc applet in browser

查看:121
本文介绍了执行浏览器中的小应用程序的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>

这code在执行的appletviewer 命令,但不能在任何浏览器

This code is executed in Appletviewer command, but not in any browser

推荐答案

作为评论别人,你真的不希望这样做。

As commented by others, you really don't want to do this.

刚刚创建的服务器端web服务(它可以是一个普通的servlet的),并使用的 java.net.URLConnection中的 中的applet。

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.

基本的servlet例如:

Basic Servlet example:

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);
}

基本Applet的例子:

Basic Applet example:

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和使用的 preparedStatement 在DAO code所有的时间。

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.

由于响应数据格式,你可以使用一个普通的字符串(如实例)或一张 XML字符串 JSON字符串或者甚至一个fullworthy的Java与系列化的一点点帮助对象。

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.

这篇关于执行浏览器中的小应用程序的jdbc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆