日期:2014-05-20  浏览次数:20836 次

将数据写入数据库
index.jsp页面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  <title>在线购物系统-用户登录入口</title>

  <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  <meta http-equiv="description" content="this is my page">
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">

 <script language="javascript">
function datacheck(){
if(login.username.value==""){
alert("用户名不能为空...");
return false;
}
if(login.password.value==""){
alert("用户密码不能为空...");
return false;
}

}
</script>
  </head>
  
  <body>
  <center>
<p>
用户登录入口
<p>
<form name="login" action="submit.jsp" method="post">
用户账号:<input name="account" type="text" value=""/><br><br>
用户密码:<input name="password" type="password" value="" />
<p>
<input name="submit" type="submit" value="提交" onclick="return datacheck();"/>
<input name="reset" type="reset" value="重置">
</form>
</center>
  </body>
</html>


submit.jsp页面、
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<% java.sql.Connection conn=null;
java.lang.String strConn;
java.sql.Statement sm=null;
java.sql.ResultSet rs=null;
try{
Class.forName("org.gjt.mm.mysql.Driver");
conn=java.sql.DriverManager.getConnection("jdbc:musql://localhost/user","root","");
sm=conn.createStatement();
String id=request.getParameter("account");
String pw=request.getParameter("password");
String sql="insert into user_info values('"+id+"','"+pw+"')";
String sql1="select * from user_info";
//执行SQL语句
sm.executeUpdate(sql);
rs=sm.executeQuery(sql1);
 %>
 <center>用户信息表</center>
 <form>
  <table>
  <% 
  while(rs.next()){//取得下一条记录
  String account=rs.getString("account");
  String password=rs.getString("password");
   
  %>
  <tr><td align="center">你的账号:</td><td><%=id %></td></tr>
  <tr><td align="center">你的账号:</td><td><%=pw %></td></tr>
  <%} %> 
  </table>
   
 </form>
 <%
}catch(java.sql.SQLException e){

out.println(e.toString());
}finally{
if(rs!=null)try{rs.close();}catch(java.sql.SQLException e1){

out.println(e1.toString());
}finally{
try{
if(sm!=null)
sm.close();
}catch(java.sql.SQLException e2){
out.println(e2.toString());
}finally{
try{

if(conn!=null)
conn.close();
}catch(java.sql.SQLException e3){
out.println(e3.toString());

}

}



}