数据库连接测试:学生信息系统界面
连接数据库 DBUtil.java
package Student;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.DriverManager;
public class DBUtil {
public static String db_url = "jdbc:mysql://localhost:3306/student?characterEncoding=utf8";
public static String db_user = "root";
public static String db_pass = "密码";
public static String db_user = "root";
public static String db_pass = "密码";
public static Connection getConn () {
Connection conn = null;
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(db_url, db_user, db_pass);
} catch (Exception e) {
e.printStackTrace();
}
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(db_url, db_user, db_pass);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
}
}
}
StudentOperate.java
package Student;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import Student.DBUtil;
/**
* Servlet implementation class StudentOperate
*/
@WebServlet("/operate")
public class StudentOperate extends HttpServlet {
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
super.service(request, response);
String method=request.getParameter("method");
if(method.equals("register"))
{
register(request,response);
// response.getWriter().append("okkk").append(request.getContextPath());
}
if(method.equals("add"))
{
add(request,response);
// response.getWriter().append("okkk").append(request.getContextPath());
}
}
private void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String name=request.getParameter("name");
// String num=request.getParameter("num");
name=new String(name.getBytes("ISO-8859-1"),"UTF-8");//服务器默认使用 ISO-8859-1 编码接受参数,手动转为UTF-8
String english=request.getParameter("english");
String math=request.getParameter("math");
String computer=request.getParameter("computer");
Connection con = DBUtil.getConn();
PreparedStatement stmt=null;
String sql = "insert into student(name,english,math,computer)values(?,?,?,?)";
try {
stmt = con.prepareStatement(sql);
stmt.setString(1,name);
stmt.setString(2,english);
stmt.setString(3,math);
stmt.setString(4,computer);
//stmt.execute();
int i=stmt.executeUpdate();
if(i==1) {
request.setAttribute("message", "添加成功!");
request.getRequestDispatcher("add.jsp").forward(request,response);
}
else
{
request.setAttribute("message", "添加失败!");
request.getRequestDispatcher("add.jsp").forward(request,response);
}
stmt.close();
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
* Servlet implementation class StudentOperate
*/
@WebServlet("/operate")
public class StudentOperate extends HttpServlet {
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
super.service(request, response);
String method=request.getParameter("method");
if(method.equals("register"))
{
register(request,response);
// response.getWriter().append("okkk").append(request.getContextPath());
}
if(method.equals("add"))
{
add(request,response);
// response.getWriter().append("okkk").append(request.getContextPath());
}
}
private void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String name=request.getParameter("name");
// String num=request.getParameter("num");
name=new String(name.getBytes("ISO-8859-1"),"UTF-8");//服务器默认使用 ISO-8859-1 编码接受参数,手动转为UTF-8
String english=request.getParameter("english");
String math=request.getParameter("math");
String computer=request.getParameter("computer");
Connection con = DBUtil.getConn();
PreparedStatement stmt=null;
String sql = "insert into student(name,english,math,computer)values(?,?,?,?)";
try {
stmt = con.prepareStatement(sql);
stmt.setString(1,name);
stmt.setString(2,english);
stmt.setString(3,math);
stmt.setString(4,computer);
//stmt.execute();
int i=stmt.executeUpdate();
if(i==1) {
request.setAttribute("message", "添加成功!");
request.getRequestDispatcher("add.jsp").forward(request,response);
}
else
{
request.setAttribute("message", "添加失败!");
request.getRequestDispatcher("add.jsp").forward(request,response);
}
stmt.close();
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void register(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String account=request.getParameter("account");
account=new String(account.getBytes("ISO-8859-1"),"UTF-8");
String code=request.getParameter("code");
String sex=request.getParameter("sex");
sex=new String(sex.getBytes("ISO-8859-1"),"UTF-8");
String name=request.getParameter("name");
// String num=request.getParameter("num");
name=new String(name.getBytes("ISO-8859-1"),"UTF-8");//服务器默认使用 ISO-8859-1 编码接受参数,手动转为UTF-8
String num=request.getParameter("num");
String email=request.getParameter("email");
String college=request.getParameter("college");
college=new String(college.getBytes("ISO-8859-1"),"UTF-8");
String major=request.getParameter("major");
major=new String(major.getBytes("ISO-8859-1"),"UTF-8");
String cclass=request.getParameter("class");
cclass=new String(cclass.getBytes("ISO-8859-1"),"UTF-8");
String year=request.getParameter("year");
String home=request.getParameter("home");
home=new String(home.getBytes("ISO-8859-1"),"UTF-8");
String other=request.getParameter("other");
other=new String(other.getBytes("ISO-8859-1"),"UTF-8");
Connection con = DBUtil.getConn();
PreparedStatement stmt=null;
String sql = "insert into register(account,code,sex,name,num,email,college,major,class,year,home,other)values(?,?,?,?,?,?,?,?,?,?,?,?)";
try {
stmt = con.prepareStatement(sql);
stmt.setString(1,account);
stmt.setString(2,code);
stmt.setString(3,sex);
stmt.setString(4,name);
stmt.setString(5,num);
stmt.setString(6,email);
stmt.setString(7,college);
stmt.setString(8,major);
stmt.setString(9,cclass);
stmt.setString(10,year);
stmt.setString(11,home);
stmt.setString(12,other);
//stmt.execute();
int i=stmt.executeUpdate();
if(i==1) {
request.setAttribute("message", "添加成功!");
request.getRequestDispatcher("regist.jsp").forward(request,response);
}
else
{
request.setAttribute("message", "添加失败!");
request.getRequestDispatcher("regist.jsp").forward(request,response);
}
stmt.close();
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
private void register(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String account=request.getParameter("account");
account=new String(account.getBytes("ISO-8859-1"),"UTF-8");
String code=request.getParameter("code");
String sex=request.getParameter("sex");
sex=new String(sex.getBytes("ISO-8859-1"),"UTF-8");
String name=request.getParameter("name");
// String num=request.getParameter("num");
name=new String(name.getBytes("ISO-8859-1"),"UTF-8");//服务器默认使用 ISO-8859-1 编码接受参数,手动转为UTF-8
String num=request.getParameter("num");
String email=request.getParameter("email");
String college=request.getParameter("college");
college=new String(college.getBytes("ISO-8859-1"),"UTF-8");
String major=request.getParameter("major");
major=new String(major.getBytes("ISO-8859-1"),"UTF-8");
String cclass=request.getParameter("class");
cclass=new String(cclass.getBytes("ISO-8859-1"),"UTF-8");
String year=request.getParameter("year");
String home=request.getParameter("home");
home=new String(home.getBytes("ISO-8859-1"),"UTF-8");
String other=request.getParameter("other");
other=new String(other.getBytes("ISO-8859-1"),"UTF-8");
Connection con = DBUtil.getConn();
PreparedStatement stmt=null;
String sql = "insert into register(account,code,sex,name,num,email,college,major,class,year,home,other)values(?,?,?,?,?,?,?,?,?,?,?,?)";
try {
stmt = con.prepareStatement(sql);
stmt.setString(1,account);
stmt.setString(2,code);
stmt.setString(3,sex);
stmt.setString(4,name);
stmt.setString(5,num);
stmt.setString(6,email);
stmt.setString(7,college);
stmt.setString(8,major);
stmt.setString(9,cclass);
stmt.setString(10,year);
stmt.setString(11,home);
stmt.setString(12,other);
//stmt.execute();
int i=stmt.executeUpdate();
if(i==1) {
request.setAttribute("message", "添加成功!");
request.getRequestDispatcher("regist.jsp").forward(request,response);
}
else
{
request.setAttribute("message", "添加失败!");
request.getRequestDispatcher("regist.jsp").forward(request,response);
}
stmt.close();
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public StudentOperate() {
super();
// TODO Auto-generated constructor stub
}
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public StudentOperate() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
<%@ 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>注册</title>
<style>
.demo {
width: 150px;
height: 20px;
float: left;
}
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>注册</title>
<style>
.demo {
width: 150px;
height: 20px;
float: left;
}
.tian {
color: red;
float: left;
}
color: red;
float: left;
}
#end {
position: absolute;
top: 0px;
right: 0px;
}
</style>
</head>
<body background="2.jpg"
style="background-repeat: no-repeat; background-size: 100%;">
<h4 align="left">当前位置:添加学生信息</h4>
position: absolute;
top: 0px;
right: 0px;
}
</style>
</head>
<body background="2.jpg"
style="background-repeat: no-repeat; background-size: 100%;">
<h4 align="left">当前位置:添加学生信息</h4>
<form method="post" action="operate?method=register"
onSubmit="return check();">
<div>
<table style="margin-left: 20%;" border="0" width='60%'>
<tr>
<th>登录账号:</th>
<td style="width: 80%;"><input type="text" id="account"
name="account" class="demo" onchange="checkaccount()"> <span
id="div1" class="tian"></span></td>
onSubmit="return check();">
<div>
<table style="margin-left: 20%;" border="0" width='60%'>
<tr>
<th>登录账号:</th>
<td style="width: 80%;"><input type="text" id="account"
name="account" class="demo" onchange="checkaccount()"> <span
id="div1" class="tian"></span></td>
</tr>
<tr>
<th>登录密码:</th>
<td><input type="password" id="code" name="code" class="demo"
onchange="checkcode()"> <span id="div2" class="tian"></span></td>
<tr>
<th>登录密码:</th>
<td><input type="password" id="code" name="code" class="demo"
onchange="checkcode()"> <span id="div2" class="tian"></span></td>
</tr>
<tr>
<th>性别:</th>
<td><select name="sex">
<option value="男">男</option>
<option value="女">女</option>
</select></td>
</tr>
<tr>
<th>性别:</th>
<td><select name="sex">
<option value="男">男</option>
<option value="女">女</option>
</select></td>
</tr>
<tr>
<th>姓名:</th>
<td><input type="text" id="name" name="name" class="demo" onchange="checkname()">
<span id="div3" class="tian"></span></td>
</tr>
<tr>
<th>学号:</th>
<td><input type="text" id="num" name="num" class="demo"
onchange="checknum()"> <span id="div4" class="tian"></span></td>
</tr>
<tr>
<th>电子邮件:</th>
<td><input type="text" id="email" name="email" class="demo"
onchange="checkemail()"> <span id="div5" class="tian"></span></td>
</tr>
<tr>
<th>所在学院:</th>
<td><input type="text" id="college" name="college"
class="demo" onblur="checkcollege()"> <span id="div6"
class="tian"></span></td>
</tr>
<tr>
<th>所在系:</th>
<td><input type="text" id="major" name="major" class="demo"
onblur="checkmajor()"> <span id="div7" class="tian"></span></td>
</tr>
<tr>
<th>所在班级:</th>
<td><input type="text" id="class" name="class" class="demo"
onblur="checkclass()"> <span id="div8" class="tian"></span></td>
</tr>
<tr>
<th>入学年份(界):</th>
<td><select name="year">
<option value="2016">2016</option>
<option value="2017">2017</option>
<option value="2018">2018</option>
<option value="2019">2019</option>
</select>届<span id="div9" class="tian"></span></td>
</tr>
<tr>
<th>生源地:</th>
<td><input type="text" id="home" name="home" class="demo"
onchange="checkphone()"> <span id="div10" class="tian"></span></td>
</tr>
<tr>
<th>备注:</th>
<td><textarea rows="8" cols="40" id="other" name="other"
onchange="checkother()"> </textarea><span id="div11" class="tian"></span></td>
</tr>
<th>姓名:</th>
<td><input type="text" id="name" name="name" class="demo" onchange="checkname()">
<span id="div3" class="tian"></span></td>
</tr>
<tr>
<th>学号:</th>
<td><input type="text" id="num" name="num" class="demo"
onchange="checknum()"> <span id="div4" class="tian"></span></td>
</tr>
<tr>
<th>电子邮件:</th>
<td><input type="text" id="email" name="email" class="demo"
onchange="checkemail()"> <span id="div5" class="tian"></span></td>
</tr>
<tr>
<th>所在学院:</th>
<td><input type="text" id="college" name="college"
class="demo" onblur="checkcollege()"> <span id="div6"
class="tian"></span></td>
</tr>
<tr>
<th>所在系:</th>
<td><input type="text" id="major" name="major" class="demo"
onblur="checkmajor()"> <span id="div7" class="tian"></span></td>
</tr>
<tr>
<th>所在班级:</th>
<td><input type="text" id="class" name="class" class="demo"
onblur="checkclass()"> <span id="div8" class="tian"></span></td>
</tr>
<tr>
<th>入学年份(界):</th>
<td><select name="year">
<option value="2016">2016</option>
<option value="2017">2017</option>
<option value="2018">2018</option>
<option value="2019">2019</option>
</select>届<span id="div9" class="tian"></span></td>
</tr>
<tr>
<th>生源地:</th>
<td><input type="text" id="home" name="home" class="demo"
onchange="checkphone()"> <span id="div10" class="tian"></span></td>
</tr>
<tr>
<th>备注:</th>
<td><textarea rows="8" cols="40" id="other" name="other"
onchange="checkother()"> </textarea><span id="div11" class="tian"></span></td>
</tr>
</table>
<input style="margin-left: 42.5%" type="submit" value="添加">
<input style="margin-left: 42.5%" type="submit" value="添加">
</div>
</form>
<script type="text/javascript">
function checkaccount() {
var div = document.getElementById("div1");
div.innerHTML = "";
var account = document.getElementById("account");
if (account.value == " ") {
div.innerHTML = "用户名不能为空!";
account.focus();
return false;
}
</form>
<script type="text/javascript">
function checkaccount() {
var div = document.getElementById("div1");
div.innerHTML = "";
var account = document.getElementById("account");
if (account.value == " ") {
div.innerHTML = "用户名不能为空!";
account.focus();
return false;
}
var characcount1 = account.value.toLowerCase();
for (var i = 0; i < account.value.length; i++) {
var characcount = characcount1.charAt(i);
if (!(characcount >= 0 && characcount <= 9)
&& (!(characcount >= 'a' && characcount <= 'z'))
&& (characcount != '_')) {
div.innerHTML = "用户名包含非法字符!";
account.focus();
return false;
}
}
if (account.value.length<6||account.value.length>12) {
div.innerHTML = "长度6-12个字符!";
account.focus();
return false;
}
return true;
}
function checkcode() {
var div = document.getElementById("div2");
div.innerHTML = "";
var code = document.getElementById("code");
if (code.value == " ") {
div.innerHTML = "密码不能为空!";
code.focus();
return false;
}
if (code.value.length<8||code.value.length>16) {
div.innerHTML = "密码长度8-16位!";
code.focus();
return false;
}
return true;
}
function checkname() {
var div = document.getElementById("div3");
div.innerHTML = "";
var name = document.getElementById("name");
if (name.value =="") {
div.innerHTML = "姓名不能为空!";
name.select();
return false;
}
return true;
}
function checknum() {
var div = document.getElementById("div4");
div.innerHTML = "";
var reg = /^2018(\d{4})$/;
var num = document.getElementById("num");
if (num.value == "") {
div.innerHTML = "学号不能为空!";
num.select();
return false;
}
div.innerHTML = "";
if (!reg.test(num.value)) {
div.innerHTML = "学号格式错误!学号以2018开头!";
num.select();
return false;
}
return true;
}
for (var i = 0; i < account.value.length; i++) {
var characcount = characcount1.charAt(i);
if (!(characcount >= 0 && characcount <= 9)
&& (!(characcount >= 'a' && characcount <= 'z'))
&& (characcount != '_')) {
div.innerHTML = "用户名包含非法字符!";
account.focus();
return false;
}
}
if (account.value.length<6||account.value.length>12) {
div.innerHTML = "长度6-12个字符!";
account.focus();
return false;
}
return true;
}
function checkcode() {
var div = document.getElementById("div2");
div.innerHTML = "";
var code = document.getElementById("code");
if (code.value == " ") {
div.innerHTML = "密码不能为空!";
code.focus();
return false;
}
if (code.value.length<8||code.value.length>16) {
div.innerHTML = "密码长度8-16位!";
code.focus();
return false;
}
return true;
}
function checkname() {
var div = document.getElementById("div3");
div.innerHTML = "";
var name = document.getElementById("name");
if (name.value =="") {
div.innerHTML = "姓名不能为空!";
name.select();
return false;
}
return true;
}
function checknum() {
var div = document.getElementById("div4");
div.innerHTML = "";
var reg = /^2018(\d{4})$/;
var num = document.getElementById("num");
if (num.value == "") {
div.innerHTML = "学号不能为空!";
num.select();
return false;
}
div.innerHTML = "";
if (!reg.test(num.value)) {
div.innerHTML = "学号格式错误!学号以2018开头!";
num.select();
return false;
}
return true;
}
function checkemail() {
var div = document.getElementById("div5");
div.innerHTML = "";
var div = document.getElementById("div5");
div.innerHTML = "";
var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
var email = document.getElementById("email");
if (!reg.test(email.value)) {
div.innerHTML = "大哥邮箱格式错误!";
email.select();
return false;
}
if (email.value == " ") {
div.innerHTML = "大哥你还没输入自己的邮箱!";
email.select();
return false;
}
if (!reg.test(email.value)) {
div.innerHTML = "大哥邮箱格式错误!";
email.select();
return false;
}
if (email.value == " ") {
div.innerHTML = "大哥你还没输入自己的邮箱!";
email.select();
return false;
}
return true;
}
}
function check() {
if (checkaccount() && checkcode() && checkname() && checknum() && checkemail())
{
confirm("恭喜您成功入坑石家庄铁道大学!");
return true;
}
confirm("格式错误!笨蛋!");
return false;
{
confirm("恭喜您成功入坑石家庄铁道大学!");
return true;
}
confirm("格式错误!笨蛋!");
return false;
}
</script>
</body>
</html>
</script>
</body>
</html>
结果样例: