JAVA:如何在对数据库进行更改后更新/刷新表

时间:2017-04-03 13:43:56

标签: java jdbc jtable h2

我是java和DBMS的新手。当用户单击相同的按钮以显示JTable并将数据写入数据库(单击写入按钮时)后,在对H2数据库进行任何更改后,我尝试更新/刷新JTable。我尝试了一些方法并阅读了一些帖子,但无法找到对我的程序至关重要的任何内容。下面的代码说明了我的问题。

这是用于从数据库读取并在JTable上显示它的方法

_public bool CreateOutlookCalendarEvent(EventContent objEvent) {
        try
        {
            Application olApp = (_Application)new Application();
            NameSpace mapiNS = olApp.GetNamespace("MAPI");
            string profile = "";
            mapiNS.Logon(profile, null, null, null);
           _AppointmentItem apt = (_AppointmentItem)
                    olApp.CreateItem(OlItemType.olAppointmentItem);
            // set some properties
            apt.Subject = objEvent.EventName;
            apt.Body = objEvent.EventDescription;

            apt.Start = objEvent.EventStartDate;
            apt.End = objEvent.EventEndDate;
            apt.ReminderMinutesBeforeStart = 15;        // One week
            apt.BusyStatus = OlBusyStatus.olTentative;  // Makes it appear bold in the calendar - which I like!
            apt.AllDayEvent = false;
            apt.Location = objEvent.EventAddress;


            bool res = apt.ForceUpdateToAllAttendees;
            apt.Save();
            objEvent.EntryID = Convert.ToInt32(apt.EntryID);

            // Prepare the parameters
            string customTableClassName = "customtable.OutlookCalendar";

            // Check if Custom table 'Sample table' exists
            DataClassInfo customTable = DataClassInfoProvider.GetDataClassInfo(customTableClassName);
            if (customTable != null)
            {
                // Create new custom table item 
                CustomTableItem newCustomTableItem = CustomTableItem.New(customTableClassName);

                // Set the ItemText field value
                newCustomTableItem.SetValue("EventID", objEvent.EventID);
                newCustomTableItem.SetValue("EventName", objEvent.EventName);

                newCustomTableItem.SetValue("EventDescription", objEvent.EventDescription);
                newCustomTableItem.SetValue("EventStartDate", objEvent.EventStartDate);
                newCustomTableItem.SetValue("EventEndDate", objEvent.EventEndDate);
                newCustomTableItem.SetValue("EventAddress", objEvent.EventAddress);

                newCustomTableItem.SetValue("Recivier", objEvent.Recivier);
                newCustomTableItem.SetValue("EntryID", "New text");
                // Insert the custom table item into database
                newCustomTableItem.Insert();

                return true;
            }

            return false;

        }

这是我用来将数据写入数据库的方法

public void readActiveData() throws IOException, InstantiationException, IllegalAccessException, SQLException {       

    try {
        st = conn.createStatement();
    } catch (SQLException sqlex) {
        JOptionPane.showMessageDialog(null, "Can't connect to DB. " + sqlex);
        dispose();
    }

    try {
        rs = st.executeQuery("SELECT * FROM main_data WHERE expirationDate > NOW() + 1;");
        rs.beforeFirst();

        while (rs.next()) {
            int id = rs.getInt(1);
            String ovogNer = rs.getString(2);
            String regNum = rs.getString(3);
            String itemName = rs.getString(4);
            String note = rs.getString(5);
            int zHemjee = rs.getInt(6);
            int hvv = rs.getInt(7);
            int hugatsaa = rs.getInt(8);
            today = rs.getDate(9);
            int totalPay = rs.getInt(10);
            expirationDate = rs.getDate(11);
            rows++;
        }
        regData = new Object[rows][11];

        Object[] Colheads = {"Бүртгэлийн дугаар", "Овог нэр", "Регистрийн дугаар", "Барьцаа",
             "Тэмдэглэл", "Зээлийн хэмжээ", "Хүү (%)", "Хугацаа", "Огноо", "Нийт төлөх", "Дуусах хугацаа"};

        rs = st.executeQuery("SELECT * FROM main_data WHERE expirationDate >   NOW() + 1;");

        for (int i1 = 0; i1 < rows; i1++) {
            rs.next();

            for (int j1 = 0; j1 < 11; j1++) {
                regData[i1][j1] = rs.getString(j1 + 1);
            }
        }
        model = new DefaultTableModel(regData, Colheads);
        table = new JTable(model);
        int v = ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
        int h = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
        JScrollPane jsp = new JScrollPane(table, v, h);
        activeDataPanel.add(jsp);
        rs.close();
        st.close();
        conn.close();
    }

1 个答案:

答案 0 :(得分:0)

这个问题已经回答here

但它归结为清除和重新呈现JTable对象。即刷新,上面的帖子解释得非常好。

希望你做对了。