jsp创建动态下拉列表

madeyinzy521 2004-09-16 05:07:27
有一数据表,根据数据表的数据创建一下拉列表,下拉列表的数据为数据库中的表数据,请各个大侠,本人为新学,最好有原码,不胜感激!
...全文
662 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
weicairong 2004-09-16
  • 打赏
  • 举报
回复
太简单了,还不自己写,受不了了
redlaputa 2004-09-16
  • 打赏
  • 举报
回复
package tics;

import java.util.*;
import java.sql.*;
import java.io.*;
import javax.naming.*;
import javax.sql.*;
import org.apache.log4j.Logger;

/**
* 连接数据库,取得VATCODE,更改SB_FB提交标识,执行SQL语句(查询或更改)
*/
public class DbConnection {
Connection conn = null;
Statement stmt = null;
ResultSet rset = null;
static Logger logger = Logger.getLogger("tics.db");

public DbConnection() { }


/************************************************
* 打开数据库连接
* 通过命名为etaxDS的DataSource连接数据库
************************************************/
public boolean openConnection() {
try {
Context ctx = new InitialContext();
javax.sql.DataSource ds = (javax.sql.DataSource)ctx.lookup("ticsDS");
this.conn = ds.getConnection();
logger.debug("openConnection ok") ;
//this.conn = DriverManager.getConnection ("jdbc:WebLogic:pool:etax Connection Pool ",null);
} catch (Exception e) {
logger.error("打开数据库连接错误!" + e.getMessage());
return false;
}
return true;
}

/*************************************************
* executeQuery and executeUpdate
* query and update DB
*************************************************/
public ResultSet executeQuery(String query) throws SQLException
{
//ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE
stmt = conn.createStatement();
rset = stmt.executeQuery(query);
logger.debug("executeQuery(" + query + ")");
return rset;
}

public ResultSet executeScrollQuery(String query) throws SQLException
{
//ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
rset = stmt.executeQuery(query);
logger.debug("executeScrollQuery(" + query + ")");
return rset;
}

public void executeUpdate(String query) throws SQLException {
stmt = conn.createStatement();
stmt.executeUpdate(query);
logger.debug("executeUpdate(" + query + ")");
if (stmt != null)
stmt.close();
}

public void rsclose() throws SQLException {
//if(rset != null) rset.close();
//rset = null;
if (stmt != null)
stmt.close();
stmt = null;
logger.debug("rsclose ok");
}

public void close() throws SQLException {
//if(rset != null) rset.close();
if (stmt != null)
stmt.close();
if (conn != null)
conn.close();
logger.debug("close ok");
}

protected void finalize() throws Throwable {
close();
}

public static void main(String [] args) {
//Design for test purpose.
//DbConnection dc = new DbConnection();
//System.out.println(dc.openConnection());
}
}
redlaputa 2004-09-16
  • 打赏
  • 举报
回复
<select name="CKKA" id="CKKA">
<option value="NULL" selected>不选</option>
<%
DbConnection dbconn=new DbConnection();
if(dbconn.openConnection())
{
try
{
String sql="select * from DM_CKTS_HGGB";
ResultSet rs=dbconn.executeQuery(sql);
while(rs.next())
{
out.println("<option value="+rs.getString("HGGB_DM")+">"+rs.getString("HGGB_MC")+"</option>");
}
}catch(SQLException e)
{
e.printStackTrace();
}finally
{
try
{
dbconn.rsclose();
dbconn.close();
}catch(Exception e)
{
e.printStackTrace();
}
}
}
%>
</select>
cgliang00 2004-09-16
  • 打赏
  • 举报
回复
能给出DBUtil.java这个封装数据库的类的代码吗?大家一起学习!!
天涯柳絮 2004-09-16
  • 打赏
  • 举报
回复
yes,就是这个了!
okkoxiong8025 2004-09-16
  • 打赏
  • 举报
回复
最简单的jsp+Html下拉
<select name="H11" >
<option value='' selected></option>
<%
String sql="select * from Table ";
ResultSet rs=DBUtil.exeQuery(sql);
while(rs.next()){
%>
<option value='<%=rs.getString("ID")%>'><%=rs.getString("VALUE")%></option>
<%
}

rs.close();

%>
</select>
xunyiren 2004-09-16
  • 打赏
  • 举报
回复
<select id=oSelect style="width:240px;">
</select>
<SCRIPT LANGUAGE="JavaScript">
<!--
for (i=0;i<10;i++) {
var oOption = document.createElement("option");
oSelect.options.add(oOption);
oOption.innerText=oOption.Value="sText"+i;
}
//-->
</SCRIPT>

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