sun7bear
发贴: 16
积分: 0
|
于 2006-05-07 12:06
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
|