这是最终的代码:(目的是想实现:点击 查询后 如果输入框为空则什么都不做,并提醒用户。如果输入框有值的话,就将输入框的参数传到SQL对 pubs 中的titles 表做查询,结果以表格形式出现。)<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<html>
<body>
请输入type的值: 
<form name=frm1> 
<input type=text name=text1 ><br/>
<input type=button name=submit value="查询" onclick="a()">
</form>
<table borderColor="#888888" borderColorDark="#ffffff" cellSpacing="0"  cellPadding="2"  border="1">
<tr>
   <td><b>type</td>
   <td><b>pub_id</td>
   <td><b>price</td>
</tr>
<script language="javascript">
   function a(){
if(frm.text1.value.length == 0){
   alert("请输入数据!!")
}
else{
    <%Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance(); 
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs"; 
String user="jy"; 
String password="jy";
String type = request.getParameter("text1");
Connection conn= DriverManager.getConnection(url,user,password); 
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select type,pub_id,price from titles where type like '%"+type+"%'";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {%>
<tr>
   <td><%=rs.getString(1)%></td>
   <td><%=rs.getString(2)%></td>
   <td><%=rs.getString(3)%></td>
</tr>
<%}%> 
<%out.print("titles表的查询数据如下:");%>
<%rs.close();
stmt.close();
conn.close();
%> }
}
</script>
</table>
</body>
</html>
应该怎么改代码呢?

解决方案 »

  1.   

    写得好乱啊,主要是 function a()没有值就不提交,里面不用else
    其他的用jsp判断有没有传过来参数就可以了.
    有参数就查询,循环输出.
      

  2.   

    楼主没有搞明白jsp的运行哦,jsp是在服务器运行滴,浏览器上面看见滴是html格式滴
      

  3.   

    可以的。不过jsp是在服务器运行的,js是的客户端运行的。可以动态的给js的变量赋值。
      

  4.   

    可以加java代码啊!
    这都不知道?
      

  5.   

    先验证,再传给后台.
    1\改一下form的属性
    <form name="changepwd" onSubmit="return a();" action="你网页的名字" method="post">
    2\再改一下.javascript
    <script language="javascript">
       function a(){
    if(frm.text1.value.length == 0){
       alert("请输入数据!!")
       return false;
    }  return true;
    }
    这是完成验证的.3/再写java
    <%Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance(); 
    String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs"; 
    String user="jy"; 
    String password="jy";
    String type = request.getParameter("text1");
    Connection conn= DriverManager.getConnection(url,user,password); 
    Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
    String sql="select type,pub_id,price from titles where type like '%"+type+"%'";
    ResultSet rs=stmt.executeQuery(sql);
    while(rs.next()) {%>
    <tr>
       <td><%=rs.getString(1)%></td>
       <td><%=rs.getString(2)%></td>
       <td><%=rs.getString(3)%></td>
    </tr>
    <%}%> 
    <%out.print("titles表的查询数据如下:");%>
    <%rs.close();
    stmt.close();
    conn.close();
    %> 
    不要把javascript和java写在一起.
      

  6.   

    <%@ page contentType="text/html;charset=gb2312"%>
    <%@ page import="java.sql.*"%>
    <%
    String type = request.getParameter("text1");
    ResultSet rs = null;
    Connection conn= null;
    Statement stmt=null;
    if(type != null){
    Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance(); 
    String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs"; 
    String user="sa"; 
    String password="sa";conn= DriverManager.getConnection(url,user,password); 
    stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
    String sql="select type,pub_id,price from titles where type like '%"+type+"%'";
    rs=stmt.executeQuery(sql);
    }%>
    <html>
    <head>
    <title>test</title>
    </head>
    <script language="javascript">
    function a(){
     if(frm1.text1.value.length == 0){
        alert("请输入数据!!")
        return false;
      }
      alert(document.frm1.action);
      document.frm1.submit();
    }
    </script>
    <body>
    <form name="frm1" action="testdb.jsp" method="post"> 
    请输入type的值:<input type=text name=text1 ><br/>
    <input type=button name=submit1 value="查询" onclick="a()">
    </form>
    <table borderColor="#888888" borderColorDark="#ffffff" cellSpacing="0"  cellPadding="2"  border="1">
    <tr>
       <td><b>type</td>
       <td><b>pub_id</td>
       <td><b>price</td>
    </tr>
    <%if(rs != null){
    out.print("titles表的查询数据如下:");
    while(rs.next()){%>
    <tr>
       <td><%=rs.getString(1)%></td>
       <td><%=rs.getString(2)%></td>
       <td><%=rs.getString(3)%></td>
    </tr>
    <%}
    rs.close();
    stmt.close();
    conn.close();
    }
    %> 
    </table>
    </body>
    </html>
      

  7.   

    把它保存为:testdb.jsp
    与<form name="frm1" action="testdb.jsp" method="post"> 中的action值为一致。
    原来程序中<input type=button name=submit value="查询" onclick="a()">有问题:
    name名字不能定义为html的关键字。submit为html中的关键字
      

  8.   

    String user="sa"; 
    String password="sa";
    改为:
    String user="jy"; 
    String password="jy";