急!!!在控制台里数据库操作方法管用在JSP中就不管用是怎么回事?详细见贴内错误信息
我在机房的XP系统上完全可以运行我写的所有代码,但是到了我自己的机器上(win7系统)就不好使!
还有我在控制台程序中测试数据库操作方法完全没问题,然而到了JSP页面调用时却总是报错。
详细错误信息:
JSP页面显示错误:
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
java.lang.NullPointerException
com.two.stms.dao.ScoreDao.addScore(ScoreDao.java:43)
com.two.stms.web.AddScoreSerlvet.doPost(AddScoreSerlvet.java:95)
com.two.stms.web.AddScoreSerlvet.doGet(AddScoreSerlvet.java:60)
javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
note The full stack trace of the root cause is available in the Apache Tomcat/6.0.16 logs.
控制台里显示的错误信息是:
成绩编号:0分数:234.0学生编号:1课程编号:12
-----------------------------
java.lang.NullPointerException
at java.lang.String.<init>(String.java:176)
at oracle.sql.CharacterSet.AL32UTF8ToString(CharacterSet.java:1517)
at oracle.jdbc.driver.DBConversion.CharBytesToString(DBConversion.java:589)
at oracle.jdbc.driver.DBConversion.CharBytesToString(DBConversion.java:542)
at oracle.jdbc.driver.T4CTTIoauthenticate.receiveOauth(T4CTTIoauthenticate.java:816)
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:362)
at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:414)
at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:165)
at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:35)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:801)
at java.sql.DriverManager.getConnection(DriverManager.java:582)
at java.sql.DriverManager.getConnection(DriverManager.java:185)
at com.two.stms.util.DBConnection.getConnection(DBConnection.java:40)
at com.two.stms.dao.ScoreDao.addScore(ScoreDao.java:33)
at com.two.stms.web.AddScoreSerlvet.doPost(AddScoreSerlvet.java:95)
at com.two.stms.web.AddScoreSerlvet.doGet(AddScoreSerlvet.java:60)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:619)
insert into Score(id,sid,cid,score,examdate) values(s_Score_id.nextval,?,?,?,?)
====================
2012-7-15 16:06:08 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet AddScoreSerlvet threw exception
java.lang.NullPointerException
at com.two.stms.dao.ScoreDao.addScore(ScoreDao.java:43)
at com.two.stms.web.AddScoreSerlvet.doPost(AddScoreSerlvet.java:95)
at com.two.stms.web.AddScoreSerlvet.doGet(AddScoreSerlvet.java:60)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:619)
这是我在控制台的测试数据:
程序代码:
package com.two.stms.util; import java.beans.Statement; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.text.SimpleDateFormat; import com.two.stms.dao.ScoreDao; import com.two.stms.entity.Score; public class DBConnection { // public Connection getConnection(){ // Connection conn=null; // // String url = "jdbc:oracle:thin:@localhost:1521:XE"; // String username = "已隐藏"; // String password = "已隐藏"; // // //驱动程序加到当前工程中 // //加载驱动程序 // Class.forName("oracle.jdbc.driver.OracleDriver"); // conn=DriverManager.getConnection(url,username,password); // System.out.print("-------------------------"); // System.out.println(conn==null); // System.out.print("-------------------------"); // //获得联接对象stms // return conn; // // } public Connection getConnection() throws Exception { Class.forName("oracle.jdbc.driver.OracleDriver"); String url = "jdbc:oracle:thin:@localhost:1521:XE"; String username = "已隐藏"; String password = "已隐藏"; return DriverManager.getConnection(url, username, password); } public static void main(String[] args) throws Exception { Class.forName("oracle.jdbc.driver.OracleDriver"); System.out.println("ok------------"); String url = "jdbc:oracle:thin:@localhost:1521:XE"; String username = "已隐藏"; String password = "已隐藏"; Connection conn = DriverManager.getConnection(url,username,password); PreparedStatement ps=conn.prepareStatement("select * from Score"); System.out.println("----------------ok"); System.out.println(ps==null); ScoreDao sd=new ScoreDao(); Score sc=new Score(); sc.setCid(12); sc.setSid(1); sc.setScore(89); sc.setExamdate(new SimpleDateFormat("yyyy-mm-dd").parse("2000-3-4")); sd.addScore(sc); ResultSet rs=ps.executeQuery(); System.out.println("id\tsid\tcid\tscore\texamdate\t"); while(rs.next()){ System.out.print(rs.getInt("id")+"\t"); System.out.print(rs.getInt("sid")+"\t"); System.out.print(rs.getInt("cid")+"\t"); System.out.print(rs.getDouble("score")+"\t"); System.out.print(rs.getDate("examdate")+"\n"); } rs.close(); ps.close(); conn.close(); System.out.println("测试无误"); } }
这是控制台的运行结果:
ok------------
----------------ok
false
====================
====================
id sid cid score examdate
4 1 12 89.0 2000-01-04
1 1 10 64.0 2012-06-09
2 2 12 54.0 2012-06-09
3 3 11 89.0 2012-06-09
5 1 12 89.0 2000-01-04
测试无误
====================分割线=======================================
这是ScoreDao类的增加方法:
/**增加一条成绩
* @throws SQLException
* @throws ClassNotFoundException */
public boolean addScore(Score sc) throws ClassNotFoundException, SQLException{
boolean result = false;
Connection conn=null;
PreparedStatement ps=null;
//获取Connection对象
try{
try {
conn= new DBConnection().getConnection();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String sql = "insert into Score(id,sid,cid,score,examdate) values(s_Score_id.nextval,?,?,?,?)";
System.out.println(sql);
System.out.println("====================");
//赋值
ps=conn.prepareStatement(sql);//此处提示错误
System.out.println("====================");
ps.setInt(1,sc.getSid());
ps.setInt(2, sc.getCid());
ps.setFloat(3, (float)sc.getScore());
ps.setDate(4, new Date(sc.getExamdate().getTime()));
//执行
result=ps.executeUpdate()>0;//返回结果是SQL语句执行后是否大于0
}finally{
//无论是否增加数据成功都关闭资源
if(ps!=null) ps.close();
if(conn!=null) conn.close();
}
return result;
}
但是我在JSP中调用时却完全不能增加和查询数据
这是我在JSP中的servlet代码:
package com.two.stms.web;
import
import
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.two.stms.dao.ScoreDao;
import com.two.stms.entity.Score;
public class AddScoreSerlvet extends HttpServlet {
/**
* Constructor of the object.
*/
public AddScoreSerlvet() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
doPost(req,res);//此处报错
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
request.setCharacterEncoding("utf-8");
Score sc=new Score();
sc.setScore(Double.parseDouble(request.getParameter("score")));
sc.setSid(Integer.parseInt(request.getParameter("sid")));
sc.setCid(Integer.parseInt(request.getParameter("cid")));
SimpleDateFormat sf=new SimpleDateFormat("yyyy-mm-dd");
String examdate=request.getParameter("examdate");
System.out.println(sc.toString());
try {
sc.setExamdate(sf.parse(examdate));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ScoreDao sd=new ScoreDao();
try {
System.out.println("-----------------------------");
sd.addScore(sc);//此处报错
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
response.sendRedirect("ListScore");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");//此处报错
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the POST method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
还有我在机房电脑上的JDBC的配置步骤我也忘了
大概有:
1.增加用户库,把Oracle10g的JDBC jar包增加到里面了
2.在项目中增加用户库
3.配置了tomcat服务器
4.发布了项目
我在自己电脑电脑上也是进行了这些步骤,难道是我还有没配置的项吗?
大侠们快帮我解决解决吧,我都快崩溃了,明明在机房的电脑上完全无误的跑出来在自己的电脑上就出不来,快帮我解决解决啊~~~~