Java Web的数据库操作

/




<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8" import = "java.sql.*"%>
<!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>
</head>
<body>
<%
try {
// 加载数据库驱动,注册到驱动管理器
Class.forName("com.mysql.jdbc.Driver");
// 数据库连接字符串
String url = "jdbc:mysql://localhost:3306/test";
// 数据库用户名
String username = "root";
// 数据库密码
String password = "111";
// 创建Connection连接
Connection conn = DriverManager.getConnection(url,username,password);
// 判断 数据库连接是否为空
if(conn != null){
// 输出连接信息
out.println("数据库连接成功!");
// 关闭数据库连接
conn.close();
}else{
// 输出连接信息
out.println("数据库连接失败!");
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
%>


</body>

</html>









/


<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!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 type="text/css">
table {
border-left:5px solid #ffffff;
border-collapse: collapse;
}
td {
font: normal 12px/ 17px Arial;
padding: 2px;
}
th {
font: bold 12px/ 17px Arial;
padding: 4px;
border-bottom: 1px solid #333;
}
body {
font-size: 14px;
}
#main{
width:500px;
border:solid 1px #000000;
}
</style>
</head>
<body>
<div id="main">
<table>
<tr>
<th width="30px">编号</th>
<th width="30px">姓名</th>
<th width="30px" align="center">性别</th>
<th width="80px">职务</th>
<th width="135px">地址</th>
<th width="135px">备注</th>
</tr>
<c:forEach items="${emplist}" var="list">
<tr>
<td align="center">${list.id}</td>
<td>${list.name}</td>
<td>${list.sex}</td>
<td>${list.business}</td>
<td>${list.address}</td>
<td>${list.remark}</td>
</tr>
</c:forEach>
</table>
</div>
</body>
</html>










/




<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%>
<!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 type="text/css">
body {
font-size: 14px;
}


td{
padding:5px 0px 0px 10px;
}


#main {
width: 530px;
border: solid 1px #000000;
}
</style>
</head>
<body>
<form action="UpdateEmployee?id=${employeeVO.id}" method="post">
<div id="main">
<table>
<tr>
<td colspan="2" align="center"><b>修改员工信息</b></td>
</tr>
<tr>
<td width="280px">姓名:<input type="text" name="name" value="${employeeVO.name}" /></td>
<td>性别:<input type="radio" name="sex" value="男"
<c:if test="${employeeVO.sex=='男'}">
checked
</c:if> />男&nbsp;&nbsp;
<input type="radio" name="sex" value="女"
<c:if test="${employeeVO.sex=='女'}">
checked
</c:if> />女</td>
</tr>
<tr>
<td width="280px">职务:<input type="text" name="business"
value="${employeeVO.business}"></td>

<td>住址:<input type="text" name="address"
value="${employeeVO.address}"></td>
</tr>
<tr>
<td colspan="2">备注:<textarea name="remark" rows="3" cols="54">${employeeVO.remark}</textarea></td>
</tr>
<tr>
<td align="center" colspan="2">
<input type="submit" value="提交" style="width:60px"/> 
<input type="reset" value="取消" style="width:60px"/>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>









/


<%@ 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>
<script type="text/javascript">
function check(form){
with(form){
if(name.value == ""){
alert("图书名称不能为空");
return false;
}
if(price.value == ""){
alert("价格不能为空");
return false;
}
if(author.value == ""){
alert("作者不能为空");
return false;
}
return true;
}
}
</script>
</head>
<body>
<form action="AddBook.jsp" method="post" οnsubmit="return check(this);">
<table align="center" width="450">
<tr>
<td align="center" colspan="2">
<h2>添加图书信息</h2>
<hr>
</td>
</tr>
<tr>
<td align="right">图书名称:</td>
<td><input type="text" name="name" /></td>
</tr>
<tr>
<td align="right">价  格:</td>
<td><input type="text" name="price" /></td>
</tr>
<tr>
<td align="right">数  量:</td>
<td><input type="text" name="bookCount" /></td>
</tr>
<tr>
<td align="right">作  者:</td>
<td><input type="text" name="author" /></td>
</tr>
<tr>
<td align="center" colspan="2">
<input type="submit" value="添 加">
</td>
</tr>
</table>
</form>
</body>
</html>







<%@ 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">


<%@page import="java.sql.Connection"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.PreparedStatement"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>添加结果</title>
</head>
<body>

<jsp:useBean id="book" class="com.lyq.bean.Book"></jsp:useBean>
<jsp:setProperty property="*" name="book"/>
<%
try {
// 加载数据库驱动,注册到驱动管理器
Class.forName("com.mysql.jdbc.Driver");
// 数据库连接字符串
String url = "jdbc:mysql://localhost:3306/db_database10";
// 数据库用户名
String username = "root";
// 数据库密码
String password = "111";
// 创建Connection连接
Connection conn = DriverManager.getConnection(url,username,password);
// 添加图书信息的SQL语句
String sql = "insert into tb_books(name,price,bookCount,author) values(?,?,?,?)";
// 获取PreparedStatement
PreparedStatement ps = conn.prepareStatement(sql);
// 对SQL语句中的第1个参数赋值
ps.setString(1, book.getName());
System.out.println("name:"+book.getName());
// 对SQL语句中的第2个参数赋值
ps.setDouble(2, book.getPrice());
// 对SQL语句中的第3个参数赋值
ps.setInt(3,book.getBookCount());
// 对SQL语句中的第4个参数赋值
ps.setString(4, book.getAuthor());
// 执行更新操作,返回所影响的行数
int row = ps.executeUpdate();
// 判断是否更新成功
if(row > 0){
// 更新成输出信息
out.print("成功添加了 " + row + "条数据!");
}
// 关闭PreparedStatement,释放资源
ps.close();
// 关闭Connection,释放资源
conn.close();
} catch (Exception e) {
out.print("图书信息添加失败!");
e.printStackTrace();
}
%>
<br>
<a href="index.jsp">返回</a>
</body>
</html>









/



<%@ 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">


<%@page import="java.util.List"%>
<%@page import="com.lyq.bean.Book"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>所有图书信息</title>
<style type="text/css">
td{font-size: 12px;}
h2{margin: 0px}
</style>
</head>
<body>
<table align="center" width="450" border="1" height="180" bordercolor="white" bgcolor="black" cellpadding="1" cellspacing="1">
<tr bgcolor="white">
<td align="center" colspan="5">
<h2>所有图书信息</h2>
</td>
</tr>
<tr align="center" bgcolor="#e1ffc1" >
<td><b>ID</b></td>
<td><b>图书名称</b></td>
<td><b>价格</b></td>
<td><b>数量</b></td>
<td><b>作者</b></td>
</tr>
<%
// 获取图书信息集合
List<Book> list = (List<Book>)request.getAttribute("list");
// 判断集合是否有效
if(list == null || list.size() < 1){
out.print("没有数据!");
}else{
// 遍历图书集合中的数据
for(Book book : list){
%>
<tr align="center" bgcolor="white">
<td><%=book.getId()%></td>
<td><%=book.getName()%></td>
<td><%=book.getPrice()%></td>
<td><%=book.getBookCount()%></td>
<td><%=book.getAuthor()%></td>
</tr>
<%
}
}
%>
</table>
</body>
</html>






<%@ 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>
</head>
<body>
<a href="FindServlet">查看所有图书</a>
</body>
</html>







/


<%@ 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">


<%@page import="java.util.List"%>
<%@page import="com.lyq.bean.Book"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>所有图书信息</title>
<style type="text/css">
form{margin: 0px;}
td{font-size: 12px;}
h2{margin: 2px}
</style>
<script type="text/javascript">
function check(form){
with(form){
if(bookCount.value == ""){
alert("请输入更新数量!");
return false;
}
if(isNaN(bookCount.value)){
alert("格式错误!");
return false;
}
return true;;
}
}
</script>
</head>
<body>
<table align="center" width="500" border="1" height="170" bordercolor="white" bgcolor="black" cellpadding="1" cellspacing="1">
<tr bgcolor="white">
<td align="center" colspan="6">
<h2>所有图书信息</h2>
</td>
</tr>
<tr align="center" bgcolor="#e1ffc1" >
<td><b>ID</b></td>
<td><b>图书名称</b></td>
<td><b>价格</b></td>
<td><b>数量</b></td>
<td><b>作者</b></td>
<td><b>修改数量</b></td>
</tr>
<%
// 获取图书信息集合
List<Book> list = (List<Book>)request.getAttribute("list");
// 判断集合是否有效
if(list == null || list.size() < 1){
out.print("没有数据!");
}else{
// 遍历图书集合中的数据
for(Book book : list){
%>
<tr align="center" bgcolor="white">
<td><%=book.getId()%></td>
<td><%=book.getName()%></td>
<td><%=book.getPrice()%></td>
<td><%=book.getBookCount()%></td>
<td><%=book.getAuthor()%></td>
<td>
<form action="UpdateServlet" method="post" οnsubmit="return check(this);">
<input type="hidden" name="id" value="<%=book.getId()%>">
<input type="text" name="bookCount" size="3">
<input type="submit" value="修 改">
</form>
</td>
</tr>
<%


}
}
%>
</table>
</body>
</html>









/



<%@ 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>
</head>
<body>
<a href="FindServlet">查看所有图书</a>
</body>
</html>








/



<%@ 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">


<%@page import="java.util.List"%>
<%@page import="com.lyq.bean.Book"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>所有图书信息</title>
<style type="text/css">
form{margin: 0px;}
td{font-size: 12px;}
h2{margin: 2px}
</style>
<script type="text/javascript">
function check(form){
with(form){
if(bookCount.value == ""){
alert("请输入更新数量!");
return false;
}
if(isNaN(bookCount.value)){
alert("格式错误!");
return false;
}
return true;;
}
}
</script>
</head>
<body>
<table align="center" width="450" border="1" height="170" bordercolor="white" bgcolor="black" cellpadding="1" cellspacing="1">
<tr bgcolor="white">
<td align="center" colspan="6">
<h2>所有图书信息</h2>
</td>
</tr>
<tr align="center" bgcolor="#e1ffc1" >
<td><b>ID</b></td>
<td><b>图书名称</b></td>
<td><b>价格</b></td>
<td><b>数量</b></td>
<td><b>作者</b></td>
<td><b>删 除</b></td>
</tr>
<%
// 获取图书信息集合
List<Book> list = (List<Book>)request.getAttribute("list");
// 判断集合是否有效
if(list == null || list.size() < 1){
out.print("没有数据!");
}else{
// 遍历图书集合中的数据
for(Book book : list){
%>
<tr align="center" bgcolor="white">
<td><%=book.getId()%></td>
<td><%=book.getName()%></td>
<td><%=book.getPrice()%></td>
<td><%=book.getBookCount()%></td>
<td><%=book.getAuthor()%></td>
<td>
<a href="DeleteServlet?id=<%=book.getId()%>">删除</a>
</td>
</tr>
<%
}
}
%>
</table>
</body>
</html>










/


<%@ 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>
</head>
<body>
<a href="FindServlet">查看所有图书</a>
</body>
</html>








/


<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<!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=GB18030">
<title>首页</title>
</head>
<body>
<jsp:useBean id="batch" class="com.lyq.Batch"></jsp:useBean>
<%
// 执行批量插入操作
int row = batch.saveBatch();
out.print("批量插入了【" + row + "】条数据!");
%>
</body>
</html>










/



<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">


<%@page import="java.util.List"%>
<%@page import="com.lyq.bean.Book"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>所有图书信息</title>
<style type="text/css">
td{font-size: 12px;}
h2{margin: 0px}
</style>
</head>
<body>
<jsp:useBean id="findBook" class="com.lyq.bean.FindBook"></jsp:useBean>
<table align="center" width="450" border="1" height="180" bordercolor="white" bgcolor="black" cellpadding="1" cellspacing="1">
<tr bgcolor="white">
<td align="center" colspan="5">
<h2>所有图书信息</h2>
</td>
</tr>
<tr align="center" bgcolor="#e1ffc1" >
<td><b>ID</b></td>
<td><b>图书名称</b></td>
<td><b>价格</b></td>
<td><b>数量</b></td>
<td><b>作者</b></td>
</tr>
<%
// 获取图书信息集合
List<Book> list = findBook.findAll();
// 判断集合是否有效
if(list == null || list.size() < 1){
out.print("没有数据!");
}else{
// 遍历图书集合中的数据
for(Book book : list){
%>
<tr align="center" bgcolor="white">
<td><%=book.getId()%></td>
<td><%=book.getName()%></td>
<td><%=book.getPrice()%></td>
<td><%=book.getBookCount()%></td>
<td><%=book.getAuthor()%></td>
</tr>
<%
}
}
%>
</table>
</body>
</html>








/




<%@ 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>
</head>
<body>
<a href="FindServlet">查看所有商品信息</a>
</body>
</html>










/



/
<%@ 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">


<%@page import="java.util.List"%>
<%@page import="com.lyq.bean.Product"%><html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>所有商品信息</title>
<style type="text/css">
td{font-size: 12px;}
h2{margin: 0px}
</style>
</head>
<body>
<table align="center" width="450" border="1" height="180" bordercolor="white" bgcolor="black" cellpadding="1" cellspacing="1">
<tr bgcolor="white">
<td align="center" colspan="5">
<h2>所有商品信息</h2>
</td>
</tr>
<tr align="center" bgcolor="#e1ffc1" >
<td><b>ID</b></td>
<td><b>商品名称</b></td>
<td><b>价格</b></td>
<td><b>数量</b></td>
<td><b>单位</b></td>
</tr>
<%
List<Product> list = (List<Product>)request.getAttribute("list");
for(Product p : list){
%>
<tr align="center" bgcolor="white">
<td><%=p.getId()%></td>
<td><%=p.getName()%></td>
<td><%=p.getPrice()%></td>
<td><%=p.getNum()%></td>
<td><%=p.getUnit()%></td>
</tr>
<%
}
%>
<tr>
<td align="center" colspan="5" bgcolor="white">
<%=request.getAttribute("bar")%>
</td>
</tr>
</table>
</body>
</html>











/


<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<!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=GB18030">
<title>添加学生信息</title>
</head>
<body>
<form action="AddStudent" method="post">
<table align="center" width="450">
<tr>
<td align="center" colspan="2">
<h2>添加学生信息</h2>
<hr>
</td>
</tr>
<tr>
<td align="right">姓名:</td>
<td><input type="text" name="name" /></td>
</tr>
<tr>
<td align="right">性别:</td>
<td>
<input type="radio" name="sex" value="男" checked="checked"/>男
<input type="radio" name="sex" value="女"/>女
</td>
</tr>
<tr>
<td align="right">年龄:</td>
<td>
<select name="age">
<option value="10" selected="selected">10</option>
<option value="11" selected="selected">11</option>
<option value="12" selected="selected">12</option>
<option value="13" selected="selected">13</option>
</select>
</td>
</tr>
<tr>
<td align="right">班级:</td>
<td><input type="text" name="classes" /></td>
</tr>
<tr>
<td align="center" colspan="2">
<input type="submit" value="添 加">
</td>
</tr>
</table>
</form>


</body>
</html>










/



<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<!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=GB18030">
<title>Insert title here</title>
</head>
<body>
<a href="FindStudent">查看所有学生信息</a>
</body>
</html>















<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<%@page import="java.util.List"%>
<%@page import="com.lyq.bean.Student"%>
<!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=GB18030">
<title>Insert title here</title>
<style type="text/css">
td{font-size: 12px;}
h2{margin: 0px}
</style>
</head>
<body>
<table align="center" width="450" border="1" height="180" bordercolor="white" bgcolor="black" cellpadding="1" cellspacing="1">
<tr bgcolor="white">
<td align="center" colspan="5">
<h2>所有学生信息</h2>
</td>
</tr>
<tr align="center" bgcolor="#e1ffc1" >
<td><b>学号</b></td>
<td><b>姓名</b></td>
<td><b>性别</b></td>
<td><b>年龄</b></td>
<td><b>班级</b></td>
</tr>
<%
// 获取图书信息集合
List<Student> list = (List<Student>)request.getAttribute("list");
// 判断集合是否有效
if(list == null || list.size() < 1){
out.print("没有数据!");
}else{
// 遍历图书集合中的数据
for(Student s : list){
%>
<tr align="center" bgcolor="white">
<td><%=s.getId()%></td>
<td><%=s.getName()%></td>
<td><%=s.getSex()%></td>
<td><%=s.getAge()%></td>
<td><%=s.getClasses()%></td>
</tr>
<%
}
}
%>
</table>
</body>
</html>











/




<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<!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=GB18030">
<title>Insert title here</title>
</head>
<body>
<a href="FindStudent">查看所有学生信息</a>
</body>
</html>












<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<%@page import="java.util.List"%>
<%@page import="com.lyq.bean.Student"%>
<!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=GB18030">
<title>Insert title here</title>
<style type="text/css">
td{font-size: 12px;}
h2{margin: 0px}
</style>
<script type="text/javascript">
function check(){
var ids = document.getElementsByName("id");
for(var i=0;i<ids.length;i++){
if(ids[i].checked){
return true;
}
}
alert("请选择要删除的学生信息!");
return false;
}
</script>
</head>
<body>
<form action="DeleteStudent" method="post" οnsubmit="return check();">
<table align="center" width="450" border="1" height="180" bordercolor="white" bgcolor="black" cellpadding="1" cellspacing="1">
<tr bgcolor="white">
<td align="center" colspan="6">
<h2>所有学生信息</h2>
</td>
</tr>
<tr align="center" bgcolor="#e1ffc1" >
<td>
<input type="submit" value="批量删除">
</td>
<td><b>学号</b></td>
<td><b>姓名</b></td>
<td><b>性别</b></td>
<td><b>年龄</b></td>
<td><b>班级</b></td>
</tr>
<%
// 获取图书信息集合
List<Student> list = (List<Student>)request.getAttribute("list");
// 判断集合是否有效
if(list == null || list.size() < 1){
out.print("没有数据!");
}else{
// 遍历图书集合中的数据
for(Student s : list){
%>
<tr align="center" bgcolor="white">
<td>
<input type="checkbox" name="id" value="<%=s.getId()%>">
</td>
<td><%=s.getId()%></td>
<td><%=s.getName()%></td>
<td><%=s.getSex()%></td>
<td><%=s.getAge()%></td>
<td><%=s.getClasses()%></td>
</tr>
<%
}
}
%>
</table>
</form>
</body>
</html>













/












  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java Web开发中的数据库操作通常涉及以下几个步骤: 1. 导入数据库驱动:首先,你需要根据你所使用的数据库类型(如MySQL、Oracle等)导入相应的数据库驱动包。这可以通过在项目的构建路径中添加相应的JAR文件来实现。 2. 建立数据库连接:使用JDBC(Java Database Connectivity)API建立与数据库的连接。你需要提供数据库的连接URL、用户名和密码等信息来建立连接。例如,对于MySQL数据库,连接URL的格式为:"jdbc:mysql://hostname:port/database_name"。 3. 执行SQL语句:一旦与数据库建立了连接,你可以使用JDBC的Statement或PreparedStatement对象执行SQL语句。Statement对象用于执行静态SQL语句,而PreparedStatement对象用于执行带有参数的SQL语句,能够提高性能和安全性。 4. 处理结果集:对于查询语句,你可以使用ResultSet对象来处理返回的结果集。通过ResultSet对象,你可以逐行获取查询结果的数据并进行处理。 5. 关闭数据库连接:在数据库操作完成后,记得关闭数据库连接,以释放资源并避免占用过多的数据库连接。通常在finally块中关闭连接。 下面是一个简单的示例代码,展示了如何使用Java进行数据库操作(以MySQL数据库为例): ```java import java.sql.*; public class DatabaseExample { public static void main(String[] args) { Connection conn = null; Statement stmt = null; ResultSet rs = null; try { // 1. 导入数据库驱动 Class.forName("com.mysql.jdbc.Driver"); // 2. 建立数据库连接 conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase", "username", "password"); // 3. 执行SQL语句 stmt = conn.createStatement(); rs = stmt.executeQuery("SELECT * FROM mytable"); // 4. 处理结果集 while (rs.next()) { int id = rs.getInt("id"); String name = rs.getString("name"); // 处理每行数据 System.out.println("ID: " + id + ", Name: " + name); } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } finally { // 5. 关闭数据库连接 try { if (rs != null) rs.close(); if (stmt != null) stmt.close(); if (conn != null) conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } } ``` 注意,这只是一个简单的示例,实际的数据库操作可能更加复杂,并可能涉及到事务管理、连接池等更高级的技术。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值