上面就是我的public class 调用的插入新闻的方法,在后台运行,没一点问题,但是到了jsp时就报错,我的并不是jsp:useBean的错,因为我已经在这点上核对过,满足三点要求。我的简单测试如下:<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*,ouyang.com.*" errorPage="" %><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<jsp:useBean id="news" class="ouyang.com.newsDBase"  scope="request"/> 
<%
 news.connect();
 news.submitnews("79","sss","0","ss");
 news.closeconn();
%>报错如下:
十分感谢!!JSPJava

解决方案 »

  1.   

    try{
    pre=dbConn1.prepareStatement("insert into news values(?,?,?,?)");
    pre.setString(1,email);
    pre.setString(2,text);
    pre.setString(3,sex);
    pre.setString(4,address);
    pre.execute();
    System.out.println("success");
    pre.close();
    }catch(SQLException e){
    e.printStackTrace();
    }
      

  2.   

    试试将dbConn1其定义为静态的初始化,服务启动就建立好链接。
      

  3.   

    吧newDBase类完整的添出来看看  你在哪里获取的dbcon1
      

  4.   

    谢谢大家回复,但问题还是没有解决。
    1,对dong_y888提出的那种,我试了,没用还是报空指针错。
    2,对jlliji提出的下面提出,对于tianma630,wanglingtao回复在下面。“后台运行没问题是什么意思?”就是在eclipes中运行结果正确的插入数据库。3,对oh_Maxy提出的,定义如下:
    public  static   PreparedStatement  pre=null;
    public  static   ResultSet  rs=null;
    public static  Connection dbConn1=null;
    4,这是我的java代码:
    public class newsDBase { /**
     * @连接,关闭数据库
     */
      private static  String username="News";
      private static  String pass="123456";
      public  static   PreparedStatement  pre=null;
      public  static   ResultSet  rs=null;
      public static  Connection dbConn1=null;
      public static void main(String[] args) {

    //newsDBase nsw= new newsDBase();

      }

    // public  newsDBase()
    // {
    // this.connect();
    // this.submitnews("[email protected]","hello","1","china");
    // this.closeconn();
    //
    // }


    public void connect()
    {


    String driverName1 = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
    String dbURL1 = "jdbc:sqlserver://localhost:1433;DatabaseName=News";
      
      try
       {
      
      Class.forName(driverName1);
      dbConn1 = DriverManager.getConnection(dbURL1,username,pass);
      
       }
     
      
     catch(Exception e){
      e.printStackTrace();
      }
    }

    public void closeconn()
    {
    try {

    pre.close();
    dbConn1.close();

    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }


    }

    public void closeres()
    {
    try {

    rs.close();

    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }


    }

    public void submitnews(String email,String text,String sex,String address)
    {

    try {


        


        pre=dbConn1.prepareStatement("insert into  news values('"+email+"','"+text+"','"+sex+"','"+address+"')");
    pre.execute();
    System.out.println("succss");
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }


    }


    }
    test.jsp测试JSP代码如下:
    <%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*,ouyang.com.*" errorPage="" %><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <jsp:useBean id="news" class="ouyang.com.newsDBase"  scope="request"/> 
    <%
     news.connect();
     news.submitnews("[email protected]","hello","1","china");
     news.closeconn();
    %>