<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<script type="text/javascript">
function changeActionToRegister(){
var s = document.getElementById("idForm")
s.setAttribute("action", "doRegister.jsp");
}
function changeActionToLogin(){
var s = document.getElementById("idForm")
s.setAttribute("action", "doLogin.jsp");
}
</script>
<body>
<form id="idForm" method="post">
用户名:<input type="text" name="username"/>
<br>
密码:<input type="password" name="password"/>
<br>
<input type="submit" onclick="changeActionToLogin()" value="登录">
<input type="submit" onclick="changeActionToRegister()" value="注册">
</form>
</body>
</html>
public static int insertuser(String username, String password,String realname) {
Connection conn = null;
PreparedStatement ps = null;
int count = 0;
try {
//获取连接
conn = DBUtils.getConnection();
//获取预编译的数据库操作对象
String sql = "insert into t_users (login_name,login_pwd,real_name) values (?,?,?)";
ps = conn.prepareStatement(sql);
//传值
ps.setString(1, username);
ps.setString(2, password);
ps.setString(3,realname);
count = ps.executeUpdate();
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
DBUtils.close(conn, ps, null);
}
return count;
}
package com.kjschool.javaweb.servlet;
import java.sql.*;
import java.util.ResourceBundle;
public class DBUtils {
private DBUtils(){
}
//类加载时绑定资源属性文件
private static ResourceBundle bundle=ResourceBundle.getBundle("resources.db");
//注册驱动
static{
try {
Class.forName(bundle.getString("driver"));
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
//获取数据库连接对象
public static Connection getConnection() throws SQLException {
String url=bundle.getString("url");
String user=bundle.getString("user");
String password=bundle.getString("password");
Connection conn= DriverManager.getConnection(url,user,password);
return conn;
}
//释放资源
public static void close(Connection conn, Statement stmt, ResultSet rs){
if (rs != null) {
try {
rs.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
<style type="text/css">
body {
background-color: azure;
}
form {
position: fixed;
top: 250px;
left: 650px;
background-color: #ADD8E6;
border-radius: 5px;
}
div {
font-size: 22px;
margin-top: 20px;
padding-left: 40px;
}
input {
width: 200px;
height: 20px;
border-radius: 5px;
}
#tj {
width: 80px;
height: 30px;
margin-left: 140px;
border-radius: 5px;
font-size: 16px;
margin-bottom: 20px;
}
</style>
</head>
<body>
<form action="/school/panduanreg.jsp" method="post">
<div>
账 号: <input type="text" name="username">
</div>
<div>
密 码: <input type="text" name="password">
</div>
<div>
<input type="submit" id="tj" value="注册">
</div>
</form>
</body>
<script type="text/javascript">
</script>
</html>
<%@page import="com.Stu"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
Stu s = new Stu();
int id = Integer.parseInt(request.getParameter("sid"));
s.setSid(id);
String uname = request.getParameter("uname");
s.setUname(uname);
String upwd = request.getParameter("upwd");
s.setUpwd(upwd);
StuDao sd=new StuDao();
if(sd.addStu(s)>0){
//跳转注册成功页面
}else{
//错误页面
}
%>