如何使小程序与 MS Access 数据库连接?

发布于 2024-09-30 05:17:17 字数 527 浏览 1 评论 0原文

编写一个小程序来显示 所描述程序的接口 以下。当小程序执行时 将在屏幕上显示 适当的布局,并响应 用户的操作。

该程序模拟一个学生 管理系统具有以下内容 特点:

界面很吸引人,非常用户化 友好、直观(即充当 有人会期望它起作用),并且 相当现实。它必须接受 学生号、姓名、年龄、地址、日期 出生、性别、血型等 用户并将其保存在 MS Access 中 数据库。 + 电子邮件 ID、电话号码、级别。

该界面使用命令按钮来 (i) 添加、编辑、删除、更新和取消 记录,(ii) 导航 向前或向后记录 (iii) 直接移至第一条记录或最后一条记录 记录。记录数 输入的内容应使用显示 当用户按下 a 时报告 “报告”按钮。

最初使所有字段不可见 或将其灰显。

在界面中适当使用 至少一组“单选按钮”和 至少一个“下拉列表”。制作 布局的适当使用 经理。

Write an applet which displays the
interface for the program described
below. When the applet is executed it
will display the screen in the
appropriate layout, and responds to
the user's actions.

The program simulates a student
management system having the following
characteristics:

The interface is attractive, very user
friendly, intuitive (i.e. acts as
someone would expect it to act), and
reasonably realistic. It must accept
the student id,name,age,address,date
of birth,gender,blood group etc from
the user and save it in MS Access
database. + email Id,phone no.,level.

The interface uses command buttons to
(i) add,edit,delete,update and cancel
the records, (ii) to navigate the
records forward or backward (iii) to
move directly to first record or last
record. The number of records
entered should be displayed using a
report when the user presses a
"report" button.

Initially make all fields not visible
or gray it out.

In the interface appropriately use at
least one set of "radio buttons" and
at least one "drop-down list". Make
appropriate use of the Layout
managers.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迷离° 2024-10-07 05:17:17

您可以通过将 MS-Access 数据库添加到 ODBC 源来访问它。

要打开数据源 (ODBC),请单击“开始”,单击“控制面板”,然后单击“性能和维护”。单击“管理工具”,然后双击“数据源 (ODBC)”。

以下是一些您可以熟悉的示例代码。看一下 java.sql.*< /a> 相关的类和方法——您将在那里找到几乎所有与数据库交互的答案。是的,这里与视觉摆动类存在一些可怕的耦合(您应该抛出一个错误,而不是仅仅显示一个错误并强制系统退出)。另外,我认为 JdbcOdbcDriver 的使用也已被弃用。

import javax.swing.*;
import sun.jdbc.odbc.JdbcOdbcDriver;
import java.sql.*;
import java.util.*;

public class SalesDB
{
    private static Connection connect;

    /**
     * Connects to the database. This method must be run before any of the other methods since
     * this method enables the connection to the database.
     */
    public static void connect()
    {

                //The 'a5q3db' is the name of the ODBC source that you added.
        String url = "jdbc:odbc:a5q3db";

        try
        {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            connect = DriverManager.getConnection(url);

        }
        catch (ClassNotFoundException ex)
        {
            JOptionPane.showMessageDialog(null, "Failed to load JDBC/ODBC driver. Program will now terminate.");
            System.exit(-1);
        }
        catch (SQLException ex)
        {
            JOptionPane.showMessageDialog(null, "Failed to connect to the database. Program will now terminate.");
            System.exit(-1);
        }

    }

    /**
     * close the database connection before the program terminates.
     */
    public static void close()
    {
        try
        {
            connect.close();
        }
        catch (SQLException ex)
        {


        }

    }

    /**
     * Runs the supplied string as a query to the database and returns the result set.
     * @param query The query with which to execute to the database.
     * @return The generated resultset from java.sql.Statement#executeQuery(String).
     */
    public static ResultSet runQuery (String query)
    {
        ResultSet result;

        try
        {

            Statement statement = connect.createStatement();
            result = statement.executeQuery(query);

        }
        catch (SQLException ex)
        {
            return null;
        }

        return result;
    }

}//End SalesDB class.

You can access the MS-Access database by adding it to your ODBC sources.

To open Data Sources (ODBC), click Start, click Control Panel, and then click Performance and Maintenance. Click Administrative Tools, and then double-click Data Sources (ODBC).

Here is some example code that you can familiarize yourself with. Take a look at the java.sql.* related classes and methods -- you'll find almost all of your answers in there for interacting with the database. Yes, there is some hideous coupling with visual swing classes going on here (you should throw an error instead of just displaying one and forcing a system exit). Also, I think the use of the JdbcOdbcDriver is deprecated as well.

import javax.swing.*;
import sun.jdbc.odbc.JdbcOdbcDriver;
import java.sql.*;
import java.util.*;

public class SalesDB
{
    private static Connection connect;

    /**
     * Connects to the database. This method must be run before any of the other methods since
     * this method enables the connection to the database.
     */
    public static void connect()
    {

                //The 'a5q3db' is the name of the ODBC source that you added.
        String url = "jdbc:odbc:a5q3db";

        try
        {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            connect = DriverManager.getConnection(url);

        }
        catch (ClassNotFoundException ex)
        {
            JOptionPane.showMessageDialog(null, "Failed to load JDBC/ODBC driver. Program will now terminate.");
            System.exit(-1);
        }
        catch (SQLException ex)
        {
            JOptionPane.showMessageDialog(null, "Failed to connect to the database. Program will now terminate.");
            System.exit(-1);
        }

    }

    /**
     * close the database connection before the program terminates.
     */
    public static void close()
    {
        try
        {
            connect.close();
        }
        catch (SQLException ex)
        {


        }

    }

    /**
     * Runs the supplied string as a query to the database and returns the result set.
     * @param query The query with which to execute to the database.
     * @return The generated resultset from java.sql.Statement#executeQuery(String).
     */
    public static ResultSet runQuery (String query)
    {
        ResultSet result;

        try
        {

            Statement statement = connect.createStatement();
            result = statement.executeQuery(query);

        }
        catch (SQLException ex)
        {
            return null;
        }

        return result;
    }

}//End SalesDB class.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文