我想将编码结果插入数据库,我该怎么做? 我有一个使用 java mail 来获取电子邮件的编码,代码运行成功,但后来我想将它(电子邮件)存储到我的数据库中。
这是我的代码:
package ta_aing;
import com.mysql.jdbc.PreparedStatement;
import java.util.*;
import javax.mail.*;
import java.sql.Connection;
import java.sql.Statement;
import ta_aing.connection;
public class baca_email {
public static void main(String[] args) {
Properties props = new Properties();
connection datMan = new connection();
props.setProperty("mail.store.protocol", "imaps");
try {
Session session = Session.getInstance(props, null);
Store store = session.getStore();
store.connect("imap.gmail.com", "email", "password");
Folder inbox = store.getFolder("INBOX");
inbox.open(Folder.READ_ONLY);
Message msg = inbox.getMessage(inbox.getMessageCount());
Address[] in = msg.getFrom();
for (Address address : in) {
System.out.println("FROM:" + address.toString());
}
Multipart mp = (Multipart) msg.getContent();
BodyPart bp = mp.getBodyPart(0);
System.out.println("SENT DATE:" + msg.getSentDate());
System.out.println("SUBJECT:" + msg.getSubject());
System.out.println("CONTENT:" + bp.getContent());
String sql = "insert into email (sender, sent_date, subject, content) values (address.toString, msg.getSentDate,msg.getSubject,bp.getContent)";
Connection conn;
conn = datMan.logOn();
PreparedStatement statement;
} catch (Exception mex) {
mex.printStackTrace();
}
}
}
最佳答案
不要在查询字符串中设置属性,请使用 ?对于准备好的语句,JDBC 会池化准备好的语句并更改 ? 的值。即时运行以节省空间。将其放入查询中就达不到目的。
String sql = "insert into email (sender, sent_date, subject, content) values (" +
"?, ?,?,?)";
PreparedStatement statement = con.prepareStatement(updateString);
statement.setString(1,address.toString());
statement.setString(2,msg.toString());
statement.setString(3,msg.getSubject());
statement.setString(4,bp.getContent());
statement.executeQuery();
关于java - 将执行插入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23005468/