第十周作业
1.设计一个注册页面,实现用户注册功能
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <html> <head> </head> <body> <script type="text/javascript"> function validate(){ if(loginForm.uname.value==""){ alret("账号不能为空!"); return; } if(loginForm.upwd.value=""){ alert("密码不能为空!"); } loginForm.submit(); } </script> <form name="doLogin.jsp" method="post"> 用户名:<input type="text" name="uname" ><br> 密码:<input type="password" name="upwd" > 年龄:<input type="number" name="age"> <input type="submit" value="登录"> <a href="rejest.jsp">注册</a> </form> </body> </html>
2.设计一个登陆页面,实现用户名密码登陆
public class student {
String sname;
String spas;
int sage;
public student(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;
}
package stu;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;
public class stuj extends stuTest{
/**
* @param args
*
*/
public boolean login(String Name, String pas) {
boolean f = false;
Connection conn = getConnection();
String sql = "select * from stu where uname=? and upwd=?";
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps = conn.prepareStatement(sql);
ps.setString(1, Name);
ps.setString(2, pas);
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 static void main(String Name, String pas,int Age) {
Connection conn = getConnection();
PreparedStatement ps = null;
Scanner sc=new Scanner(System.in);
try {
String sql = "insert into student(sname,spas,sage) values(?,?,?)";
// 4.执行SQL语句
System.out.println("请输名");
String sname=sc.next();
System.out.println("请输入密码");
String spas=sc.next();
System.out.println("请输入年龄");
int sage=sc.nextInt();
ps = conn.prepareStatement(sql);
ps.setString(1, sname);
ps.setString(2, spas);
ps.setInt(3, sage);
ps.executeUpdate();// 增删改都用这个
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
closeAll(conn, ps, null);
}
}
}
package stu;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class stuTest {
/**
* @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();
}
}
}