当前位置: 技术问答>java相关
servlet中调用bean的问题。
来源: 互联网 发布时间:2015-01-08
本文导语: 环境:win2kserver + weblogic5.1 +jdk1.2.2.005 ------------------------------------------------- servlet: ------------------------------------------------- package csms; import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import csms.GetTableInfo; public...
环境:win2kserver + weblogic5.1 +jdk1.2.2.005
-------------------------------------------------
servlet:
-------------------------------------------------
package csms;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import csms.GetTableInfo;
public class AddMis extends HttpServlet
{
public void service(HttpServletRequest req, HttpServletResponse res)
throws IOException
{
int i;
GetTableInfo getinfo = new GetTableInfo();
res.setContentType("text/html");
PrintWriter out = res.getWriter();
String[][] t_i = getinfo.GetInfo();
if ( t_i[ 0 ][ 0 ].compareTo("null") == 0 )
{
out.println("读数据库错误");
}
else
{
out.println("Hello World!");
out.println("");
out.println("");
for( i = 0 ; t_i[ i ][ 0 ].compareTo("null") != 0 ; i ++ )
{
out.println("" +
t_i[ i ][ 1 ] +
""
);
}
out.println("");
}
}
}
---------------------------------------------------------
GetTableInfo.java
---------------------------------------------------------
package csms;
import java.sql.*;
import java.io.*;
public class GetTableInfo
{
String sDBDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
String sConnStr = "jdbc:odbc:csms";
Connection conn = null;
public GetTableInfo()
{
try
{
Class.forName( sDBDriver );
}
catch( java.lang.ClassNotFoundException e )
{
System.err.println("GetTableInfo(): " + e.getMessage() );
}
}
public ResultSet executeQuery( String sql )
{
ResultSet rs = null;
try
{
conn = DriverManager.getConnection( sConnStr );
Statement stmt = conn.createStatement();
rs = stmt.executeQuery( sql );
}
catch( SQLException ex )
{
System.err.println( "aq.executeQuery:" + ex.getMessage() );
}
return rs;
}
public String[][] GetInfo()
{
String [][]arr = new String [20][4];
int n=0;
ResultSet rs = null;
rs = executeQuery( "select field_name,field_mean,html_style,field_style,field_length from tableinfo where table_name='mission' and mark=1" );
try
{
while ( rs.next() )
{
arr[n][0] = rs.getString("field_name");
arr[n][1] = rs.getString("html_mean");
arr[n][2] = rs.getString("html_style");
arr[n][3] = rs.getString("field_style");
arr[n][4] = rs.getString("field_length");
n++;
}
arr[n][0]="null";
rs.close();
}
catch( SQLException ex)
{
System.err.println( "aq.executeQuery:" + ex.getMessage() );
arr[0][0]="null";
}
return arr;
}
}
weblogic错误提示:
星期一 六月 14 18:23:45 GMT+08:00 2001: Servlet fail
ed with Exception
java.lang.NullPointerException
at csms.GetTableInfo.GetInfo(GetTableInfo.java, Compiled Code)
at csms.AddMis.service(AddMis.java, Compiled Code)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:865)
at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubIm
pl.java:123)
at weblogic.servlet.internal.ServletContextImpl.invokeServlet(ServletCon
textImpl.java:742)
at weblogic.servlet.internal.ServletContextImpl.invokeServlet(ServletCon
textImpl.java:686)
at weblogic.servlet.internal.ServletContextManager.invokeServlet(Servlet
ContextManager.java:247)
at weblogic.socket.MuxableSocketHTTP.invokeServlet(MuxableSocketHTTP.jav
a:363)
at weblogic.socket.MuxableSocketHTTP.execute(MuxableSocketHTTP.java:263)
at weblogic.kernel.ExecuteThread.run(ExecuteThread.java, Compiled Code)
星期一 六月 14 18:23:46 GMT+08:00 2001: servletimages: init
我学java不到一个月。还请高人指点是怎么回事?
-------------------------------------------------
servlet:
-------------------------------------------------
package csms;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import csms.GetTableInfo;
public class AddMis extends HttpServlet
{
public void service(HttpServletRequest req, HttpServletResponse res)
throws IOException
{
int i;
GetTableInfo getinfo = new GetTableInfo();
res.setContentType("text/html");
PrintWriter out = res.getWriter();
String[][] t_i = getinfo.GetInfo();
if ( t_i[ 0 ][ 0 ].compareTo("null") == 0 )
{
out.println("读数据库错误");
}
else
{
out.println("Hello World!");
out.println("");
out.println("");
for( i = 0 ; t_i[ i ][ 0 ].compareTo("null") != 0 ; i ++ )
{
out.println("" +
t_i[ i ][ 1 ] +
""
);
}
out.println("");
}
}
}
---------------------------------------------------------
GetTableInfo.java
---------------------------------------------------------
package csms;
import java.sql.*;
import java.io.*;
public class GetTableInfo
{
String sDBDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
String sConnStr = "jdbc:odbc:csms";
Connection conn = null;
public GetTableInfo()
{
try
{
Class.forName( sDBDriver );
}
catch( java.lang.ClassNotFoundException e )
{
System.err.println("GetTableInfo(): " + e.getMessage() );
}
}
public ResultSet executeQuery( String sql )
{
ResultSet rs = null;
try
{
conn = DriverManager.getConnection( sConnStr );
Statement stmt = conn.createStatement();
rs = stmt.executeQuery( sql );
}
catch( SQLException ex )
{
System.err.println( "aq.executeQuery:" + ex.getMessage() );
}
return rs;
}
public String[][] GetInfo()
{
String [][]arr = new String [20][4];
int n=0;
ResultSet rs = null;
rs = executeQuery( "select field_name,field_mean,html_style,field_style,field_length from tableinfo where table_name='mission' and mark=1" );
try
{
while ( rs.next() )
{
arr[n][0] = rs.getString("field_name");
arr[n][1] = rs.getString("html_mean");
arr[n][2] = rs.getString("html_style");
arr[n][3] = rs.getString("field_style");
arr[n][4] = rs.getString("field_length");
n++;
}
arr[n][0]="null";
rs.close();
}
catch( SQLException ex)
{
System.err.println( "aq.executeQuery:" + ex.getMessage() );
arr[0][0]="null";
}
return arr;
}
}
weblogic错误提示:
星期一 六月 14 18:23:45 GMT+08:00 2001: Servlet fail
ed with Exception
java.lang.NullPointerException
at csms.GetTableInfo.GetInfo(GetTableInfo.java, Compiled Code)
at csms.AddMis.service(AddMis.java, Compiled Code)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:865)
at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubIm
pl.java:123)
at weblogic.servlet.internal.ServletContextImpl.invokeServlet(ServletCon
textImpl.java:742)
at weblogic.servlet.internal.ServletContextImpl.invokeServlet(ServletCon
textImpl.java:686)
at weblogic.servlet.internal.ServletContextManager.invokeServlet(Servlet
ContextManager.java:247)
at weblogic.socket.MuxableSocketHTTP.invokeServlet(MuxableSocketHTTP.jav
a:363)
at weblogic.socket.MuxableSocketHTTP.execute(MuxableSocketHTTP.java:263)
at weblogic.kernel.ExecuteThread.run(ExecuteThread.java, Compiled Code)
星期一 六月 14 18:23:46 GMT+08:00 2001: servletimages: init
我学java不到一个月。还请高人指点是怎么回事?
|
好像并没有将GetTableInfo的实例放在bean容器中嘛!不是bean的问题!
比较你的下面两段:
1、中定义的数组长度不够哦!错误就发现到这里,有问题再说!
1、String [][]arr = new String [20][4]
2、 while ( rs.next() )
{
arr[n][0] = rs.getString("field_name");
arr[n][1] = rs.getString("html_mean");
arr[n][2] = rs.getString("html_style");
arr[n][3] = rs.getString("field_style");
arr[n][4] = rs.getString("field_length");
n++;
}
比较你的下面两段:
1、中定义的数组长度不够哦!错误就发现到这里,有问题再说!
1、String [][]arr = new String [20][4]
2、 while ( rs.next() )
{
arr[n][0] = rs.getString("field_name");
arr[n][1] = rs.getString("html_mean");
arr[n][2] = rs.getString("html_style");
arr[n][3] = rs.getString("field_style");
arr[n][4] = rs.getString("field_length");
n++;
}