专注于互联网--专注于架构

最新标签
网站地图
文章索引
Rss订阅

首页 »Java教程 » mysqlnetbeans:在NetBeans中使用MySQL创建简单Web应用程序( 2) »正文

mysqlnetbeans:在NetBeans中使用MySQL创建简单Web应用程序( 2)

来源: 发布时间:星期四, 2009年1月8日 浏览:50次 评论:0
  实现逻辑层  既然数据层已经准备就绪我们开始在适当位置放置 Java 类逻辑层由 3个类组成: SubjectName.java、SubjectCounselor.java 和 AccessDB.java这几个类提供两种功能;它们通过响应数据请求(SubjectName.java 和 SubjectCounselor.java)连接 JSP 页面并通过执行基于用户指定信息(AccessDB.java)查询和数据库进行连接我们介绍下这两种功能:

  1.    连接 JSP 页面

  2.    连接数据库

  连接 JSP 页面SubjectName.java  SubjectName.java 使 index.jsp 页面能够访问 subjects 表中列出主题名称它通过允许 AccessDB.java 使用 ter 思路方法设置例子变量 id 和 name执行此操作然后让 index.jsp 使用公共 getter 思路方法访问它们要设置 SubjectName.java执行以下操作:

  在 Projects 窗口中右键单击该项目节点并选择 New > Java Class...打开 New Java Class 向导

  在 Class Name 文本字段中输入 SubjectName还应该创建个新包包含项目所有 Java 类对于 Package键入 org单击 Finish新类模板在 Source Editor 中打开在 Projects 窗口中新包和类节点在 Source Packages 中显示出来

  现在在 Source Editor 中新建模板里将以下内容添加到新 SubjectName 类主体部分然后保存(Ctrl+S)文件:

private String id;
private String name;
// create ter methods
public void Id(String id){
  this.id=id;
}
public void Name(String name){
  this.name=name;
}
// create getter methods
public String getId{
   id;
}
public String getName{
   name;
}
     SubjectCounselor.java  根据从 index.jsp 窗体接收到 subject_id 值SubjectCounselor.java 能够使 response.jsp 页面访问数据库中主题和顾问详细信息像 SubjectName.java 该类通过允许 AccessDB.java 使用公共 ter 思路方法设置所有例子变量来实现此操作然后让 response.jsp 使用 getter 思路方法访问它们要设置 SubjectCounselor.java执行以下操作:

  在 Projects 窗口中再次右键单击该项目节点并选择 New > Java Class...打开 New Java Class 向导

  在 Class Name 文本字段中输入 SubjectCounselor单击 Finish新类模板在 Source Editor 中打开在 Projects 窗口中新类节点显示在我们的前创建 org 包下

  现在在 Source Editor 中新建模板里将以下内容添加到新 SubjectCounselor 类中主体部分然后保存(Ctrl+S)文件:

private String subjectName;
private String description;
private String counselorID;
private String firstName;
private String nickName;
private String lastName;
private String telephone;
private String email;
private String memberSince;
// create ter methods
public void SubjectName(String subject) {
  this.subjectName=subject;
}
public void Description(String desc) {
  this.description=desc;
}
public void CounselorID(String conId) {
  this.counselorID=conId;
}
public void FirstName(String first) {
  this.firstName=first;
}
public void NickName(String nick) {
  this.nickName=nick;
}
public void LastName(String last) {
  this.lastName=last;
}
public void Telephone(String phone) {
  this.telephone=phone;
}
public void Email(String email) {
  this.email=email;
}
public void MemberSince(String mem){
  this.memberSince=mem;
}
// create getter methods
public String getSubjectName {
   subjectName;
}
public String getDescription {
   description;
}
public String getCounselorName {
  String counselorName=firstName + " "
    + nickName + " " + lastName;
   counselorName;
}
public String getMemberSinceDate {
   memberSince;
}
public String getTelephone {
   telephone;
}
public String getEmail {
   email;
4.       }
连接数据库AccessDB.java  我们可以通过将任务各个击破方式来编码 AccessDB.java:它必须和数据库连接发送用户指定查询然后存储从查询中接收数据我们以片段方式处理 AccessDB.java 编码所以您可以在此下载工作版本 AccessDB.java这样如果在某个点感到迷惑可以将您代码和保存版本进行对比

  首先创建文件然后准备代码该代码能使类建立和上面定义连接池连接:

  在 Projects 窗口中右键单击该项目节点并选择 New > Java Class...打开 New Java Class 向导

  在 Class Name 文本字段中输入 SubjectName单击 Finish新类模板在 Source Editor 中打开在 Projects 窗口中新类节点显示在我们的前创建 org 包下

  现在在 Source Editor 中新建模板里右键单击画布并选择 Enterprise Resources > Use Database在 Choose Database 对话框中选择我们的前从 Data Source 下拉列表中定义数据源 JNDI 名称确保选择了 Generate Inline Lookup Code 单选按纽并单击 Finish在 Source Editor 中生成以下代码:

