简体   繁体   English

为Applet创建SQL数据库

[英]Creating SQL database for Applet

I am having a simple Applet running on a Eclipse. 我在Eclipse上运行了一个简单的Applet。 My requirement is to store some values(random id, name, account number) in SQL database from this applet. 我的要求是从此小程序在SQL数据库中存储一些值(随机ID,名称,帐号)。 Later, I also need access this DB from a servlet, which is running on the same setup. 稍后,我还需要从运行于相同设置的servlet访问该数据库。

I am new to this and couldn't get any link for this development. 我对此并不陌生,因此无法获得此开发的任何链接。 Could someone give/guide me steps to create MySQL DB specific for my requirement and access from Applet(running on eclipse) and store values in it? 有人可以给我/指导我一些步骤来创建特定于我的需求的MySQL数据库并从Applet(在Eclipse上运行)访问并在其中存储值吗?

Kindly refer below link. 请参考以下链接。

I'm sure you'll get your answer. 我相信你会得到答案的。

http://publib.boulder.ibm.com/infocenter/db2luw/v8/index.jsp?topic=/com.ibm.db2.udb.doc/ad/samples/jdbc/s-Applt-java.htm http://publib.boulder.ibm.com/infocenter/db2luw/v8/index.jsp?topic=/com.ibm.db2.udb.doc/ad/samples/jdbc/s-Applt-java.htm

public Connection getConnection() throws SQLException {

    Connection conn = null;
    Properties connectionProps = new Properties();
    connectionProps.put("user", this.userName);
    connectionProps.put("password", this.password);

    if (this.dbms.equals("mysql")) {
        conn = DriverManager.getConnection(
                   "jdbc:" + this.dbms + "://" +
                   this.serverName +
                   ":" + this.portNumber + "/",
                   connectionProps);
    } else if (this.dbms.equals("derby")) {
        conn = DriverManager.getConnection(
                   "jdbc:" + this.dbms + ":" +
                   this.dbName +
                   ";create=true",
                   connectionProps);
    }
    System.out.println("Connected to database");
    return conn;
}

I'd recommend that you break the problem into pieces. 我建议您将问题分解为几部分。

Start with the database interface. 从数据库界面开始。 Write a Java interface that describes all the methods you want to execute. 编写描述您要执行的所有方法的Java接口。 SQL databases usually let you Create, Read, Update, and/or Delete (CRUD). SQL数据库通常允许您创建,读取,更新和/或删除(CRUD)。

package persistence;

public interface DataAccess<K, V> {
   V find(K key);
   List<V> find();
   K save(V value);
   int update(V value);
   int delete(V value);
};

Implement this interface with a Java POJO. 用Java POJO实现此接口。 Test it thoroughly and put it aside. 对其进行彻底测试并将其放在一旁。

Give an instance of that DAO to your servlet so it can access the database. 将那个DAO的实例提供给您的servlet,以便它可以访问数据库。 Now all the servlet needs to do is listen for HTTP requests, bind and validate the parameters, figure out where to send the request, and package up the response. 现在,该servlet所需要做的就是侦听HTTP请求,绑定和验证参数,弄清楚将请求发送到何处以及打包响应。 The database is already sorted. 数据库已经排序。

Last step is the applet. 最后一步是小程序。 Make it talk to your servlet by making requests to the URLs that the servlet expects and pass the HTTP parameters. 通过向Servlet期望的URL发出请求并传递HTTP参数,使其与Servlet对话。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM