Javascript

web 实验三通过js的数据库操作

本文主要是介绍web 实验三通过js的数据库操作,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

基于js的数据库操作

代码如下:

css文件:

body {
text-align: center;
}

table {
width: 400px;
border: 1px solid #696969;
border-collapse: collapse;
margin:0 auto;
}
th {
border: 1px solid #696969;
background-color: #FFF8DC;
}
td {
text-align: center;
border: 1px solid #696969;
height: 50px;
background-color: #E0FFFF;
}
input {
font-size: 20px;
}

 

index.jsp文件

<%@ page language="java"  contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@page import="java.sql.*" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>图书管理系统</title>
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
	<h1>学生管理系统</h1>
	<a href="add.jsp">添加学生信息</a>
	<br />
	<br />
	<table style="width: 50%;">
		<tr>
			<th>学号</th>
			<th>姓名</th>
			<th>性别</th>
			<th>生日</th>
			<th>管理</th>
		</tr>
		<%
			Class.forName("com.mysql.jdbc.Driver");
		    String db_url="jdbc:mysql://localhost:3306/lab03?uerUnicode=true&characterEncoding=UTF-8";
		    String db_user="root";
		    String db_password="123456";
		    Connection con=DriverManager.getConnection(db_url,db_user,db_password);
			//使用Statement对象
			Statement stmt = con.createStatement();
			ResultSet rs = stmt.executeQuery("select * from students");

			/*
			PreparedStatement stmt = con.prepareStatement("select * from bookinfo");
			ResultSet rs = stmt.executeQuery();
			*/
			while (rs.next()) {
				String id = rs.getString(2);
				out.println("<tr><td>" + rs.getString(2) + "</td><td>" + rs.getString(1) +"</td><td>" + rs.getString(3) + "</td><td>"
						+ rs.getString(4) + "</td><td><a href='change.jsp?id=" + id
						+ "'>修改</a> <a href='delete.jsp?id=" + id + "'>删除</a></td></tr>");
			}
			rs.close();
			stmt.close();
			con.close();
		%>
		
	</table>
	<br />
	<hr />
	<div style="text-align: center; width: 100%; font-size: 12px; color: #333;">
		©版权所有:石家庄铁道大学信息科学与技术学院  
	</div>
</body>
</html>

  add.jsp文件

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"  errorPage="error.jsp"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>添加学生信息</title>
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
	<form action="addsql.jsp" method="post">
		<h2>添加学生信息</h2>
		<table style="width: 50%">
			<tr>
				<th width="30%">学号:</th>
				<td width="70%"><input name="id" type="text"></td>
			</tr>
			<tr>
				<th>姓名:</th>
				<td><input name="name" type="text"></td>
			</tr>
			<tr>
				<th>性别:</th>
				<td><input name="sex" type="radio" value="男">男   
				<input name="sex" type="radio" value="女">女   </td>
				
			</tr>
				<tr>
				<th>生日:</th>
				<td><input name="year" type="text" style="width:60px;">年<input name="month" type="text" style="width:40px;">月<input name="day" type="text" style="width:40px;">日</td>
			</tr>
			<tr>
				<td colspan="2"><input type="submit" name="submit" value="添加"> <input type="reset" value="重置"></td>
			</tr>
		</table>
	</form>
	
</body>
</html>

  addsql.jsp

<%@ page language="java"  contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@page import="java.sql.*" %>
<%@ page errorPage="error.jsp"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>添加学生信息</title>
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
	<%
		request.setCharacterEncoding("utf-8");
		String id = request.getParameter("id");
		String name = request.getParameter("name");
		String sex = request.getParameter("sex");
		String year =request.getParameter("year");
		String month =request.getParameter("month");
		String day =request.getParameter("day");
		String birthday=year+"/"+month+"/"+day;
		Class.forName("com.mysql.jdbc.Driver");
	    String db_url="jdbc:mysql://localhost:3306/lab03?uerUnicode=true&characterEncoding=UTF-8";
	    String db_user="root";
	    String db_password="123456";
	    Connection con=DriverManager.getConnection(db_url,db_user,db_password);

		
 		PreparedStatement stmt = con.prepareStatement("insert into students(id,name,sex,birthday) values(?, ?, ?,?)");
		stmt.setString(1, id);
		stmt.setString(2, name);
		stmt.setString(3, sex); 
		stmt.setString(4, birthday); 
		int i = stmt.executeUpdate();
		
		
		if (i == 1) {
			out.println("<h2>添加成功!</h2><br/>");
			out.println("<a href='index.jsp'>返回首页</a>");
		} else {
			out.println("<h2>添加失败!</h2><br/>");
			out.println("<a href='add.jsp'>重新添加</a>");
		}
		stmt.close();
		con.close();
		
	%>
</body>
</html>

  change.jsp

<%@ page language="java"  contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@page import="java.sql.*" %>
<%@ page errorPage="error.jsp"%>
<!DOCTYPE html>
<html>
<head>
<title>修改学生信息</title>
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
	<%
		request.setCharacterEncoding("utf-8");
		String id = request.getParameter("id");	
		Class.forName("com.mysql.jdbc.Driver");
	    String db_url="jdbc:mysql://localhost:3306/lab03?uerUnicode=true&characterEncoding=UTF-8";
	    String db_user="root";
	    String db_password="123456";
	    Connection con=DriverManager.getConnection(db_url,db_user,db_password);
		//使用Statement对象

		//Statement stmt = con.createStatement();
		//ResultSet rs = stmt.executeQuery("select * from bookinfo where id=" + id);
		
		PreparedStatement stmt = con.prepareStatement("select * from students where id=?");
		stmt.setString(1, id);
		ResultSet rs = stmt.executeQuery();
		
		rs.next();
	%>
	<%
	String birthday=rs.getString(4);
    String res[]=birthday.split("/");
    String year=res[0];
    String month=res[1];
    String day=res[2];
	%>
	<form action="changesql.jsp" method="post">
		<h2>修改图书信息</h2>
		<table style="width:50%">
			<tr>
				<th width="30%">学号:</th>
				<td width="70%"><input name="id" type="text"
					value="<%=rs.getString(2)%>" readonly></td>
			</tr>
			<tr>
				<th>姓名:</th>
				<td><input name="name" type="text"
					value="<%=rs.getString(1)%>"></td>
			</tr>
			<tr>
				<th>性别:</th>
				
				<% 
				if ("男 ".equals(rs.getString(3)))
				{
					System.out.println(rs.getString(3));
					out.println("<td><input name='sex' type='radio' value='男' checked>男   "
							+ "<input name='sex' type='radio' value='女'>女   </td>");
				}
				if ("女 ".equals(rs.getString(3))){
					System.out.println(rs.getString(3));
					out.println("<td><input name='sex' type='radio' value='男' >男   "
							+ "<input name='sex' type='radio' value='女' checked>女   </td>");}
				%>
				
			</tr>
			<tr>
				<th>生日:</th>
				<th>
				<input name="year" type="text" 
				style="width:60px;" value="<%=year%>" >年
				<input name="month" type="text" 
				style="width:40px;" value="<%=month%>">月
				<input name="day" type="text" 
				style="width:40px;" value="<%=day%>">日
				</th>
			</tr>
			<tr>
				<td colspan="2"><input type="hidden" name="id" value="<%=id%>">
					<input type="submit" value="修改"> <input type="reset"
					value="重置"></td>
			</tr>
		</table>
	</form>
	<%
		rs.close();
		stmt.close();
		con.close();
	%>
</body>
</html>

  changesql.jsp

<%@ page language="java"  contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@page import="java.sql.*" %>
<%@ page errorPage="error.jsp"%>
<!DOCTYPE html>
<html>
<head>
<title>修改完成</title>
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
	<%
	    request.setCharacterEncoding("utf-8");
	    String id = request.getParameter("id");
	    String name = request.getParameter("name");
	    String sex = request.getParameter("sex");
	    String year =request.getParameter("year");
	    String month =request.getParameter("month");
	    String day =request.getParameter("day");
	    String birthday=year+"/"+month+"/"+day;
		Class.forName("com.mysql.jdbc.Driver");
	    String db_url="jdbc:mysql://localhost:3306/lab03?uerUnicode=true&characterEncoding=UTF-8";
	    String db_user="root";
	    String db_password="123456";
	    Connection con=DriverManager.getConnection(db_url,db_user,db_password);
		Statement stmt = con.createStatement();
		String sql = "update students set id='" + id + "',name='" + name + "',sex='" + sex
				+ " 'where id=" + id;
		System.out.println(sql);
		int i = stmt.executeUpdate(sql);
		if (i == 1) {
			out.println("<h2>修改成功!</h2><br/>");
			out.println("<a href='index.jsp'>返回首页</a>");
		} else {
			out.println("<h2>修改失败!</h2><br/>");
			out.println("<a href='edit.jsp?id='" + id + ">重新添加</a>");
		}
		stmt.close();
		con.close();
	%>
</body>
</html>

  delete.jsp

<%@ page language="java"  contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@page import="java.sql.*" %>
<%@ page errorPage="error.jsp"%>
<!DOCTYPE html>
<html>
<head>
<title>删除学生信息</title>
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
	<%
	request.setCharacterEncoding("utf-8");
	Class.forName("com.mysql.jdbc.Driver");
    String db_url="jdbc:mysql://localhost:3306/lab03?uerUnicode=true&characterEncoding=UTF-8";
    String db_user="root";
    String db_password="123456";
    Connection con=DriverManager.getConnection(db_url,db_user,db_password);
	Statement stmt=con.createStatement();
	String id=request.getParameter("id");
	int i=stmt.executeUpdate("delete from students where id="+id);
	if(i==1)
	{
		out.println("<h2>删除成功!</h2><br/>");
	}
		else
	{
		out.println("<h2>删除失败!</h2><br/>");
	}
	out.println("<a href='index.jsp'>返回首页</a>");
	stmt.close();
	con.close();

	%>
</body>
</html>

  error.jsp

<%@ page language="java" isErrorPage="true" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>error page</title>
</head>
<body>
	<h1>程序出现错误</h1>
	<div>错误信息为:<br/>
	<%=exception.getMessage()%><br/>
	<%=exception.toString()%>
	</div>
</body>
</html>

  

这篇关于web 实验三通过js的数据库操作的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!