private DataSource getJdbcConnectionPool throws NamingException {
  Context c= InitialContext;
   (DataSource) c.lookup("java:comp/env/jdbc/connectionPool");
}
  这允许该类和使用的前在 Web 服务器上定义连接池数据库连接另外注意在创建 getJdbcConnectionPool 思路方法时IDE 在 Source Editor 中自动生成导入语句用于 javax.sql 和 javax.naming API 以下类:

  现在可以开始编码该类对两个 JSP 页面执行数据库查询了我们首先查询 index.jsp它涉及检索列在 subjects 表中所有主题姓名和 ID:

  在 AccessDB.java Source Editor 中将以下串变量输入到类声明下面区域中:

private String sqlSubjectName="SELECT subjects_id, name FROM subjects";

  SQL 查询允许我们指定想要从数据库中引出数据

  现在将以下 getSubjectName 思路方法添加到类中这基本上创建了和连接池连接然后和数据库连接并执行查询它然后在数据库返回结果集中循环并创建 SubjectName 例子保留每个返回行 ID 和姓名每个例子都被添加到列表中最后通过思路方法被返回至对象:

// get subject names from database
public List getSubjectName throws Exception{
  // connection instance
  Connection connection=null;
  // instance of SubjectName used to retain retrieved data
  SubjectName subName=null;
  // list to hold all instances of SubjectName
  List list= ArrayList;
  try {
    // connect to database
    DataSource dataSource=getJdbcConnectionPool;
    connection=dataSource.getConnection;
    // prepare the SQL query to get subject name and id
    PreparedStatement stat=connection.prepareStatement(sqlSubjectName);
    // up the result to retain all queried data
    ResultSet rs=stat.executeQuery;
    // now loop through the rows from the generated result
    while(rs.next){
      // declare an instance of SubjectName to match
      // ed data with ' instance variables
      subName= SubjectName;
      String subject_id=rs.getString("subjects_id");
      String name=rs.getString("name");
      // the data to the variables
      subName.Id(subject_id);
      subName.Name(name);
      // finally, add the subName instance to the list
      list.add(subName);
    }
  } catch(Exception e){
    .out.prln(e.getMessage);
  // close the connection so it can be ed to
  // the connection pool then the list
  } finally{
    connection.close;
     list;
  }
}
      在为 getSubjectName 思路方法添加以上代码时注意出现在 Source Editor 中各种加下划线红色文本将光标放在有下划线文本中时在该行最左边会显示个灯泡图标这表示 IDE 可以为提供可能解决方案

  通过单击灯泡图标或按下 Alt-Enter 打开相应工具提示修正所有:

  我们将探讨5种并且它们都涉及将类(和接口)导入项目中:

  m     用作思路方法返回对象 List 接口需要从 java.util 中定义

  m     ArrayList 类被用于实现 List并且我们需要该类保存 SubjectName 所有例子

  m     和数据库通信需要来自 java.sql API Connection、PreparedStatement 和 ResultSet

  为这些个从工具提示中选择 Add Import并且注意在 Source Editor 中自动生成新导入语句:

  现在可以为来自 response.jsp 查询添加代码这和的前演示 index.jsp 思路方法相同例如创建合适 SQL 查询然后创建个查询数据库和保存数据思路方法然而在这种情况下需要创建两个查询:第个访问 subjects 表并检索用户从 index.jsp 下拉菜单中所选 ID 相应行然后通过使用返回主体行中 counselors_idfk第 2个查询可以匹配 counselors 表中 counselor ID:

  在 AccessDB.java Source Editor 中将以下两个串变量输入到类声明以下区域中:

