我正在尝试创建一个电子邮件管理系统。以下是撰写电子邮件的代码,但是它会在启动ResultSet之前抛出错误。"你能帮我吗?
String To = to.getText();
String Cc = cc.getText();
String Bcc = bcc.getText();
String Sub = sub.getText();
String Msg = text.getText();
if(To.equals("") || Sub.equals("") || Msg.equals(""))
{
JOptionPane.showMessageDialog(null, "Please Fill All Appropiate Details");
}
else
{
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection( "jdbc:mysql://localhost:3306/email management system","root", "ok" );
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select first,email,mail,name from login,curlogin;");
String from =rs.getString("mail");
String frmname =rs.getString("name");
String recipent = "";
String rname = "";
Boolean tof;
loop1: while(rs.next())
{
recipent = rs.getString("email");
rname = rs.getString("first");
if(recipent.equals(To))
{
tof = true;
break loop1;
}
else
{
tof= false;
}
}
if(tof=true)
{
stmt.executeUpdate("Insert into inbox Values('"+frmname+"','"+Sub+"','"+Msg+"',curDate(), curTime(),'"+rname+"','"+from+"');");
new inbox().setVisible(true);
this.dispose();
}
else
{
JOptionPane.showMessageDialog(null,"Sorry the 'To' Email you entered does not exist.");
}
ResultSet rs1 = stmt.executeQuery("select first,email,mail,name from login,curlogin;");
Boolean taf = false;
Boolean entered = false;
loop2: while(rs1.next())
{
recipent = rs1.getString("email");
rname = rs1.getString("first");
if(Cc.equals(""))
{
}
else
{
entered= true;
if(recipent.equals(Cc))
{
taf = true;
break loop2;
}
else
{
taf= false;
}
}
}
if(entered=true)
{
if(taf=true)
{
stmt.executeUpdate("Insert into inbox Values('"+frmname+"','"+Sub+"','"+Msg+"',curDate(), curTime(),'"+rname+"','"+from+"');");
new inbox().setVisible(true);
this.dispose();
}
else
{
JOptionPane.showMessageDialog(null,"Sorry the 'CC' Email you entered does not exist.");
}
}
ResultSet rs2 = stmt.executeQuery("select first,email,mail,name from login,curlogin;");
Boolean fot=false;
Boolean entered2 =false;
loop3: while(rs2.next())
{
recipent = rs2.getString("email");
rname = rs2.getString("first");
if(Bcc.equals(""))
{
}
else
{
if(recipent.equals(Bcc))
{
fot = true;
break loop3;
}
else
{
fot= false;
}
}
}
if(entered2=true)
{
if(fot=true)
{
stmt.executeUpdate("Insert into inbox Values('"+frmname+"','"+Sub+"','"+Msg+"',curDate(), curTime(),'"+rname+"','"+from+"');");
new inbox().setVisible(true);
this.dispose();
}
else
{
JOptionPane.showMessageDialog(null,"Sorry the 'CC' Email you entered does not exist.");
}
}
}
是否可以因为创建了多个结果集?还是因为创建了多个while语句?
答案 0 :(得分:1)
以下内容无效:
ResultSet rs = stmt.executeQuery("select first,email,mail,name from login,curlogin;");
String from =rs.getString("mail");
String frmname =rs.getString("name");
执行查询在位置-1返回ResultSet。您需要通过迭代器或首先调用rs.next();
来访问它(假设您有任何结果)。
答案 1 :(得分:1)
首先访问之前,您需要致电rs.next()
:
ResultSet rs = stmt.executeQuery("select first,email,mail,name from login,curlogin;");
if(rs.next()) { //Advances cursor to first/next row
String from =rs.getString("mail");
String frmname =rs.getString("name");
然后再次 - 使用标签循环有点GOTO
- 您可能也想重新考虑while(rs.next())
循环。
答案 2 :(得分:1)
您需要移动ResultSet
的光标才能从中获取值。在致电rs.first()
之前,请先使用rs.next()
或rs.getString("mail")
。
例如:
ResultSet rs = stmt.executeQuery("select first,email,mail,`name from login`,curlogin;");
String from;
if(rs.first()){
from =rs.getString("mail");
}