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="text/html;
charset=gb2312"
language="java"
import ="java.sql.*,java.io.*"%>
<jsp:useBean id="contact"
class="ch12.ContactBean"
scope="page"/>
使用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("LiuJun"
while(rst.next())
{
out.println("<tr>"
out.println("<td>"+rst.getString("userName"+"</td>"
out.println("<td>"+rst.getInt("mobile"+"</td>"
out.println("<td>"+rst.getString("phone"+"</td>"
out.println("<td>"+rst.getString("mail"+"</td>"
out.println("<td>"+rst.getDate("lastcontact"+"</td>"
out.println("<td>"+rst.getString("mem"+"</td>"
out.println("</tr>"
}
rst.close();
}
catch(Exception e)
{
}
%>
程序执行时,应该会返回LiuJun 1338137 01084473962 delphipig@163.com 2005/05/27 other email:liujun@jumptone.com.cn
这样的结果,
而程序居然没有任何返回结果,请大家帮忙看看是哪的错?谢谢.
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="text/html;
charset=gb2312"
language="java"
import ="java.sql.*,java.io.*"%>
<jsp:useBean id="contact"
class="ch12.ContactBean"
scope="page"/>
使用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("LiuJun"
while(rst.next())
{
out.println("<tr>"
out.println("<td>"+rst.getString("userName"+"</td>"
out.println("<td>"+rst.getInt("mobile"+"</td>"
out.println("<td>"+rst.getString("phone"+"</td>"
out.println("<td>"+rst.getString("mail"+"</td>"
out.println("<td>"+rst.getDate("lastcontact"+"</td>"
out.println("<td>"+rst.getString("mem"+"</td>"
out.println("</tr>"
}
rst.close();
}
catch(Exception e)
{
}
%>
程序执行时,应该会返回LiuJun 1338137 01084473962 delphipig@163.com 2005/05/27 other email:liujun@jumptone.com.cn
这样的结果,
而程序居然没有任何返回结果,请大家帮忙看看是哪的错?谢谢.