private String sqlSubject="SELECT * FROM subjects WHERE subjects_id = ?";
private String sqlCounselor="SELECT * FROM counselors WHERE counselors_id = ?";
  现在将以下 getSubCounselor 思路方法添加到类中这会创建和连接池连接然后如上所述连接到数据库并执行两个查询然后它创建个 SubjectCounselor 例子来保留从这两个结果集中获得所有数据:

// get subject data and counselor data for corresponding subject
public SubjectCounselor getSubCounselor(String subjectID) throws Exception{
  // instance of SubjectCounselor used to retain data
  SubjectCounselor subCon= SubjectCounselor;
  // connection instance
  Connection connection=null;
  try {
    // connect to database
    DataSource dataSource=getJdbcConnectionPool;
    connection=dataSource.getConnection;
    // prepare the SQL query to get subject data
    PreparedStatement stat=connection.prepareStatement(sqlSubject);
    stat.String(1, subjectID);
    ResultSet rs=stat.executeQuery;
    // this assumes there is _disibledevent=rs.getString("member_since");
    // finally all variables to their
    // equivalents in the SubjectCounselor instance
    subCon.SubjectName(subjectName);
    subCon.Description(description);
    subCon.CounselorID(counselorID);
    subCon.FirstName(firstName);
    subCon.NickName(nickName);
    subCon.LastName(lastName);
    subCon.Telephone(telephone);
    subCon.Email(email);
    subCon.MemberSince(memberSince);
  } catch(Exception e){
    .out.prln(e.getMessage);
  } finally{
    // close the connection so it can be ed to the
    // connection pool then the SubjectCounselor instance
    connection.close;
     subCon;
  }
}
  现在 AccessDB.java 需要代码已经添加完成并且使用它完成了实现逻辑层所需所有步骤可能在继续下步的前想要将您 AccessDB.java 版本和可下载版本相比较剩下任务就是将 JSP 代码添加到 Web 页面中使我们能够显示在 SubjectName 和 SubjectCounselor 例子中保留数据



  实现演示层  现在我们返回到的前在本教程中创建 index.jsp 和 response.jsp 占位符添加 JSP 代码使我们页面能动态地生成内容如基于用户输入生成内容要实现这个操作需要采取以下3个步骤:

  1.    向项目编译类路径中添加 JSTL Library

  2.    将taglib 指令添加到 JSP 页面

  3.    添加代码

  向项目编译类路径中添加 JSTL Library  为了能让 JSP 资源更好地为我们所用我们将使用JavaServer Pages Standard Tag Library (JSTL) 中标记访问并显示从逻辑层所得数据该库和 IDE 捆绑在因此我们需要确保将 JSTL 库添加到了 Web 项目编译类路径中然后向每个 JSP 页面添加相关 taglib 指令这样我们使用服务器就能够在从 JSP 页面中读取标记时识别它们根据您使用是 Tomcat 还是 SJSAS执行以下操作:

  捆绑 Tomcat Web Server  在 Projects 窗口中右键单击该项目 Libraries 节点并选择 Add Library选择 JSTL 1.1 库并单击 Add Library

  现在展开 Libraries 节点而且您应该看到两个新节点:个是 JSTL 库 standard.jar 文件个是库 jstl.jar 文件:

  Sun Java Application Server  不执行任何操作!我们不需要采取任何措施将 JSTL 库添加到项目编译类路径中这是 JSTL 库已经包含在应用服务器库中您可以通过展开 Libraries > Sun Java Application Server 节点验证这点:appserv-jstl.jar 节点在 JSTL 库中定义所有标准标记



0

相关文章

读者评论

发表评论

  • 昵称:
  • 内容: