button中的代码在eclipse中可以运行,而且在网页中alert("你好!");也有运行,为什么中间那段没运行了<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript">
  function button(){
     // document.getElementById('useraddL').submit();
      Class.forName("org.gjt.mm.mysql.Driver"); //驱动程序你自己的,我的是com.mysql.jdbc.Driver
  String url="jdbc:mysql://localhost:3306/mysql";//你自己设置数据库名称 
  Connection con=DriverManager.getConnection(url,"root",""); //如果你mysql中root的密码是空的话最好写成""代替null
  String sql="insert into txt (name,password) values ('"+"bb"+"','"+"mm"+"')";//你使用的表是txt,sql建表自己看着办吧
  Statement stmt=con.createStatement(); 
  stmt.executeUpdate(sql);
  alert("你好!");
    }
  </script>
</head>
<body>
<form action="UsersServlet.do?action=doLogin" name="useraddL"
id="useraddL" method="post">
<table class="midbox">
<tr>
<td class="namebox" colspan="2"><strong>『用户登录』</strong></td>
</tr>
<tr>
<td>
<c:if test="${result!=null}"><font color="red">${result}</font></c:if>
<table>
<tr>
<td>&nbsp;用&nbsp;户&nbsp;名 :</td>
<td><input type="text" name="usernameR" id="usernameR" value="<% 
//Cookie[] cookies = request.getCookies();
 // for   (int i=0;i< cookies.length;i++)   {
//  Cookie cookie=cookies[i];
//  if(cookie.getName().equals("bbsName")){
//  out.println(cookie.getValue());}
//  }%>"/></td>
</tr>
<tr>
<td>&nbsp;密&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;码 :</td>
<td><input type="password" name="passwordR" id="passwordR" value="<% 
  //for   (int i=0;i< cookies.length;i++)   {
//  Cookie cookie=cookies[i];
//  if(cookie.getName().equals("bbsPassword")){out.println(cookie.getValue());}
//  }%>"/></td>
</tr>
<tr>
<td></td>
<td><input type="checkbox" name="saveInfo" id="saveInfo" />保存登录信息
</td>
</tr>
<tr>
<td></td>
<td>
&nbsp;<button a href="http://selftest.chinaunix.net/" name="but" id="but" onclick="button();">登&nbsp;陆</button>
&nbsp;&nbsp;&nbsp; <input href="http://selftest.chinaunix.net/" type="reset"
value="重新填写"></td>
</tr>
</table>
</td>
</tr>
</table>
</form>
<div class="blankline"></div>
</body>
</html>

解决方案 »

  1.   

    ;<button name="but" id="but" onclick="button();">登&nbsp;陆</button>
    去掉 a href="http://selftest.chinaunix.net/"  试试 ?
      

  2.   

     Class.forName("org.gjt.mm.mysql.Driver"); //驱动程序你自己的,我的是com.mysql.jdbc.Driver
    String url="jdbc:mysql://localhost:3306/mysql";//你自己设置数据库名称  
    Connection con=DriverManager.getConnection(url,"root",""); //如果你mysql中root的密码是空的话最好写成""代替null
    String sql="insert into txt (name,password) values ('"+"bb"+"','"+"mm"+"')";//你使用的表是txt,sql建表自己看着办吧
    Statement stmt=con.createStatement();  
    stmt.executeUpdate(sql);
       这些貌似都是java代码  javascript中应该是不支持的吧  你应该用javascript跳转到action或者servlet里面  再对数据库进行操作!
      

  3.   

    这就是你的源码?
    js和java代码混为一谈,
    你这样写的代码,神马浏览器也不能识别
      

  4.   

    你怎么把java代码写在<script type="text/javascript">里面了? jsp页面上的java代码应该写在<% %>,你写在那里肯定不能运行