JSP通过JavaBean访问数据库的问题。 ( 积分: 100 )

  • 主题发起人 主题发起人 eastnet
  • 开始时间 开始时间
E

eastnet

Unregistered / Unconfirmed
GUEST, unregistred user!
JavaBean中操作数据库的代码如下:
package ch12;
import java.sql.*;
public class ContactBean
{
private Connection con;
//初始化连接
public ContactBean()
{
String CLASSFORNAME="com.microsoft.jdbc.sqlserver.SQLServerDriver";
String SERVANDDB="jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=jspdev";
String USER="sa";
String PWD="sa";
try
{
Class.forName(CLASSFORNAME);
con = DriverManager.getConnection(SERVANDDB,USER,PWD);
}
catch(Exception e)
{
e.printStackTrace();
}
}
//在数据库中添加信息;
public void addContact() throws Exception
{
try
{
PreparedStatement pstmt=con.prepareStatement("insert into Contact values(?,?,?,?,?,?)");
pstmt.setString(1,"LiuJun");
pstmt.setInt(2,1338137);
pstmt.setString(3,"01084473962");
pstmt.setString(4,"delphipig@163.com");
pstmt.setDate(5,new java.sql.Date(new java.util.Date().getTime()));
pstmt.setString(6,"other email:liujun@jumptone.com.cn");
}
catch(Exception e)
{
e.printStackTrace();
throw e;
}
}
//查询数据库
public ResultSet getContact(String userName)
{
try
{
Statement stm=con.createStatement();
ResultSet result=stm.executeQuery("select * from contact where userName='"+userName+"'");
return result;
}
catch(Exception e)
{
}
return null;
}
}
//=========================================================================
使用JavaBean的JSP文件如下:
<%@ page contentType=&quot;text/html;
charset=gb2312&quot;
language=&quot;java&quot;
import =&quot;java.sql.*,java.io.*&quot;%>
<jsp:useBean id=&quot;contact&quot;
class=&quot;ch12.ContactBean&quot;
scope=&quot;page&quot;/>
使用JavaBean来操作数据库:<hr>
<table border=1>
<tr><td>姓名</td><td>手机</td><td>电话</td><td>email</td><td>最后的联系时间</td><td>备注</td><tr>
<%
try
{
contact.addContact();
ResultSet rst=contact.getContact(&quot;LiuJun&quot;);
while(rst.next())
{
out.println(&quot;<tr>&quot;);
out.println(&quot;<td>&quot;+rst.getString(&quot;userName&quot;)+&quot;</td>&quot;);
out.println(&quot;<td>&quot;+rst.getInt(&quot;mobile&quot;)+&quot;</td>&quot;);
out.println(&quot;<td>&quot;+rst.getString(&quot;phone&quot;)+&quot;</td>&quot;);
out.println(&quot;<td>&quot;+rst.getString(&quot;mail&quot;)+&quot;</td>&quot;);
out.println(&quot;<td>&quot;+rst.getDate(&quot;lastcontact&quot;)+&quot;</td>&quot;);
out.println(&quot;<td>&quot;+rst.getString(&quot;mem&quot;)+&quot;</td>&quot;);
out.println(&quot;</tr>&quot;);
}
rst.close();
}
catch(Exception e)
{
}
%>
程序执行时,应该会返回LiuJun 1338137 01084473962 delphipig@163.com 2005/05/27 other email:liujun@jumptone.com.cn
这样的结果,
而程序居然没有任何返回结果,请大家帮忙看看是哪的错?谢谢.
 
JavaBean中操作数据库的代码如下:
package ch12;
import java.sql.*;
public class ContactBean
{
private Connection con;
//初始化连接
public ContactBean()
{
String CLASSFORNAME=&quot;com.microsoft.jdbc.sqlserver.SQLServerDriver&quot;;
String SERVANDDB=&quot;jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=jspdev&quot;;
String USER=&quot;sa&quot;;
String PWD=&quot;sa&quot;;
try
{
Class.forName(CLASSFORNAME);
con = DriverManager.getConnection(SERVANDDB,USER,PWD);
}
catch(Exception e)
{
e.printStackTrace();
}
}
//在数据库中添加信息;
public void addContact() throws Exception
{
try
{
PreparedStatement pstmt=con.prepareStatement(&quot;insert into Contact values(?,?,?,?,?,?)&quot;);
pstmt.setString(1,&quot;LiuJun&quot;);
pstmt.setInt(2,1338137);
pstmt.setString(3,&quot;01084473962&quot;);
pstmt.setString(4,&quot;delphipig@163.com&quot;);
pstmt.setDate(5,new java.sql.Date(new java.util.Date().getTime()));
pstmt.setString(6,&quot;other email:liujun@jumptone.com.cn&quot;);
}
catch(Exception e)
{
e.printStackTrace();
throw e;
}
}
//查询数据库
public ResultSet getContact(String userName)
{
try
{
Statement stm=con.createStatement();
ResultSet result=stm.executeQuery(&quot;select * from contact where userName='&quot;+userName+&quot;'&quot;);
return result;
}
catch(Exception e)
{
}
return null;
}
}
//=========================================================================
使用JavaBean的JSP文件如下:
<%@ page contentType=&quot;text/html;
charset=gb2312&quot;
language=&quot;java&quot;
import =&quot;java.sql.*,java.io.*&quot;%>
<jsp:useBean id=&quot;contact&quot;
class=&quot;ch12.ContactBean&quot;
scope=&quot;page&quot;/>
使用JavaBean来操作数据库:<hr>
<table border=1>
<tr><td>姓名</td><td>手机</td><td>电话</td><td>email</td><td>最后的联系时间</td><td>备注</td><tr>
<%
try
{
contact.addContact();
ResultSet rst=contact.getContact(&quot;LiuJun&quot;);
while(rst.next())
{
out.println(&quot;<tr>&quot;);
out.println(&quot;<td>&quot;+rst.getString(&quot;userName&quot;)+&quot;</td>&quot;);
out.println(&quot;<td>&quot;+rst.getInt(&quot;mobile&quot;)+&quot;</td>&quot;);
out.println(&quot;<td>&quot;+rst.getString(&quot;phone&quot;)+&quot;</td>&quot;);
out.println(&quot;<td>&quot;+rst.getString(&quot;mail&quot;)+&quot;</td>&quot;);
out.println(&quot;<td>&quot;+rst.getDate(&quot;lastcontact&quot;)+&quot;</td>&quot;);
out.println(&quot;<td>&quot;+rst.getString(&quot;mem&quot;)+&quot;</td>&quot;);
out.println(&quot;</tr>&quot;);
}
rst.close();
}
catch(Exception e)
{
}
%>
程序执行时,应该会返回LiuJun 1338137 01084473962 delphipig@163.com 2005/05/27 other email:liujun@jumptone.com.cn
这样的结果,
而程序居然没有任何返回结果,请大家帮忙看看是哪的错?谢谢.
 
