gpt4 book ai didi

Java 发布资源

转载 作者:行者123 更新时间:2023-11-30 03:37:46 25 4
gpt4 key购买 nike

我的代码有问题。在 MainFrame.java 中,我创建了一个新的 JDialog,它将 ResultSet 从 derby 数据库加载到 jTable。我已在数据库中插入 150000 行用于测试目的。将 ResultSet 中的值加载到 jTable(在 LoadTable() 方法中)会增加 java.exe 系统资源内存约 500 mb。问题是在处理 JDialog 时,所使用的 500 MB 没有被释放。仅当在 MainFrame.java 中调用 system.exit() 时,它们才会被释放。

当我重新打开 JDialog 时,内存资源增加到 800mb。第三次重新打开 JDialog 将内存增加到 1 Gb,依此类推。

以下是使用的代码:
1)调用JDialog:

   private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt)                                           
{
try
{
Database.GetConnection().commit();
}
catch (SQLException ex)
{
Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
Log.GetLogger().log(Level.ALL, "jMenuItem1ActionPerformed() Method in MainFrame.Java, Database error!", ex);
MessageBox.ShowMessage(this, ex.toString(), "ERROR", JOptionPane.ERROR_MESSAGE);
}
Insert ins = new Insert(this, true);
ins.setVisible(true);
}

2) 将值加载到 jTable 中:

    private void LoadTable() throws SQLException
{

Timestamp tstart = new Timestamp(new java.util.Date().getTime());

//get all from database
ResultSet rs;
rs = Database.GetCutiiAll();

Timestamp tstop1 = new Timestamp(new java.util.Date().getTime());

//create a model for the table
int len = Database.GetColoaneArr().length + 1;
javax.swing.table.DefaultTableModel tableModel = new DefaultTableModel(0, len);
//set table headers for columns
String[] headers = new String[Database.GetColoaneArr().length + 1];
headers[0] = "ID";
System.arraycopy(Database.GetColoaneArr(), 0, headers, 1, headers.length - 1);
tableModel.setColumnIdentifiers(headers);
//set selection mode
table1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
//insert rows to table
int rowNo = 0;
while (rs.next())
{
//create object array with values for table columns
int length = headers.length;
Object[] data = new Object[length];
for (int i = 0; i < length; i++)
{
//parse values where are foreign keys in the database
Object val = rs.getObject(headers[i]);
switch (headers[i])
{
case "SERIE":
{
String[] temp = Database.GetSerii();
int index = (int) val;

data[i] = temp[index - 1];
break;
}
case "PROTECTIE_EX_1":
case "PROTECTIE_EX_2":
case "PROTECTIE_EX_3":
{
String[] temp = Database.GetProtectii();
int index = (int) val;

data[i] = temp[index - 1];
break;
}
case "TIP_MATERIAL":
{
String[] temp = Database.GetMateriale();
int index = (int) val;

data[i] = temp[index -1];
break;
}
default:
{
data[i] = rs.getObject(headers[i]);
break;
}
}
}
tableModel.insertRow(rowNo, data);
rowNo++;
}
//attach model to table
table1.setModel(tableModel);
//create listeners to update database when changing values
Timestamp tstop2 = new Timestamp(new java.util.Date().getTime());
//show how long it took to load the database
long s = tstop2.getTime() - tstart.getTime();
jDurata.setText(String.valueOf(s) + " ms");
}

3)处理JDialog:

private void jBtnIesireActionPerformed(java.awt.event.ActionEvent evt) {                                               
// TODO add your handling code here:
if (Database.GetCanSave())
{
int answer = MessageBox.ShowConfirmationMessage(this, "Salvati modificarile inainte de a iesi?", "OPTIUNE", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
switch (answer)
{
case JOptionPane.CANCEL_OPTION: break;
case JOptionPane.YES_OPTION: {
try
{
Database.Save();
}
catch (SQLException ex)
{
Logger.getLogger(Insert.class.getName()).log(Level.SEVERE, null, ex);
Log.GetLogger().log(Level.ALL, "Database.Save() Method in Insert.Java, Failed to update database.", ex);
MessageBox.ShowMessage(this, ex.toString(), "ERROR", JOptionPane.ERROR_MESSAGE);
}
break;
}
case JOptionPane.NO_OPTION: {
try
{
Database.RollbackAllSavepoints();
}
catch (SQLException ex)
{
Logger.getLogger(Insert.class.getName()).log(Level.SEVERE, null, ex);
Log.GetLogger().log(Level.ALL, "Database.Undo() Method in Insert.Java, Failed to update database.", ex);
MessageBox.ShowMessage(this, ex.toString(), "ERROR", JOptionPane.ERROR_MESSAGE);
}
finally
{
this.dispose();
}
}
default: break;
}
}
else
this.dispose();
}

提前致谢,抱歉,我无法在此处格式化代码。

最佳答案

将 150000 条记录加载到内存中是一个非常糟糕的主意。我相信你应该有一个分页,一次只加载一小块到内存中,比如 10 条记录。如果您将策略切换为使用分页,那么您的内存问题将得到解决。

关于Java 发布资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27439451/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com