0

我仍然是java的菜鸟,只知道基础知识,我有一个小问题,希望你能帮助我。

我下一段代码在我正在制作的项目中使用了 5 次,为了保持代码简单,我想让它成为一种方法。

在这段代码中我唯一需要更改的是查询,它是唯一的变量。

提前致谢。(对不起,我的英语不好)。

Vector columnNames = new Vector();
Vector data = new Vector();
JPanel panel = new JPanel(); //
try {
  Conectar();
  String query = "Select * from Dados";
   stm = (Statement) con.createStatement();
   rs = stm.executeQuery(query);
   ResultSetMetaData metaData = rs.getMetaData();
   int columns = metaData.getColumnCount();
   for (int i = 1; i <= columns; i++) {
   columnNames.addElement(metaData.getColumnName(i));
}
while (rs.next()) {
   Vector row = new Vector(columns);
   for (int i = 1; i <= columns; i++) {
   row.addElement(rs.getObject(i));
}
 data.addElement(row);
}
 rs.close();
 stm.close();
} catch (Exception e) {
 System.out.println(e);
}
JTable table = new JTable(data, columnNames);
TableColumn column;
for (int i = 0; i < table.getColumnCount(); i++) {
column = table.getColumnModel().getColumn(i);
column.setMaxWidth(250);
}
table.setPreferredScrollableViewportSize(new Dimension(600, 400));
table.setEnabled(false);
JScrollPane scrollPane = new JScrollPane(table); panel.add(scrollPane); 
JFrame frame = new JFrame();
frame.add(panel); //adiciona o panel à frame
frame.setResizable(false);
frame.setSize(640, 480); //define o tamanho da frame
frame.setLocationRelativeTo(null);
frame.setVisible(true); //torna a frame visivel
} 
4

3 回答 3

1

只需将您的代码与

public void goodDescriptiveName(String query) {
    ...
}

并删除该行

String query = "Select * from Dados";

从你的代码。然后你就这样称呼它:

goodDescriptiveName("Select * from Dados");
goodDescriptiveName("Select * from Next");
...
于 2012-07-13T09:09:27.000 回答
0
public boolean someAction(String query) {
...
rs = stm.executeQuery(query);
...
return true;
}

如果遇到任何错误,请返回false. 如果您不想用 true/false 测试它,只需使用void而不是boolean.

于 2012-07-13T09:08:50.603 回答
0
public void methodName(String query) {
    Vector columnNames = new Vector();
    Vector data = new Vector();
    JPanel panel = new JPanel(); //
    try {
      Conectar();
       stm = (Statement) con.createStatement();
       rs = stm.executeQuery(query);
       ResultSetMetaData metaData = rs.getMetaData();
       int columns = metaData.getColumnCount();
       for (int i = 1; i <= columns; i++) {
       columnNames.addElement(metaData.getColumnName(i));
    }
    while (rs.next()) {
       Vector row = new Vector(columns);
       for (int i = 1; i <= columns; i++) {
       row.addElement(rs.getObject(i));
    }
     data.addElement(row);
    }
     rs.close();
     stm.close();
    } catch (Exception e) {
     System.out.println(e);
    }
    JTable table = new JTable(data, columnNames);
    TableColumn column;
    for (int i = 0; i < table.getColumnCount(); i++) {
    column = table.getColumnModel().getColumn(i);
    column.setMaxWidth(250);
    }
    table.setPreferredScrollableViewportSize(new Dimension(600, 400));
    table.setEnabled(false);
    JScrollPane scrollPane = new JScrollPane(table); panel.add(scrollPane); 
    JFrame frame = new JFrame();
    frame.add(panel); //adiciona o panel à frame
    frame.setResizable(false);
    frame.setSize(640, 480); //define o tamanho da frame
    frame.setLocationRelativeTo(null);
    frame.setVisible(true); //torna a frame visivel
    } 
}

但我认为应该在其他地方提取 GUI 代码。:)

于 2012-07-13T09:12:42.907 回答