第7次作业
package user; public class User { private int number; private int password; public int getNumber() { return number; } public void setNumber(int number) { this.number = number; } public int getPassword() { return password; } public void setPassword(int password) { this.password = password; } public User() { super(); // TODO Auto-generated constructor stub } public User(int number, int password) { super(); this.number = number; this.password = password; } } package user; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import com.mysql.jdbc.Connection; import com.mysql.jdbc.PreparedStatement; public class Preson { public static int addstu(User s) throws ClassNotFoundException, SQLException { int i = 0; try { // 加载数据库驱动 Class.forName("com.mysql.jdbc.Driver"); // 获取数据库连接对象 Connection conn = (Connection) DriverManager.getConnection( "jdbc:mysql:///stu", "root", "123456"); // 获取SQL语句执行者 PreparedStatement pstmt = (PreparedStatement) conn .prepareStatement("insert into number values(?,?)"); // 执行SQL语句 ResultSet rs = (ResultSet) pstmt.executeQuery(); // 5.处理结果集 pstmt.setInt(1, s.getNumber()); pstmt.setInt(2, s.getPassword()); i = pstmt.executeUpdate(); conn.close(); pstmt.close(); rs.close(); } catch (Exception e) { } return i; } } <%@ 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 'index.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="my.jsp" method="get" name="登录"> <h2>登录界面</h2> 账号:<input id="uname" type="text" name="user"> </br> 密码:<input id="upass" type="password" name="psw"></br> <input type="submit" value="登录"> <input id="login" type="submit" value="注册"> </form> </body> </html> <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ page import="user.Preson"%> <%@ page import="user.User"%> <%@ page import="java.sql.*"%> <%@ page import="java.lang.*" %> <% 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 'index.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"> </head> <body> <% User s = new User(); strig name = request.getParameter("user"); s.setNumber(name); string password = request.getParameter("psw"); s.setPassword(password); Preson ps = new Preson(); if (ps.addstu(s) > 0) { out.print("注册成功"); } else { out.println("注册失败"); } %> </body> </html>