首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

JSP对SQL SERVER进行数据库操作的奇怪有关问题~

2011-12-28 
JSP对SQL SERVER进行数据库操作的奇怪问题~~~~packagetestimporttest.*importjava.sql.*publicclassTyp

JSP对SQL SERVER进行数据库操作的奇怪问题~~~~
package   test;
import   test.*;
import   java.sql.*;
public   class   Typebean
{
  String   typeid= " ";
  String   typename= " ";
  Connection   conn=null;
  ResultSet   rs=null;
  Statement   sta=null;
public   Typebean()
{

              String   user= "sa ";
              String   pass= "8760747 ";
                String   driverName   =   "com.microsoft.sqlserver.jdbc.SQLServerDriver ";
                String   dbURL   =   "jdbc:sqlserver://localhost:1433;DatabaseName=BookStore ";              
             
                try{
                  Class.forName( "com.microsoft.sqlserver.jdbc.SQLServerDriver ");
                          conn   =   DriverManager.getConnection(dbURL,user,pass);
                          sta=conn.createStatement();                  
                }
                  catch   (Exception   e)
                  {
                        e.printStackTrace();
                  }
}
public   ResultSet   getTypeinfo()
{
try
{
    sta=conn.createStatement();
                rs=sta.executeQuery( "select   typename   from   type ");

}
catch(SQLException   e)
{
System.out.println( "捕获了 "+e.getMessage()+ "异常 ");
}
return   rs;
}
public   void   setTypeid(String   typeid)
{
this.typeid=typeid;
}
    public   String   getTypeid()
    {
return   typeid;
    }
public   void   setTypename(String   typename)
{
this.typename=typename;
}
public   String   getTypename()
{
return   typename;
}

public   boolean   executeInsert()
{
boolean   b=false;
String   sql= "insert   into   type   values( ' "+typename+ " ') ";
try
{            
sta.executeUpdate(sql);
b=true;

}
catch(SQLException   e)
{
System.out.println( "捕获了 "+e.getMessage()+ "异常 ");
}
return   b;
}
public   static   void   main(String   args[])
{
        Boolean   b;
Typebean   t=new   Typebean();
t.setTypename( "机械书 ");


b=t.executeInsert();
if(b)
System.out.print( "INSERT   OK ");
}
      }

上面的程序可以得出结果。完全没有问题....
但是如果我把上面的主函数去掉,并且改成javabean.放在JSP里面运行
就提示NULLPOINTEXCEPTIONG
为什么????

%@   page   contentType= "text/html;   charset=gb2312 "   language= "java "   import= "java.sql.* "   errorPage= " "   %>
<!DOCTYPE   html   PUBLIC   "-//W3C//DTD   XHTML   1.0   Transitional//EN "   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<html   xmlns= "http://www.w3.org/1999/xhtml ">
<head>
<meta   http-equiv= "Content-Type "   content= "text/html;   charset=gb2312 "   />
<title> 无标题文档 </title>
</head>
<jsp:useBean   id= "type "   class= "test.Typebean "     scope= "session "/>
<body>
<!-- <jsp:setProperty   name= "type "   property= "typename "   param= "typename "> </jsp:setProperty> -->
<%
//request.setCharacterEncoding( "GB2312 ");
boolean   b=false;
type.setTypename( "机械 ");
b=type.executeInsert();
if(b)
out.print( "添加成功 ");
else
out.print( "添加失败 ");
%>
</body>
</html>


期待高手解答!!!


[解决办法]
出错行是
sta.executeUpdate(sql);
提示
Nullpointerexception~~~
从这里可以看出应该是typename这个参数没有传进去`~
你把String sql= "insert into type values( ' "+typename+ " ') ";改为一个具体的值看看
String sql= "insert into type values( '机械 ') ";如果没有什么问题的话~~
那就是其它问题`~~比如编码,class文件有没有放对什么的``
[解决办法]
<jsp:useBean id= "type " class= "test.Typebean " scope= "session "/>

换成
<%
test.Typebean type=new test.Typebean();
%>

热点排行