jsp中下拉框中的值从数据库中动态获取

jsp中下拉框中的值从数据库中动态获取,第1张

jsp中把数据库全量查询后填充select列表:

举例说明:

<table>

<tr>

<td width="400px" align="left">入学批次:<SELECT NAME="grade"

id="grade" onchange="refreshEduLevelAndSpecialAjax()"> //选择入学批次会刷新层次专业

<OPTION VALUE="0">

--请选择--

<c:forEach items="${gradeInfo}" var="gradeInfo">

<OPTION VALUE="${gradeInfo.gradeName}">${gradeInfo.gradeName}

</c:forEach>

</SELECT></td>

<td width="400px" align="left">统考课程:<SELECT

NAME="uniExamCourseId" id="uniExamCourseId">

<OPTION VALUE="0">

--请选择--

<c:forEach items="${unifiedExamCourseList}" var="uniExamCourse">

<OPTION VALUE="${uniExamCourse.id}">${uniExamCourse.uniExamCourseName}

</c:forEach>

</SELECT></td>

</tr>

<tr>

<td colspan="2" id="refreshEduLevelAndSpecialAjax"> //设置ID,用于填充层次和专业的下拉框

<table>

<tr>

<td width="400" align="left">层 次:<SELECT

NAME="eduLevelId" id="eduLevelId"

onchange="refreshSpecialAjax()"> //选择层次后刷新专业

<OPTION VALUE="0">--请选择--</OPTION>

<c:forEach items="${educationLevel}" var="educationLevel">

<OPTION VALUE="${educationLevel.id}">${educationLevel.educationLevelName}

</c:forEach>

</SELECT></td>

<td width="400" align="left" id="refreshSpecialAjax">专业:<SELECT//设置ID,用于填充专业的下拉框

NAME="specialId" id="specialId">

<OPTION VALUE="0">--请选择--</OPTION>

<c:forEach items="${specialList}" var="special">

<OPTION VALUE="${special.id}">${special.specialName}

</c:forEach>

</SELECT></td>

</tr>

</table>

</td>

</tr>

</table>

java后台通过dao接口获取specialList的值,并且保存到session即可。

List<int>dt = new List<int>()

SqlConnection conn = new SqlConnection()

conn.ConnectionString = "Data Source=.\\SQLEXPRESSAttachDbFilename=|DataDirectory|\\Database1.mdfIntegrated Security=TrueUser Instance=True"

conn.Open()

string selStr = "select panel_id from fw_t_Sel_Img"

SqlCommand cmd = new SqlCommand(selStr, conn)

SqlDataReader reader = cmd.ExecuteReader()

while (reader.Read())

{

dt.Add(reader.GetInt32(0))

}

this.DropDownList1.DataSource = dt

this.DropDownList1.DataBind()

直接在页面前面连接数据库查询出结果result,然后下面就直接result里面取就可以了。

先声明(拿oracle举例):

<%!

String DBDRIVER = "oracle.jdbc.driver.OracleDriver"

String DBURL = "jdbc:oracle:thin:@XXX.XXX.XXX.XXX:1521:XXX"

String DBUSER = "XXX"

String DBPASSWORD= "XXX"

Connection con = null

PreparedStatement pstmt = null

ResultSet rs = null

%>

执行查询:

<%

String sql = "select * from XXX"

try

{

Class.forName(DBDRIVER)

con = DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD)

pstmt = con.prepareStatement(sql)

rs = pstmt.executeQuery()

%>

显示的时候如下:

<%

while(rs.next())

{

i++

int id=rs.getInt("id")

String title = rs.getString("admin")

String author = rs.getString("user")

%>

<td><%=admin%></td>

<td><%=user%></td>

大概代码差不多这样了,你看着凑凑


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/6425685.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-22
下一篇 2023-03-22

发表评论

登录后才能评论

评论列表(0条)

保存