主题:[讨论]JSP中使用JavaBean访问不了数据库
[em18][em18][em18]
我JSP文件:
<%@ 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>
<body>
<jsp:useBean id = "Connection" class= "MyBean.DbBean" scope="page"/>
<%! Connection conn; %>
<%
String sql = "select ContactId, ContactName, ContactTitle,DepartmentId from ContactInfo";
conn = Connection.getConnect();
if( conn == null ) out.print( "fail" );
try {
Statement s = conn.createStatement();
ResultSet rs = s.executeQuery( sql );
while( rs.next() ) {
out.println( rs.getString( "ContactId" ) + "\t" + rs.getString( "ContactName" ) + "\t" + rs.getString( "ContactTitle" ) + "\t" + rs.getString( "DepartmentId" ) );
}
} catch( NullPointerException e ) {
out.print( e.getMessage() );
}
//Connection.disConnect( rs, null, s, conn );
//
%>
</body>
</html>
我的JavaBean中的getConnect函数:
public Connection getConnect() {
String JDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
String conURL = "jdbc:odbc:CRM_System";
Connection con = null;
try {
Class.forName( JDriver );
System.out.println( "Driver Success!" );
} catch( ClassNotFoundException e ) {
System.out.println( "forname:" + e.getMessage() );
}
try {
con = DriverManager.getConnection( conURL );
System.out.println( "Connect Success!" );
} catch( SQLException e ) {
System.out.println( "SQLException:" + e.getMessage() );
}
return con;
}
[color=FF0000]为什么IE中输出:failnull,我的conn为什么是个null呢[/color]
我JSP文件:
<%@ 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>
<body>
<jsp:useBean id = "Connection" class= "MyBean.DbBean" scope="page"/>
<%! Connection conn; %>
<%
String sql = "select ContactId, ContactName, ContactTitle,DepartmentId from ContactInfo";
conn = Connection.getConnect();
if( conn == null ) out.print( "fail" );
try {
Statement s = conn.createStatement();
ResultSet rs = s.executeQuery( sql );
while( rs.next() ) {
out.println( rs.getString( "ContactId" ) + "\t" + rs.getString( "ContactName" ) + "\t" + rs.getString( "ContactTitle" ) + "\t" + rs.getString( "DepartmentId" ) );
}
} catch( NullPointerException e ) {
out.print( e.getMessage() );
}
//Connection.disConnect( rs, null, s, conn );
//
%>
</body>
</html>
我的JavaBean中的getConnect函数:
public Connection getConnect() {
String JDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
String conURL = "jdbc:odbc:CRM_System";
Connection con = null;
try {
Class.forName( JDriver );
System.out.println( "Driver Success!" );
} catch( ClassNotFoundException e ) {
System.out.println( "forname:" + e.getMessage() );
}
try {
con = DriverManager.getConnection( conURL );
System.out.println( "Connect Success!" );
} catch( SQLException e ) {
System.out.println( "SQLException:" + e.getMessage() );
}
return con;
}
[color=FF0000]为什么IE中输出:failnull,我的conn为什么是个null呢[/color]