Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java EE 综合讨论区  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 请教一个关于JNDI的问题??
sun7bear





发贴: 16
积分: 0
于 2006-05-07 12:06 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
ContactBean.java:
package com.jspdev.page;

import java.util.*;
import com.jspdev.util.*;
import java.sql.*;

public class ContactBean {
private Connection conn;
Vector v;
public ContactBean() throws Exception {
11conn = DatabaseConn.getConnection();
v = new Vector();
}
public int getAvailableCount() throws Exception {
int ret = 0;
Statement stmt = conn.createStatement();
String strSql = "select count(*) as num from contact";
ResultSet rset = stmt.executeQuery(strSql);
while (rset.next()) {
ret = rset.getInt("num");
}
return ret;
}

public PageBean listData(String page) throws Exception {
try {
PageBean pageBean = new PageBean(this);
int pageNum = Integer.parseInt(page);
Statement stmt = conn.createStatement();

String strSql = "select top" + pageNum * pageBean.rowsPerPage
+ "* from contact order by userName";
ResultSet rset = stmt.executeQuery(strSql);
int i = 0;
while (rset.next()) {
if (i > (pageNum - 1) * pageBean.rowsPerPage - 1) {
Object[] obj = new Object[6];
obj[0] = rset.getString("userName");
obj[1] = new Integer(rset.getInt("mobile"));
obj[2] = rset.getString("phone");
obj[3] = rset.getString("mail");
obj[4] = rset.getDate("lastcontact");
obj[5] = rset.getString("mem");
v.add(obj);
}
i++;
}
rset.close();
stmt.close();
pageBean.curPage = pageNum;
pageBean.data = v;
return pageBean;
} catch (Exception e) {
e.printStackTrace();
throw e;
} finally {

}
}

public Vector getResult() throws Exception {
return v;
}
public static void main(String[] args) {
int j = 0;
try {
67ContactBean d = new ContactBean();
j = d.getAvailableCount();
System.out.println(j);
} catch (Exception e) {
e.printStackTrace();
}
}
}
DatabaseConn.java:
package com.jspdev.util;

import java.sql.*;
import javax.naming.Context;
import javax.sql.DataSource;
import javax.naming.InitialContext;

public class DatabaseConn {

/**
* @param args
*/
public static synchronized Connection getConnection() throws Exception {

try {
Context initCtx = new InitialContext();
17Context envCtx = (Context) initCtx.lookup("java:comp/env");
DataSource ds = (DataSource) envCtx.lookup("jdbc/DBPool");
return ds.getConnection();
} catch (SQLException e) {
throw e;
} catch (Exception e) {
throw e;
}
}
}
运行ContactBean.java怎么报如下错误呢?
javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:645)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:247)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:284)
at javax.naming.InitialContext.lookup(InitialContext.java:351)
at com.jspdev.util.DatabaseConn.getConnection(DatabaseConn.java:17)
at com.jspdev.page.ContactBean.<init>(ContactBean.java:11)
at com.jspdev.page.ContactBean.main(ContactBean.java:67)


why edited on 2006-05-07 19:17

作者 Re:请教一个关于JNDI的问题?? [Re:sun7bear]
sunjavaduke

乒乓球国手-张怡宁



发贴: 176
积分: 6
于 2006-05-07 14:56 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
这个文件不是直接用来运行的
是要放在bean里的



-----------------------------------------------------------------
icd.Neusoft Co,.Ltd.
mail:zhangzhongl@neusoft.com
tel:13591718127
QQ:176932855
------------------------------------------------------------------
作者 Re:请教一个关于JNDI的问题?? [Re:sun7bear]
sun7bear





发贴: 16
积分: 0
于 2006-05-07 15:57 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
PageBean.java:
package com.jspdev.page;
import java.util.Vector;
public class PageBean {

  /**
   * @param args
   */
  public int curPage; // 当前是第几页

  public int maxpage; // 一共有多少页

  public int maxRowCount; // 一共有多少行

  public int rowsPerPage = 5; // 每页多少行

  public Vector data; // 本页要显示的资料

  public void countMaxPage() {// 根据总行数计算总页数

    if (this.maxRowCount % this.rowsPerPage == 0) {
      this.maxpage = this.maxRowCount / this.rowsPerPage;
    } else {
      this.maxpage = this.maxRowCount / this.rowsPerPage + 1;
    }
  }

  public Vector getResult() {
    return this.data;
  }

  public PageBean(ContactBean contact) throws Exception {
    this.maxRowCount = contact.getAvailableCount();// 得到总行数
    this.data = contact.getResult();
    this.countMaxPage();
  }

}
contact.jsp:
<jsp:useBean id="pageCtl" class="com.jspdev.page.PageBean" scope="request"/>
<table border=1>
<% java.util.Vector v = pageCtl.getResult();
java.util.Enumeration e = v.elements();
while(e.hasMoreElements())
{
Object[] obj=(Object[])e.nextElement();
%>
<tr>
<td align="center" width="95"><%= obj[0] %></td>
<td align="center" width="93"><%= obj[1]%></td>
<td align="center" width="71"><%= obj[2] %></td>
<td align="center" width="142"><%= obj[3] %></td>
<td align="center" width="142"><%= obj[4] %></td>
<td align="center" width="142"><%= obj[5] %></td>
</tr>
<%}%>
</table>
<%if(pageCtl.maxpage !=1){%>
<form name="PageForm" action="/page/servlet/contactservlet" method="post">
<%@ include file="/pageman.jsp"%>
</form>
<%}%>
可是我放在PageBean中,然后运行contact.jsp,怎么报如下错误呢?
org.apache.jasper.JasperException: /contact.jsp(1,1) The value for the useBean class attribute com.jspdev.page.PageBean is invalid.
  org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:510)
  org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:375)
  org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
  org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
  javax.servlet.http.HttpServlet.service(HttpServlet.java:802)



作者 Re:请教一个关于JNDI的问题?? [Re:sun7bear]
sun7bear





发贴: 16
积分: 0
于 2006-05-09 18:07 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
怎么无人问津列



flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923