mysql空指针异常

时间:2015-02-18 14:18:02

标签: java mysql linux jdbc

我正在运行一个自动生成电子邮件的调度程序。我在Linux窗口中设置了crontab文件,当我尝试执行调度程序时,我收到以下错误消息:

Exception excp conn: com.mysql.jdbc.Driver

我已经导入了所有必要的库,因为你可以看到我的源代码 需要紧急支持

package Hosting;
Hosting.ScheduleMessanger

public class ScheduleMessanger {

    /**
     * @param args
     */
    public static void main(String[] args) 
    {
        Statement stmt = null;
        ResultSet rset = null;
        Statement stmt1 = null;
        ResultSet rset1 = null;
        Connection conn = null;
        int i = 0;int _fesc_ = 0;int _nesc_ = 0;int _lesc_ = 0;
        String Query = "";String Query1 = "";String EmailText = "";String CustEmail = "";
        String connect_string = "jdbc:mysql://127.0.0.1/dbname?user=user&password=passw";


        try
        {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            conn = DriverManager.getConnection(connect_string);
        }
        catch (Exception excp)
        {
          conn = null;
          System.out.println("Exception excp conn: "+excp.getMessage());
          return;
        }

        Query = "Query goes here";
        try
        {
          stmt = conn.createStatement();
          rset = stmt.executeQuery(Query);
          while (rset.next())
          {
            EmailText = "Dear All <br>";
            EmailText = EmailText + "The complain #"+rset.getString(2)+" is still un-resolved. <br>";
            EmailText = EmailText + "This mail is being generated to take the matter in your knowledge. <br><br>";

            EmailText = EmailText + "Complain By : " + rset.getString(3) + " <br>";
            EmailText = EmailText + "Complaint Type : " + rset.getString(4) + "<br>";
            EmailText = EmailText + "Priority Level : " + rset.getString(21) + "<br>";
            EmailText = EmailText + "Problem Description : " + rset.getString(24) + "<br>";

            EmailText = EmailText+"Sincerely, <br>";

            _fesc_ = _nesc_ = _lesc_ = 0;
            CustEmail = "";
            Query1 = "Second Query";
            try
            {
              stmt1 = conn.createStatement();
              rset1 = stmt1.executeQuery(Query1);
              if (rset1.next())
              {
                _fesc_ = rset1.getInt(1);
                _nesc_ = rset1.getInt(2);
                _lesc_ = rset1.getInt(3);
              }
              rset1.close();
              stmt1.close();
            }
            catch (Exception e)
            {
              _fesc_ = _nesc_ = _lesc_ = 0;
            }

            if (_fesc_ == 0)
            {
              if (rset.getString(10).length() > 1) 
              {
                CustEmail = CustEmail + rset.getString(10) + ",";
              }
              if (rset.getString(11).length() > 1)
              {
                CustEmail = CustEmail + rset.getString(11) + ",";
              }
              if (rset.getString(12).length() > 1) 
              {
                CustEmail = CustEmail + rset.getString(12) + ",";
              }
              i = SendEmail("Complain Escalation", "First Complain Escalation", EmailText, CustEmail);
              if (i == 1) 
              {
                UpdateEscalationTable(rset.getInt(1), 1, conn);
              }
            }
            else if (_nesc_ == 0)
            {
              if (rset.getString(13).length() > 1) 
              {
                CustEmail = CustEmail + rset.getString(13) + ",";
              }
              if (rset.getString(14).length() > 1) 
              {
                CustEmail = CustEmail + rset.getString(14) + ",";
              }
              if (rset.getString(15).length() > 1)
              {
                CustEmail = CustEmail + rset.getString(15) + ",";
              }
              i = SendEmail("Complain Escalation", "Second Complain Escalation", EmailText, CustEmail);
              if (i == 1) 
              {
                UpdateEscalationTable(rset.getInt(1), 2, conn);
              }
            }
            else if (_lesc_ == 0)
            {
              if (rset.getString(16).length() > 1) 
              {
                CustEmail = CustEmail + rset.getString(16) + ",";
              }
              if (rset.getString(17).length() > 1) 
              {
                CustEmail = CustEmail + rset.getString(17) + ",";
              }
              if (rset.getString(18).length() > 1) 
              {
                CustEmail = CustEmail + rset.getString(18) + ",";
              }
              i = SendEmail("Complain Escalation", "Final Complain Escalation", EmailText, CustEmail);
              if (i == 1) 
              {
                UpdateEscalationTable(rset.getInt(1), 3, conn);
              }
            }
          }
          rset.close();
          stmt.close();
        }
        catch (Exception e)
        {
          e.printStackTrace();
        }

    }

}

    enter code here

3 个答案:

答案 0 :(得分:1)

你错过了类路径中的mysql jdbc连接器。

答案 1 :(得分:1)

正如Jens建议的那样

  

你错过了类路径中的mysql jdbc连接器。

如何解决?

在项目中找到.classpath文件,并在.classpath文件中添加mysql jdbc connection jar路径,

  

注意:如果您要修改此文件而您不熟悉它   ,所以小心其他错误也可能出现。

替代选项在eclipse中打开项目

  1. 右键点击project --> build path --> configure build path
  2. 会出现一个弹出窗口转到library tab --> Add jar --> Give Path to Your jar
  3. 再次建造项目,宾果你很高兴
  4. 关于您的代码的一些建议

    newInstance() method在下面提到的行中不是必需的,

    Class.forName("com.mysql.jdbc.Driver").newInstance();
    

    直接使用

    Class.forName("com.mysql.jdbc.Driver");
    

    而不是像这样在单行中声明变量

    String Query = "";String Query1 = "";String EmailText = "";String CustEmail = "";
    

    在单独的行中声明它们会增加 可读性

    String Query = "";
    String Query1 = "";
    String EmailText = "";
    String CustEmail = "";
    

    而不是String使用StringBuffer因为

    String s = "a" + "b" + "c";
    

    最终将成为

    String s = new StringBuffer().append("a").append("b").append("c").toString();
    

答案 2 :(得分:0)

在lib文件夹中添加连接所需的jar文件 com.mysql.jdbc.Driver 点击此链接打开连接 http://www.mkyong.com/jdbc/how-to-connect-to-mysql-with-jdbc-driver-java/ 从上面的链接下载示例,然后复制粘贴sql jar文件并使用

直接从中下载 http://dev.mysql.com/downloads/connector/j/