开发者

How to make an applet connect with an MS Access database?

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 开发者_Python百科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.


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.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