頂一下。
 
return null;
去掉试试
你用什么开发工具 如果是Eclipse 包含了struts 用它的标记比较好
 
我的JAVABEAN,如果语句select count(*) from table的记录数是0时就要出错,以下是我的JAVA代码,希望大峡指点一下
package chenliang;
import java.io.*;
import java.sql.*;
public class Conndb
{
private String DBDriver = &quot;sun.jdbc.odbc.JdbcOdbcDriver&quot;;
private String ConnStr =&quot;jdbc:odbc:tt&quot;;
private String Uid = &quot;&quot;;
private String Pwd = &quot;&quot;;
private Connection conn = null;
private Statement stmt=null;
private ResultSet rs = null;

public void setDBDriver(String DBDriver)
{
this.DBDriver = DBDriver;
}

public String getDBDriver()
{
return DBDriver;
}

public void setConnStr(String ConnStr)
{
this.ConnStr = ConnStr;
}

public String getConnStr()
{
return ConnStr;
}

public void setUid(String Uid)
{
this.Uid = Uid;
}

public String getUid()
{
return Uid;
}
public void setPwd(String Pwd)
{
this.Pwd = Pwd;
}

public String getPwd()
{
return Pwd;
}

public boolean disconnect()
{
try
{
if(rs!=null)
rs.close();
if(stmt!=null)
stmt.close();
if(conn!=null)
conn.close();
return true;
}
catch(Exception ex)
{
return false;
}

}
public ResultSet query(String rssql)
{
try
{
Class.forName(DBDriver);
conn = DriverManager.getConnection(ConnStr,Uid,Pwd);
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
rs = stmt.executeQuery(rssql);
return rs;
}
catch(Exception ex)
{
return null;
}
}

public boolean update(String rssql)throws Exception
{
//rssql = getStr(rssql);

try
{
Class.forName(DBDriver);
conn = DriverManager.getConnection(ConnStr,Uid,Pwd);
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
stmt.executeUpdate(rssql);
conn.close();
stmt.close();
return true;
}
catch(Exception ex)
{
return false;
}
}

public String getStr(String str)
{
try
{
byte[] temp = str.getBytes(&quot;ISO8859-1&quot;);
String temp2 = new String(temp);
return temp2;

}catch(Exception e){}
return &quot;null&quot;;
}
}
 
public ResultSet getContact(String userName)
{
try
{
Statement stm=con.createStatement();
ResultSet result=stm.executeQuery(&quot;select * from contact where userName='&quot;+userName+&quot;'&quot;);
return result;
}
catch(Exception e)
{
}
return null;
}
无论结果是什么,总是返回null,先初始化一下rs=null,最后,return rs;
 
re:程序执行时,应该会返回LiuJun 1338137 01084473962 delphipig@163.com 2005/05/27 other email:liujun@jumptone.com.cn
你的public ResultSet getContact(String userName)应该返回一个rs结果集对象,没有结果集返回当然为空了。
 
<table border=1>
<tr><td>姓名</td><td>手机</td><td>电话</td><td>email</td><td>最后的联系时间</td><td>备注</td><tr>
<%
try
{
contact.addContact();
ResultSet rst=contact.getContact(&quot;LiuJun&quot;);
if (rst!=null)
while(rst.next())
{
out.println(&quot;<tr>&quot;);
out.println(&quot;<td>&quot;+rst.getString(&quot;userName&quot;)+&quot;</td>&quot;);
out.println(&quot;<td>&quot;+rst.getInt(&quot;mobile&quot;)+&quot;</td>&quot;);
out.println(&quot;<td>&quot;+rst.getString(&quot;phone&quot;)+&quot;</td>&quot;);
out.println(&quot;<td>&quot;+rst.getString(&quot;mail&quot;)+&quot;</td>&quot;);
out.println(&quot;<td>&quot;+rst.getDate(&quot;lastcontact&quot;)+&quot;</td>&quot;);
out.println(&quot;<td>&quot;+rst.getString(&quot;mem&quot;)+&quot;</td>&quot;);
out.println(&quot;</tr>&quot;);
}
rst.close();
}
catch(Exception e)
{
}
%>
 
后退
顶部