第十周作业
数据库test 中建个表 stu(stuid 主键 自动增长 ,用户名,密码,年龄)
1.设计一个注册页面,实现用户注册功能
2.设计一个登陆页面,实现用户名密码登陆
3.两个页面可以互相超链接
1<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 3 <html> 4 <head> 5 <title>My JSP 'Register.jsp' starting page</title> 6 <script type="text/javascript"> 7 function ok() { 8 if (registerForm.username.value == "") { 9 alert("账号不能为空!"); 10 return; 11 } 12 if (registerForm.password.value == "") { 13 alert("密码不能为空!"); 14 return; 15 } 16 registerForm.submit(); 17 18 } 19 </script> 20 </head> 21 <body> 22 <form name="registerForm" action="Add.jsp" method="post"> 23 <table> 24 <tr> 25 <td> 26 <th align="center">请注册</th> 27 </td> 28 </tr> 29 <tr> 30 <td>用户名:</td> 31 <td><input type="text" name="username"></input> 32 </td> 33 </tr> 34 <tr> 35 <td>密 码:</td> 36 <td><input type="password" name="password"></input> 37 </td> 38 </tr> 39 44 <tr> 45 <td rowspan="2"><input type="button" value="完成注册" onClick="ok()"></td> 46 </tr> 47 </table> 48 </form> 49 </body> 50 </html> Register
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 3 <html> 4 <head> 5 <title>My JSP 'Register.jsp' starting page</title> 6 <script type="text/javascript"> 7 function ok() { 8 if (registerForm.username.value == "") { 9 alert("账号不能为空!"); 10 return; 11 } 12 if (registerForm.password.value == "") { 13 alert("密码不能为空!"); 14 return; 15 } 16 registerForm.submit(); 17 18 } 19 </script> 20 </head> 21 <body> 22 <form name="registerForm" action="Add.jsp" method="post"> 23 <table> 24 <tr> 25 <td> 26 <th align="center">请注册</th> 27 </td> 28 </tr> 29 <tr> 30 <td>用户名:</td> 31 <td><input type="text" name="username"></input> 32 </td> 33 </tr> 34 <tr> 35 <td>密 码:</td> 36 <td><input type="password" name="password"></input> 37 </td> 38 </tr> 39 44 <tr> 45 <td rowspan="2"><input type="button" value="完成注册" onClick="ok()"></td> 46 </tr> 47 </table> 48 </form> 49 </body> 50 </html> Register
1 package com.study.stu; 2 3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.PreparedStatement; 6 import java.sql.ResultSet; 7 import java.sql.SQLException; 8 import java.util.List; 9 10 import javax.naming.Context; 11 import javax.naming.InitialContext; 12 import javax.naming.NamingException; 13 import javax.sql.DataSource; 14 15 public class JDBCUtils { 16 17 18 //获取连接 19 protected static Connection getConnection(){ 20 Connection conn=null; 21 try { 22 Class.forName("com.mysql.jdbc.Driver"); 23 // 2.建立连接 24 conn = DriverManager.getConnection( 25 "jdbc:mysql://localhost:3306/wl_tale", "root", "root"); 26 } catch (Exception e) { 27 e.printStackTrace(); 28 } 29 return conn; 30 } 31 32 33 34 35 //关闭连接 36 protected static void closeAll(Connection con,PreparedStatement ps,ResultSet rs){ 37 try { 38 if(rs != null) 39 rs.close(); 40 if(ps != null) 41 ps.close(); 42 if(con != null) 43 con.close(); 44 45 } catch (SQLException e) { 46 e.printStackTrace(); 47 } 48 } 49 50 } JDBC