十
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP '1.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <% String name = request.getParameter("name1"); String pas = request.getParameter("pw"); String age = request.getParameter("age"); if(name!=null&&pas!=null){ out.println("注册成功!"); response.setHeader("refresh", "5;url=MyJsp.jsp"); }else { out.println("注册失败!"); response.setHeader("refresh", "5;url=re.jsp"); } %> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'login.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <% String name1 = request.getParameter("name1"); String pw = request.getParameter("pw"); try { if (name1!=null&&pw!=null) { out.println("登录成功!"); } else { out.println("登陆失败!"); response.setHeader("refresh", "2;url=MyJsp.jsp"); } } catch (Exception e) { out.println("异常!!"); response.setHeader("refresh", "2;url=MyJsp.jsp"); } %> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index2.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <form action="re.jsp" method="post"> 用户名:<input type="text" name="name1"><br> 密 码: <input type="password" name="pw"> <br> <input type="submit" value="注册"> </form> <form action="login.jsp" method="post"> <input type="submit" value="登录"> </form> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 're.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <form action="1.jsp"> 姓名:<input type="text" name="name1" /> <br> 密码:<input type="password"name="pw" /> <br> 年龄:<input type="text" name="age" /> <br> <input type="submit" value="注册" /> </form> </body> </html>
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Scanner; public class sql{ public class stu extends st{ /** * @param args * */ public boolean login(String name1, String pw) { boolean f = false; Connection conn = getConnection(); String sql = "select * from stu where name1=? and pw=?"; PreparedStatement ps = null; ResultSet rs = null; try { ps = conn.prepareStatement(sql); ps.setString(1, name1); ps.setString(2, pw); rs = ps.executeQuery(); if (rs.next()) f = true; } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { closeAll(conn, ps, rs); } return f; } public void main(String name1, String pw,int age) { Connection conn = getConnection(); PreparedStatement ps = null; Scanner sc=new Scanner(System.in); try { String sql = "insert into student(name1,pw,age) values(?,?,?)"; // 4.执行SQL语句 System.out.println("请输名"); String name=sc.next(); System.out.println("请输入密码"); String pas=sc.next(); System.out.println("请输入年龄"); int sage=sc.nextInt(); ps = conn.prepareStatement(sql); ps.setString(1, name1); ps.setString(2, pw); ps.setInt(3, age); ps.executeUpdate();// 增删改都用这个 } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { closeAll(conn, ps, null); } } } }
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class st { /** * @param args */ protected static Connection getConnection(){ Connection conn=null; try { Class.forName("com.mysql.jdbc.Driver"); // 2.建立连接 conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/test", "root", "jth12345"); } catch (Exception e) { e.printStackTrace(); } return conn; } //关闭连接 protected static void closeAll(Connection con,PreparedStatement ps,ResultSet rs){ try { if(rs != null) rs.close(); if(ps != null) ps.close(); if(con != null) con.close(); } catch (SQLException e) { e.printStackTrace(); } } }
public class stu { String sname; String spas; int sage; public stu(String name, String pas, int age) { super(); sname = name; this.spas = pas; sage = age; } public String getName() { return sname; } public void setName(String name) { sname = name; } public String getPas() { return spas; } public void setPas(String pas) { this.spas = pas; } public int getAge() { return sage; } public void setAge(int age) { sage = age; } }