这是一种将作业从SQlite
显示在表上并将它们适当地设置为labelText
的方法。
private void setLabelText() {
try {
String table_click0 = (table_job.getModel().getValueAt(row, 0).toString());
String sqlSt = "SELECT Employer.name, * FROM Job INNER JOIN Employer ON Job.employerID = Employer.employerID WHERE jobID='"+table_click0+"' ";
conn = JavaConnect.ConnectDB();
pst = conn.prepareStatement(sqlSt);
rs = pst.executeQuery();
if(rs.next()) {
descriptionArea.setText(rs.getString(5));
empTitLabel.setText(rs.getString(1)+" - "+rs.getString(4));
idLabel.setText("Job Reference: " + rs.getString(2));
typeLabel.setText("Job Type: " + rs.getString(6));
salaryLabel.setText("Salary: " + rs.getString(7));
benefitsLabel.setText("Benefits : " + rs.getString(8));
vacLabel.setText("Vacancies : " + rs.getString(9));
closeLabel.setText("Closing Date: " + rs.getString(10));
reqLabel.setText("Requirement : " + rs.getString(11));
placeLabel.setText("Placement : " + rs.getString(12));
applyToLabel.setText("Apply To: " + rs.getString(13));
statusLabel.setText("Job Status: "+rs.getString(14));
locLabel.setText("Location: "+rs.getString(16));
postedLabel.setText("Posted: "+rs.getString(15));
}
}
我现在希望能够允许用户选择作业(表格上单击的行)以允许编辑数据更新。因此,我提供了一种执行此操作的表单,而不是为Form文本字段重复诸如
descriptionArea.setText(rs.getString(5));
之类的行,而是一种更短的方法。还是每个JTextfield
必须使用textfieldName.setText(....)方式分别进行操作,是否有任何技巧可以使它更短?任何好的简单技术,也许都可以重用以上代码以最大程度地减少重复。 最佳答案
您可能想看一下Apache Commons DbUtils
,它使用Class Literals as Runtime-Type Tokens和ResultSetMetaData
简化了JDBC中一些较繁琐的部分。
关于java - 避免在程序周围重复JTextField setText,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14225791/