mysql空指针异常
mysql null pointer exception
我是 运行 自动生成电子邮件的调度程序。我在 Linux window 中设置了 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
您错过了类路径中的 mysql jdbc 连接器。
正如 Jens 建议的那样
You miss the mysql jdbc connectior in your classpath.
如何解决?
在您的项目中找到 .classpath
文件并在您的 .classpath 文件中添加 mysql jdbc connection
jar 路径,
Note: If you are modifying this file and you are not familiar with it
, So Be carefull else other error may also arise .
替代选项在 eclipse 中打开项目
- 右键单击
project --> build path --> configure build path
- 将出现一个弹出窗口转到
library tab --> Add jar --> Give Path to Your jar
- 再次构建项目,你就可以开始了
关于你代码的一些建议
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();
在您的 lib 文件夹中添加连接所需的 jar 文件
com.mysql.jdbc.Driver
按照此 link 打开连接
http://www.mkyong.com/jdbc/how-to-connect-to-mysql-with-jdbc-driver-java/
从上面 link 下载示例,然后复制粘贴 sql jar 文件并使用
或者
直接从这里下载
http://dev.mysql.com/downloads/connector/j/
我是 运行 自动生成电子邮件的调度程序。我在 Linux window 中设置了 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
您错过了类路径中的 mysql jdbc 连接器。
正如 Jens 建议的那样
You miss the mysql jdbc connectior in your classpath.
如何解决?
在您的项目中找到 .classpath
文件并在您的 .classpath 文件中添加 mysql jdbc connection
jar 路径,
Note: If you are modifying this file and you are not familiar with it , So Be carefull else other error may also arise .
替代选项在 eclipse 中打开项目
- 右键单击
project --> build path --> configure build path
- 将出现一个弹出窗口转到
library tab --> Add jar --> Give Path to Your jar
- 再次构建项目,你就可以开始了
关于你代码的一些建议
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();
在您的 lib 文件夹中添加连接所需的 jar 文件
com.mysql.jdbc.Driver
按照此 link 打开连接
http://www.mkyong.com/jdbc/how-to-connect-to-mysql-with-jdbc-driver-java/
从上面 link 下载示例,然后复制粘贴 sql jar 文件并使用
或者
直接从这里下载
http://dev.mysql.com/downloads/connector/j/