Java.sql.SQLException:参数索引超出范围2> 1

时间:2016-05-07 22:04:18

标签: java mysql

我遇到了这个错误,我不知道是什么导致了这个错误。

这是我想出的一个简单的电子邮件自动回复java。我将连接到一个数据库,该数据库在表中有两列。 rEmail和sEmail,接收者......发件人。

所以如果接收者和发送者匹配我为我的java代码做的检查..它会自动回复一封电子邮件,但我一直得到这个SQL错误,我不知道它是什么...下面是我的代码..

public class TestList
  extends TestMatch
{

  private Collection senders;

  public void init()
    throws MessagingException
  {

  }

  public Collection match(Mail mail) throws MessagingException
  {
Connection conn = null;
Statement stmt = null;
try
{
  Class.forName("com.mysql.jdbc.Driver");

  conn = DriverManager.getConnection("jdbc:mysql://localhost/mail", "root", "password");

  stmt = conn.createStatement();

  String receiver = mail.getRecipients().toString();
  String sender = mail.getSender().toString();

  String receiverResult = receiver.substring(receiver.indexOf("[") + 1, receiver.indexOf("]"));

  String sql = "SELECT senderEmail FROM TestList where rEmail='"+ rResult +"'" + " AND sEmail='"+sender+"'";
  System.out.println("SQL ERROR Here -- 31");

  ResultSet rs = stmt.executeQuery(sql);

  this.senders = new HashSet();
  while (rs.next()) {
    this.senders.add(new MailAddress(rs.getString("sEmail")));
  }

  rs.close();
  stmt.close();
  conn.close(); 
}
catch (SQLException se)
{
  se.printStackTrace();
}
catch (Exception e)
{
  e.printStackTrace();
}
finally
{
  try
  {
    if (stmt != null) {
      stmt.close();
    }
  }
  catch (SQLException se2) {}
  try
  {
    if (conn != null) {
      conn.close();
    }
  }
  catch (SQLException se)
  {
    se.printStackTrace();
  }
}   
    System.out.println("SQL ERROR Here -- 72);
if (this.senders.contains(mail.getSender())) {  
    System.out.println("SQL ERROR Here -- 74");
    Properties props = new Properties();
    Session session = Session.getDefaultInstance(props);
    MimeMessage msg = new MimeMessage(session);

        System.out.println("auto reply");
        msg.setSubject ("subject");
        msg.setSender (new InternetAddress("Postmaster@test.com"));
        msg.setRecipient(Message.RecipientType.TO,(new InternetAddress("user1@captcha.com")));
        msg.setText ("describe the mail text of","utf-8","Plain");
        Transport.send(msg);
        System.out.println("e-mail sent.");

  return mail.getRecipients();
} else {
return null;
}
  }
}

这件事会跑两次,我也不知道为什么。起初一切都会顺利进行,直到发送电子邮件然后它会再次重复并停在SQL错误--72。

我一直在努力解决这个问题,所以有人可以帮忙吗?

=====使用rs.getString(1)或rs.getString(2)后更新NEW ERROR;

`SQL ERROR Here -- line 31
 java.sql.SQLException: Column Index out of range, 2 > 1.
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:987)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:982)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:927)
    at com.mysql.jdbc.ResultSetImpl.checkColumnBounds(ResultSetImpl.java:818)
    at com.mysql.jdbc.ResultSetImpl.getStringInternal(ResultSetImpl.java:5651)
    at com.mysql.jdbc.ResultSetImpl.getString(ResultSetImpl.java:5570)
    at org.apache.james.transport.matchers.SenderIsGreyList.match(SenderIsGreyList.java:59)
    at org.apache.james.transport.LinearProcessor.service(LinearProcessor.java:357)
    at org.apache.james.transport.JamesSpoolManager.process(JamesSpoolManager.java:405)
    at org.apache.james.transport.JamesSpoolManager.run(JamesSpoolManager.java:309)
    at java.lang.Thread.run(Thread.java:745)
SQL ERROR Here -- line 72`

1 个答案:

答案 0 :(得分:2)

您似乎正在尝试检索名为" sEmail"的列。来自结果集中的行......

  rs.getString("sEmail")

但是您的查询没有返回该名称的列。 SELECT语句返回名为" senderEmail"的列。

我总是使用数字。使用1获取第一列,例如

  rs.getString(1)