各位朋友,帮帮忙,我刚学JSP一个星期,很多都搞不懂,能不能帮我看一下下面这段程序,数据库是没问题的,但是我点来点去都没反应?不知道为什么,数据库里有一个数据是这样的,ID=1001也就是说,先选择ID再输入1001进行查询,但是没有结果,不知道是不是程序出错了??
<%@ page contentType="text/html;charset=gb2312" import="java.sql.*"%>
<%!
public String codeToString(String str)
{//处理中文字符串的函数
String s=str;
try
{
byte tempB[]=s.getBytes("ISO-8859-1");
s=new String(tempB);
return s;
}
catch(Exception e)
{
return s;
}
}
%>
<html>
<script language="javascript">
function on_submit()
{
if(form1.select.value=="")
{
alert("请输入要查询的资料");
form1.select.focus();
return false;
}
}
</script>
<body>
<form name="form1" action="MyDatabase.jsp" method="post" onsubmit="return on_submit()">
<select name="num">
<option value="ID">ID</option>
<option value="NAME">NAME</option>
<option value="CALL">CALL</option>
<option value="Address">Address</option>
</select>
<input type="text" name="select" size="30">
<input type="submit" value="提交">
</form>
<%Statement stmt=null;
Connection conn=null;
ResultSet rs=null;
String selectString=codeToString(request.getParameter("select"));
String selectMethod=request.getParameter("num");
String SqlString="select * from text where "+selectMethod+"="+selectString;//注意where后面有空格
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection("jdbc:mysql://localhost/yqfang","root","");
stmt = conn.createStatement();
rs = stmt.executeQuery(SqlString);
out.println("<center>"+"访问数据库所得到的信息如下:"+"</center>"+"<hr><p>");
out.println("<center><table border=1>");
while(rs.next())
{
out.println("<tr><td>"+rs.getString(1)+"</td>");
out.println("<td>"+rs.getString(2)+"</td>");
out.println("<td>"+rs.getString(3)+"</td>");
out.println("<td>"+rs.getString(4)+"</td></tr>");
}
out.println("</center></table>");
conn.close();
stmt.close();
rs.close();
}
catch(Exception e)
{
e.printStackTrace();
}
%>
</body>
</html>