在这段代码中,我将MySQL数据库的值放入数据库中表的String array(list[count])
和count=number
行。当我使用for-loop
在javascript的下拉列表中打印这些值时,使用j
变量,编译器无法识别j
。我无法理解为什么。请帮帮我。
<%@page import="java.sql.DriverManager"%>
<%@page import="com.mysql.jdbc.Driver"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.ResultSet"%>
<html>
<body>
<br/>
<br/>
<div align="center"><h1>Task Assignment</h1></div>
<form name="taskassignment"><div align="center"><table>
<tr><td><font size="5">Person(s)</font></td><td><select id="person"></select></td></tr>
<tr><td><font size="5">Task(s)</font></td><td><select name="task"></select></td></tr>
<tr><td></td><td><input type="submit" value="Assign" name="assigntask"/></td></tr></div></table></form>
<%
DriverManager.registerDriver(new Driver());
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/BI","root","mayur");
String query = "select name from records";
PreparedStatement stmt = conn.prepareStatement(query);
ResultSet rs = stmt.executeQuery();
int count =0;
while(rs.next())
{
count++;
}
String[] list = new String[count];
int i=0;
rs.beforeFirst();
while(rs.next())
{
list[i] = rs.getString(1);
out.println(list[i]);
i++;
}
%>
<script>
function myFunction()
{
for(var j=1;j<=<%=list.length%>;j++)
{
var x = document.getElementById("person");
var opt = document.createElement("option");
opt.text = '<%=list[j]%>';
opt.value = '<%=list[j]%>';
x.add(opt);
}
}
window.onload = myFunction;
</script>
</body>
</html>
答案 0 :(得分:0)
考虑这个jsp scriplet来填充你的选择。我用评论突出了注意事项。一旦你完成了这项工作,请务必阅读JSTL - Scriplets非常古老。
<select id="person">
<%
//no longer needed for 99% of jdbc drivers
DriverManager.registerDriver(new Driver());
//Dont EVER post realusernames/passwords!
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/BI","root","mayur");
String query = "select name from records";
PreparedStatement stmt = conn.prepareStatement(query);
ResultSet rs = stmt.executeQuery();
while(rs.next()) {
String item = rs.getString(1);
//now produce html select
%><option value='<%= item %>'><%= item %></option><%
}
rs.close ();
stmt.close ();
conn.close ();
%>
</select>
更多说明:
答案 1 :(得分:0)
嗨,Mayur我按照我的表格和一点点代码更改了查询。请根据要求进行修改。我在我的本地&amp;它正在添加所有的字段。它会工作,但正如Jan建议的那样也引用JSTL,因为scriptlet很难维护
<%@page import="java.sql.DriverManager"%>
<%@page import="com.mysql.jdbc.Driver"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.util.*"%>
<html>
<body>
<br/>
<br/>
<div align="center"><h1>Task Assignment</h1></div>
<%
String hiddenResult = "";
DriverManager.registerDriver(new Driver());
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/wiprodb","root","1234");
String query = "select state from state_tb";
PreparedStatement stmt = conn.prepareStatement(query);
ResultSet rs = stmt.executeQuery();
int count =0;
while(rs.next())
{
hiddenResult = hiddenResult+ (rs.getString(1))+",";
}
hiddenResult = hiddenResult.substring(0, hiddenResult.length()-1);
%>
<form name="taskassignment">
<input type="hidden" id="hiddenDDResult" name="hiddenDDResult" value="<%=hiddenResult%>" />
<div align="center">
<table>
<tr>
<td><font size="5">Person(s)</font></td>
<td><select id="person"></select></td>
</tr>
<tr>
<td><font size="5">Task(s)</font></td>
<td><select name="task"></select></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Assign" name="assigntask"/></td>
</tr>
</table>
</div>
</form>
<script>
function myFunction()
{
var ddResult = document.getElementById("hiddenDDResult").value;
var ddElement = document.getElementById("person");
var tempValue = ddResult.split(",");
//alert('ddResult'+ddResult);
for(var i=0;i<tempValue.length;i++)
{
var opt = document.createElement("option");
opt.text = tempValue[i];
opt.value = tempValue[i];
ddElement.add(opt);
}
}
myFunction();
</script>
</body>
</html>
答案 2 :(得分:-1)
未经测试,但下面的内容应该可以使用
<%@page import="java.sql.DriverManager"%>
<%@page import="com.mysql.jdbc.Driver"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.ResultSet"%>
<html>
<body>
<br/>
<br/>
<div align="center"><h1>Task Assignment</h1></div>
<form name="taskassignment"><div align="center"><table>
<tr><td><font size="5">Person(s)</font></td><td>
<select id="person">
<%
DriverManager.registerDriver(new Driver());
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/BI","root","mayur");
String query = "select name from records";
PreparedStatement stmt = conn.prepareStatement(query);
ResultSet rs = stmt.executeQuery();
int count =0;
while(rs.next())
{
count++;
}
String[] list = new String[count];
int i=0;
rs.beforeFirst();
while(rs.next())
{
list[i] = rs.getString(1);
out.println(list[i]);
%>
<option value='<%=list[i]%>'><%=list[i]%></option>
<%
i++;
}
%>
</select>
</td></tr>
<tr><td><font size="5">Task(s)</font></td><td><select name="task"></select></td></tr>
<tr><td></td><td><input type="submit" value="Assign" name="assigntask"/></td></tr></div></table></form>
</body>
</html>