---
title: "第七篇：数据库程序员篇"
author: "fanjava"
date: "2004-06-24T07:06:00Z"
tags: []
slug: "untitled"
draft: false
lastmod: "2004-06-24T07:06:00Z"
id: 602085
url: "https://fanjava.iteye.com/blog/602085"
---

**开场白：**看你还记得不记得我在“第五篇”中的把“execute这个方法名该为“myDog”可以吗？现在你还不可以，以后我会告诉你怎么可以的啦。”

**程序要求：**程序开始，第一个页面要显示，以这样的信息，一个下拉框用来选择“用户的部门名称”，这个下拉框中的内容要从“部门信息表中取得”（当页面一生成的时候就要获得）。然后按下“提交”按钮，能显示所有该部门的员工。

**分析：**因为页面一生成的时候就要从“部门表”中获得数据，所以我要采用一种方式让页面生成的时候就能操作action获得数据。数据的显示不再是单一数据，所以在页面上设计到循环。显示出来的一项数据不再是一个数据。

建立数据表：还记得我们在上一篇中建立的数据库吗。

数据库名字叫“user”已经有一个表叫“userTable”，我们现在就在这个数据库中再建立一个

**表“depatTable”**

部门号码 depatNo 文本

部门名称 depatName 文本

**填入几个值：&lt;?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /&gt;**

001 生产部

002 工程部

003 人事部

**程序开始**

从现在开始，我们将按照真正项目开发的思想来做，模块的划分将细致起来

A模块：提供数据库连接

B模块：actionAndForm模块

C模块: 业务逻辑,提供数据的操作

D模块: 数据实体封装

这些模块实际上就是“包”的概念，在eclipse中建立“包”我就不想说了

**A模块(2个类)——package** dataConnectFactory

**ConnectFactory类**

//数据连接工厂

**package** dataConnectFactory;

**import** java.sql.\*;

**public** **class** ConnectFactory

{

//定义数据库的驱动程序（以下给出的是连接odbc数据库的）

**private** **static** String strDriver="sun.jdbc.odbc.JdbcOdbcDriver";

//定义数据库的URL（名称）

**private** **static** String strConnection="jdbc:odbc:user";

//数据访问的用户名

**private** **static** String strUsername="";

//数据库访问的密码

**private** **static** String strPassword="";

**public** **static** Connection getConnectionByDriver()

**throws** MyException

{

Connection conn = **null**;

**try**

{

Class.forName(strDriver);//注册驱动

conn = //获得连接

DriverManager.getConnection( strConnection,

strUsername,

strPassword);

}

**catch**(ClassNotFoundException ex1)

{

ex1.printStackTrace();

**throw** **new** MyException("Class Not Found!");

}

**catch**(SQLException ex2)

{

ex2.printStackTrace();

**throw** **new** MyException("SQL Error");

}

**finally** {}

**return** conn;

}

}

**MyException****类**

**package** dataConnectFactory;

//定义自己的异常类

**public** **class** MyException **extends** Exception

{

**public** MyException()

{

**super**();

}

**public** MyException(String message)

{

**super**(message);

}

}

**B模块(3个类)——package** action

**UserAction类**

**package** action;

//当第一个查询页面一生成的时候就把部门的信息得到并交给页面

**import** javax.servlet.http.HttpServletRequest;

**import** javax.servlet.http.HttpServletResponse;

**import** org.apache.struts.action.ActionError;

**import** org.apache.struts.action.ActionErrors;

**import** org.apache.struts.action.ActionForm;

**import** org.apache.struts.action.ActionForward;

**import** org.apache.struts.action.ActionMapping;

**import** org.apache.struts.actions.DispatchAction;

**import** biz.SearchBiz;

**import** entity.\*;

**public** **class** UserAction **extends** DispatchAction

{

//转到查询界面

**public** ActionForward toSearch( ActionMapping mapping,

ActionForm form,

HttpServletRequest request,

HttpServletResponse response)

**throws** Exception

{

//定义错误信息封装

ActionErrors errors = **null**;

//获取页面表单信息

UserForm uf = (UserForm)form;

//定义部门实体数组

DepartmentEntity\[] departs = **null**;

SearchBiz searchBizDept = **null**;

**try**

{

searchBizDept = **new** SearchBiz();

departs =

searchBizDept.selectDeptEntitys(

**new** DepartmentEntity());

uf.setDeparts(departs);

}

**catch** (Exception e)

{

errors = **new** ActionErrors();

e.printStackTrace();

errors.add(ActionErrors.GLOBAL\_ERROR, **new** ActionError("system.exception"));

**return** mapping.findForward("error");

}

**finally**

{

**if** (errors != **null**)

{

saveErrors(request, errors);

}

}

**return** mapping.findForward("toSearch");

}

}

**package** action;

**UserForm类**

//将所有的实体封装到form中

**import** org.apache.struts.action.ActionForm;

**import** entity.\*;

**public** **class** UserForm **extends** ActionForm

{

**private** UserEntity user = **new** UserEntity();

**private** UserEntity\[] users = **null**;

**private** DepartmentEntity depart = **new** DepartmentEntity();

**private** DepartmentEntity\[] departs = **null**;

**public** DepartmentEntity getDepart()

{

**return** depart;

}

**public** DepartmentEntity\[] getDeparts()

{

**return** departs;

}

**public** UserEntity getUser() {

**return** user;

}

**public** UserEntity\[] getUsers()

{

**return** users;

}

**public** **void** setDepart(DepartmentEntity depart)

{

**this**.depart = depart;

}

**public** **void** setDeparts(DepartmentEntity\[] departs)

{

**this**.departs = departs;

}

**public** **void** setUser(UserEntity user)

{