如果您想知道我为什么要问这个问题,那是因为我有许多不同的方法--所有这些方法都有非常相似的代码(每个方法中的唯一不同之处是生成resultset注释之间的差异)。我试过很多次试着缩短这个时间,但是我没有想法了。我一直试图得到的是在主方法中有重复的代码(因此它不会重复),然后能够从这里调用resultSet方法。
/**
* This is the Main Method.
*
* @param args
*/
public static void main(String[] args) {
// call method resultSet1
resultSet1();
}
/**
* This Method shows Resultset1.
*/
public static void resultSet1() {
// declare my variables and assign values to them
String url = "jdbc:mysql://lamp.eeecs.qub.ac.uk/mmeaklim01";
Connection connection;
// declare my prepared statement and assign a value to it
PreparedStatement updateSalaries;
String updateSalariesString = "update Staff_Members set salary = ? where idStaff = ?";
// declare two arrays and put values in them
int[] staffSalaries = { 45000, 40000, 35000, 42000, 29000, 29500,
47000, 34000, 30500, 42000, 44500, 41000, 29500, 29500, 30000,
34000 };
String[] staffIDs = { "M001", "M002", "M003", "M004", "M005", "M006",
"M007", "M008", "M009", "M010", "M011", "M012", "M013", "M014",
"M015", "M016" };
// try catch block to handle exceptions
try {
// connect to the database
connection = DriverManager.getConnection(url, "mmeaklim01",
"xxx");
// start the update
updateSalaries = connection.prepareStatement(updateSalariesString);
// declare my variable and assign a value to it
int length = staffIDs.length;
// for loop to take the values from my two arrays and use them to
// update the Staff_Members table
for (int loop = 0; loop < length; loop++) {
updateSalaries.setInt(1, staffSalaries[loop]);
updateSalaries.setString(2, staffIDs[loop]);
updateSalaries.executeUpdate();
}
// generate resultset1
ResultSet resultSet1 = updateSalaries
.executeQuery("SELECT name, position, salary FROM Staff_Members");
// while loop
System.out.println("Resultset 1\n");
System.out.println("Name\t\tPosition\tSalary");
System.out.println("----------\t----------\t----------");
while (resultSet1.next()) {
String name = resultSet1.getString("name");
String position = resultSet1.getString("position");
int salary = resultSet1.getInt("salary");
// print out resultset1
System.out.println(name + "\t" + position + "\t" + salary
+ "\t");
} // end of resultset1
// close the update
updateSalaries.close();
// close the connection
connection.close();
// add a linebreak
System.out.println();
// output a message to the user to indicate that the program has
// executed correctly
System.out.println("The Method has executed correctly.");
} catch (SQLException sqlException) {
// catch block
System.out.println("Error: " + sqlException.getMessage());
}
}理想情况下,我想要的resultSet方法应该是:
// generate resultset1
ResultSet resultSet1 = updateSalaries
.executeQuery("SELECT name, position, salary FROM Staff_Members");
// while loop
System.out.println("Resultset 1\n");
System.out.println("Name\t\tPosition\tSalary");
System.out.println("----------\t----------\t----------");
while (resultSet1.next()) {
String name = resultSet1.getString("name");
String position = resultSet1.getString("position");
int salary = resultSet1.getInt("salary");
// print out resultset1
System.out.println(name + "\t" + position + "\t" + salary
+ "\t");
} // end of resultset1我相信可能有一个简单的方法来解决这个问题,但我似乎想不出答案。
发布于 2015-11-27 10:38:10
发布于 2015-11-27 17:37:10
当您使用在使用资源后需要关闭的资源时,您应该使用试着用资源来赶上。如果您的类实现了AutoCloseable (这是Connection的情况),那么您将自动关闭,而不会将代码与之混淆。
这里有一个硬编码连接,String url = "jdbc:mysql://lamp.eeecs.qub.ac.uk/mmeaklim01";使您的代码绑定到特定的数据库。我会尝试在启动时使用一个属性文件来解压缩。这将有助于在不创建新版本的情况下更改数据库。
注释应该用来说明代码的一些不寻常之处,而不是代码正在做的事情。当您的代码清晰且名称良好时,您可以删除像// while loop这样的注释(这是一个很好的例子,因为在有一个实际的while循环之前,该注释不是3条语句)。
对于使用System.out,我总是很谨慎,因为它不能保证您可以使用它,或者使用起来很方便。我建议您考虑使用日志框架来完成这项任务,因为设置它不需要那么多的工作,而且在项目中(即使是小的)也会非常有用。
https://codereview.stackexchange.com/questions/112005
复制相似问题