我正在开发一个使用JDBC和Java Mail的java项目。我正在尝试将运行我的SQL语句的输出发送到我的电子邮件。
问题是msg.setText(),这是将文本插入电子邮件正文的方法,是静态的。所以,我不能把我在那里执行SQL语句的任何方法作为参数,因为我不能对非静态方法进行静态引用。
如何将输出(控制台中的结果)发送到电子邮件地址?
控制台的预期输出是:
ALLDONE: 0
COUNT(1): 0
IN_AC_IND: 0
Billing Count(1): 0
Signon: 0
因此,在电子邮件的正文中将是整个邮件。
The code:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class test {
Connection conn;
public static void main (String[] args) {
new test();
email();
} // end of main class
public static void email(){
final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
// Get a Properties object
Properties props = System.getProperties();
props.setProperty("mail.smtp.host", "smtp.gmail.com");
props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
props.setProperty("mail.smtp.socketFactory.fallback", "false");
props.setProperty("mail.smtp.port", "587");
props.setProperty("mail.smtp.socketFactory.port", "587");
props.put("mail.smtp.auth", "true");
props.put("mail.debug", "true");
props.put("mail.store.protocol", "pop3");
props.put("mail.transport.protocol", "smtp");
final String username = "user";//
final String password = "pass";
try{
Session session = Session.getDefaultInstance(props,
new Authenticator(){
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}});
// -- Create a new message --
Message msg = new MimeMessage(session);
// -- Set the FROM and TO fields --
msg.setFrom(new InternetAddress("email"));
msg.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("email",false));
msg.setSubject("Daily Activity Report");
msg.setText("This is a test email ");
msg.setSentDate(new Date());
Transport.send(msg);
System.out.println("Message sent.");
}catch (MessagingException e){ System.out.println("Error: " + e);}
}
public test() {
try
{
Class.forName("value");
conn = DriverManager.getConnection("value", "value" , "value");
execute();
conn.close();
}catch (ClassNotFoundException ex) {System.err.println(ex.getMessage());
}catch (SQLException ex) {System.err.println(ex.getMessage());}
}
public void execute() {
method();
// someOtherMethod();
method2();
method3();
}
public void method(){
String query = "SELECT ALLDONE FROM table";
try{
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery(query);
while (rs.next())
{
String s = rs.getString ("ALLDONE");
System.out.println("ALLDONE: " + s);
}
}catch (SQLException ex){
System.err.println(ex.getMessage());
}
答案 0 :(得分:0)
你需要让“method”返回一个String。这是它的重写版本:
public String method()
{
String query = "SELECT ALLDONE FROM table";
StringBuilder retval = new StringBuilder("");
try
{
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery(query);
while (rs.next())
{
String s = rs.getString("ALLDONE");
retval.append(s + "\n");
System.out.println("ALLDONE: " + s);
}
}
catch (SQLException ex)
{
System.err.println(ex.getMessage());
}
return retval.toString();
}
请注意,除了使用System.out.println将结果打印到控制台之外,我们还将它们添加到StringBuilder中,然后将它们作为String返回。