在另一个java类中访问resultSet中的数据
我有一个类(SQLRequests),它连接到SQL数据库并从表中获取某些信息。它们存储在结果集中(rsUpdate和rsNew)。下面是方法,我添加了一些代码,以确保提取正确的数据在另一个java类中访问resultSet中的数据,java,sql,Java,Sql,我有一个类(SQLRequests),它连接到SQL数据库并从表中获取某些信息。它们存储在结果集中(rsUpdate和rsNew)。下面是方法,我添加了一些代码,以确保提取正确的数据 public void ProcessSQLUpdate (Connection conn) { try { Statement stmt = conn.createStatement(); String sql = SQLDataAdaptor.SELECT_PROCESS_
public void ProcessSQLUpdate (Connection conn)
{
try
{
Statement stmt = conn.createStatement();
String sql = SQLDataAdaptor.SELECT_PROCESS_SQL_UPDATE;
ResultSet rsUpdate = stmt.executeQuery(sql);
while(rsUpdate.next ())
{
System.out.println("Applix Number: " + rsUpdate.getString(2) + " " + ("Change: " + rsUpdate.getString(1)));
logger.info("Applix Number: " + rsUpdate.getString(2) + " " + ("Change: " + rsUpdate.getString(1)));
}
if(stmt!=null)
stmt.close();
if(conn!=null)
conn.close();
}
我想在另一个类(EmailSender)的电子邮件方法中发送此信息,但我无法确定如何将此信息添加到其中
public void sendEmail () throws PollingException
{
Properties props = new Properties();
PollingProperties properties = PollingProperties.getInstance();
props.put("mail.smtp.host", (properties.getProperty(PollingProperties.POL_EMAIL_SMTP)));
Date date = new Date();
try {
Session mailSession = Session.getDefaultInstance(props, null);
MimeMessage message = new MimeMessage(mailSession);
message.setSubject (properties.getProperty(PollingProperties.POL_EMAIL_SUBJECT));
message.setFrom(new InternetAddress(properties.getProperty(PollingProperties.POL_EMAIL_FROM)));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(properties.getProperty(PollingProperties.POL_EMAIL_TO)));
message.setText("Applix Update for " + date +
"\n\n New Rows: " [rsUpdate info here]+
"\n\n Updated Rows:");
Transport.send(message);
希望这是有意义的你要找的是一份工作 DTO是一种设计模式,用于减少两个层(或两个方法…)之间调用的冗余,方法是使用包含所有所需字段的对象,作为参数传递,以避免在目标方法上进行多次调用或使用大型构造函数 例如,如果您正在查询一个人的详细信息,并且希望调用一个方法来打印这些信息,那么现在您可以执行以下操作:
// Query the db and fill the resultset, then
String firstName = rs.getString("firstName");
String lastName = rs.getString("lastName");
int age = rs.getString("age");
// close the connection, the resultset etc, and then
printPersonDetail(firstName); // first call
printPersonDetail(lastName); // second call
printPersonDetail(String.valueOf(age)); // another call
还有别的地方
private static void printPersonDetail(String something){
System.out.println(something);
}
private static void printPersonDetail(PersonDTO person){
System.out.println(person.getFirstName());
System.out.println(person.getLastName());
System.out.println(person.getAge());
}
使用DTO,您可以创建一个反映您需要表示的实体的对象,在本例中为person:
public Class PersonDTO{
String firstName;
String lastName;
int age;
/* Generate Getters and Setters with your IDE,
eg. in Eclipse: "ALT + SHIFT + S" -> "Generate Getters and Setters" */
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
还有你们班的同学
// Query the db and fill the resultset, then
PersonDTO person = new PersonDTO();
person.setFirstName(rs.getString("firstName"));
person.setLastName(rs.getString("lastName"));
person.setAge(rs.getString("age"));
// close the connection, the resultset etc, and then
printPersonDetail(person); // only call: you are passing a DTO as parameter
还有别的地方
private static void printPersonDetail(String something){
System.out.println(something);
}
private static void printPersonDetail(PersonDTO person){
System.out.println(person.getFirstName());
System.out.println(person.getLastName());
System.out.println(person.getAge());
}
这是一个微不足道的例子,但我希望它能帮助你理解这个想法。使用DTO设计更大的实体,并与其他类交换它们的值;在它们中只放值,而不放任何逻辑,并确定您的方法来接收实体,而不是接收单个值 注意:完成后始终关闭resultset,在
getString()
中始终使用名称而不是索引,可能使用本世纪的某些东西,如Spring的resultset和RowMapper
您要找的是一份工作 DTO是一种设计模式,用于减少两个层(或两个方法…)之间调用的冗余,方法是使用包含所有所需字段的对象,作为参数传递,以避免在目标方法上进行多次调用或使用大型构造函数 例如,如果您正在查询一个人的详细信息,并且希望调用一个方法来打印这些信息,那么现在您可以执行以下操作:
// Query the db and fill the resultset, then
String firstName = rs.getString("firstName");
String lastName = rs.getString("lastName");
int age = rs.getString("age");
// close the connection, the resultset etc, and then
printPersonDetail(firstName); // first call
printPersonDetail(lastName); // second call
printPersonDetail(String.valueOf(age)); // another call
还有别的地方
private static void printPersonDetail(String something){
System.out.println(something);
}
private static void printPersonDetail(PersonDTO person){
System.out.println(person.getFirstName());
System.out.println(person.getLastName());
System.out.println(person.getAge());
}
使用DTO,您可以创建一个反映您需要表示的实体的对象,在本例中为person:
public Class PersonDTO{
String firstName;
String lastName;
int age;
/* Generate Getters and Setters with your IDE,
eg. in Eclipse: "ALT + SHIFT + S" -> "Generate Getters and Setters" */
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
还有你们班的同学
// Query the db and fill the resultset, then
PersonDTO person = new PersonDTO();
person.setFirstName(rs.getString("firstName"));
person.setLastName(rs.getString("lastName"));
person.setAge(rs.getString("age"));
// close the connection, the resultset etc, and then
printPersonDetail(person); // only call: you are passing a DTO as parameter
还有别的地方
private static void printPersonDetail(String something){
System.out.println(something);
}
private static void printPersonDetail(PersonDTO person){
System.out.println(person.getFirstName());
System.out.println(person.getLastName());
System.out.println(person.getAge());
}
这是一个微不足道的例子,但我希望它能帮助你理解这个想法。使用DTO设计更大的实体,并与其他类交换它们的值;在它们中只放值,而不放任何逻辑,并确定您的方法来接收实体,而不是接收单个值 注意:完成后始终关闭resultset,在
getString()
中始终使用名称而不是索引,可能使用本世纪的某些东西,如Spring的resultset和RowMapper
正确设置代码格式要发送什么?您可以将必要的数据作为参数(可能是映射)发送到相关方法!正确设置代码格式要发送什么?您可以将必要的数据作为参数(可能是映射)发送到相关方法!