又一个下拉列表

 大家好!
    我遇到的问题是:把全部币种代码从数据库中读取出来写入一个下拉框中,并且当选中某一币种代码时,其相应的币种名称从数据库中读出来显示到一个文本框中。
    “把全部币种代码从数据库中读取出来写入一个下拉框中”这一部分我已实现了,现在请教大家后半部分如何实现。
    我的开发环境是:myeclipse+Tomcat 5.x+mysql。
    我把已实现部分的代码写出来,请打大家在此基础上帮我实现后办部分。
一、.jsp文件

(1)-------->show.jsp


<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
   
    <title>My JSP 'show.jsp' starting page</title>
   
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">   
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

  </head>
 
  <body>
   <form action="servlet/ShowMemoServlet" method="post">
    <input type="submit" name="submit" value="显示" />
    </form>
   
  </body>
</html>





   (2)------>showdemos.jsp

<%@ page language="java" import="java.util.*,com.netcop.netcop12.showmemotwo.*"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"
+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
   
    <title>My JSP 'showmemos.jsp' starting page</title>
   
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">   
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

  </head>
 
  <body>
    <select name="bzdm">
     <%
    ArrayList<Memo> ms1 = null;
ms1 = (ArrayList)session.getAttribute("memolist");
       out.println("<option value=''>"+"请选择"+"</option>");
for(int i = 0; i < ms1.size(); i++){
Memo m = ms1.get(i);

out.println("<option value=''>"+m.getId()+"</option>");

       }
      
  %>

</select>
  </body>
</html>



说明:show.jsp页面上只有一个“显示“按钮,按后在showdemos.jsp页面上显示下拉框及其内容。




二、Java代码
(1)------->ShowMemoServlet.java   //Servlet


package com.netcop.netcop12.showmemotwo;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class ShowMemoServlet extends HttpServlet {


public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {


}


public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

HttpSession session = request.getSession();
System.out.println(session.getId());

Memo m = new Memo();
ArrayList<Memo> ms = m.getAllMemos();

session.setAttribute("memolist", ms);

response.sendRedirect("../showMemos.jsp");

}

}


(2)-------->Memo.java   //类

package com.netcop.netcop12.showmemotwo;
import java.util.ArrayList;

import java.sql.*;
public class Memo {
static{
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
}catch(ClassNotFoundException e){
e.printStackTrace();
}
}

public Memo(){}

public Memo(int id, String memo) {
super();
this.id = id;
this.memo = memo;
}

public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getMemo() {
return memo;
}
public void setMemo(String memo) {
this.memo = memo;
}


public ArrayList<Memo> getAllMemos(){
Connection  conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
ArrayList<Memo> ms = new ArrayList();

try{
conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORACLE","scott","tiger");
ps = conn.prepareStatement("SELECT * FROM Memos");
rs = ps.executeQuery();

while(rs.next()){
int id = rs.getInt("id");
String memo = rs.getString("memo");
ms.add(new Memo(id, memo));
}

rs.close();
ps.close();
conn.close();


}catch(SQLException e){
e.printStackTrace();
}

return ms;

}


private int id = 0;
private String memo = "";
}


三、数据库表

db.sql


CREATE TABLE Memos(
   id INTEGER PRIMARY KEY,
   memo varchar(20)
);

CREATE SEQUENCE MemosSeq;
INSERT INTO Memos VALUES(MemosSeq.nextval,'北京播报');
INSERT INTO Memos VALUES(MemosSeq.nextval,'奥运冠军');
INSERT INTO Memos VALUES(MemosSeq.nextval,'李东华');

 

 

 

 

 

方法二:  
   
  SelectData.java  
   
  package   beans;  
   
  import   java.sql.*;  
  public   class     SelectData    
  {  
  public   String     getSelectData(String   strSQL,String     dataField   )    
  {  
  String     optionData="";  
  try  
  {  
  Class.forName("org.gjt.mm.mysql.Driver");    
  Connection   con   =   DriverManager.getConnection("jdbc:mysql://localhost:3306/3dsoft","root","");    
  Statement   stmt   =   con.createStatement();    
  ResultSet   rs;      
  rs   =   stmt.executeQuery(strSQL);    
  while(rs.next())  
  {  
  optionData=optionData+"<option>"+rs.getString(dataField)+"</option><br>";      
   
  }  
  }  
  catch(SQLException   ex)    
  {  
  System.err.println("executeQuery:"+ex.getMessage());  
  return   "Some   Error   !";  
  }  
  catch(Exception   e)    
  {  
  System.err.println("executeQuery:"+e.getMessage());  
  return   "Some   Error   !";  
  }  
  return     optionData;  
   
  }  
   
  }  
   
   
   
   
  Jsp文件:  
   
  <jsp:useBean   id="selectData"   scope="session"   class="beans.SelectData"   />  
  .............  
   
  <select   name="storename"   size="1"   id="storename">  
  <%  
  String     strSQL2="select   storename   from   storeinfo"   ;  
  String     dataField2="storename";  
  out.println(selectData.getSelectData(strSQL2,dataField2   ))   ;  
  %>  
  </select>  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值