【JSP+JDBC编程】实现简单的数据库增删改查
博客专区 > sugary 的博客 > 博客详情
【JSP+JDBC编程】实现简单的数据库增删改查
sugary 发表于2年前
【JSP+JDBC编程】实现简单的数据库增删改查
  • 发表于 2年前
  • 阅读 50
  • 收藏 0
  • 点赞 2
  • 评论 0

【腾讯云】新注册用户域名抢购1元起>>>   

摘要: JSP+JDBC技术完成用户管理功能

JSP+JDBC技术完成用户管理功能#

【1】主界面模块##

1)index.jsp###

<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%@ page import="java.sql.*"%>

<html>
<head>

<title>My JSP 'index.jsp' starting page</title>

</head>

<body>
	<%
		String url = "jdbc:mysql://localhost:3306/testdb"; //数据库连接串
		Class.forName("org.gjt.mm.mysql.Driver").newInstance(); //加载驱动程序
		Connection conn = DriverManager.getConnection(url, "root", "root"); //建立连接
		String sql = "select * from users";
		PreparedStatement pStmt = conn.prepareStatement(sql);
		ResultSet rs = pStmt.executeQuery();
	%>
	<table border="1" width="500">
		<tr>
			<th>编号</th>
			<th>姓名</th>
			<th>密码</th>
			<th colspan="2">操作</th>
		</tr>
		<%
			while (rs.next()) {
		%>

		<tr>
			<td>
				<%
					out.print(rs.getString("id"));
				%>
			</td>
			<td>
				<%
					out.print(rs.getString("username"));
				%>
			</td>
			<td>
				<%
					out.print(rs.getString("password"));
				%>
			</td>
			<td><a href="delete.jsp?id=<%=rs.getString("id")%>"  onclick="return confirm('确定删除该条记录?')">删除</a></td>
			<td><a href="edit.jsp?id=<%=rs.getString("id")%>">编辑</a></td>
		</tr>
		<%
			}
		%>

	</table>
	</br>
	<a href="register.jsp">添加新记录</a>

	<%
		//关闭
		rs.close();
		pStmt.close();
		conn.close();
	%>

</body>
</html>

新建模块##

1)register.jsp###

<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>

<html>
<head>

<title>My JSP 'register.jsp' starting page</title>

</head>

<body>

	<form action="add.jsp">
		姓名:<input type="text" name="username"/><br>
		密码:<input type="text" name="password"/><br><br>
		<input type="submit" value="添加" />
	</form>

</body>
</html>

2)add.jsp###

<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%@ page import="java.sql.*"%>

<html>
<head>

<title>My JSP 'add.jsp' starting page</title>

</head>

<body>

	<%
		String url = "jdbc:mysql://localhost:3306/testdb"; //数据库连接串
		Class.forName("org.gjt.mm.mysql.Driver").newInstance(); //加载驱动程序
		Connection conn = DriverManager.getConnection(url, "root", "root"); //建立连接
		
		String sql = "insert into users values(null,?,?)";
		PreparedStatement pStmt = conn.prepareStatement(sql);
		
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		pStmt.setString(1, username);
		pStmt.setString(2, password);

		int cnt = pStmt.executeUpdate();
		if (cnt > 0) {
			response.sendRedirect("index.jsp");
		} else {
			out.print("用户已存在,<a href='register.jsp'>重新注册</a>");
		}

		//关闭
		pStmt.close();
		conn.close();
	%>
	
</body>
</html>

编辑模块##

1)edit.jsp###

<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%@ page import="java.sql.*"%>


<html>
<head>

<title>My JSP 'edit.jsp' starting page</title>

</head>

<body>

	<%
		String url = "jdbc:mysql://localhost:3306/testdb"; //数据库连接串
		Class.forName("org.gjt.mm.mysql.Driver").newInstance(); //加载驱动程序
		Connection conn = DriverManager.getConnection(url, "root", "root"); //建立连接

		String id = request.getParameter("id");
		String username = "";
		String password = "";

		String sql = "select * from users where id=?";
		PreparedStatement pStmt = conn.prepareStatement(sql);
		pStmt.setString(1, id);
		ResultSet rs = pStmt.executeQuery();
		while (rs.next()) {
			username = rs.getString("username");
			password = rs.getString("password");
		}
	%>
	
	<form action="modify.jsp">
		编号:<input type="text" name="id" readonly="readonly" value="<%=id %>" /><br>
		 姓名:<input type="text" name="username" value="<%=username %>"  /><br>
		  密码:<input type="text" name="password"  value="<%=password %>"/><br><br> 
		 <input type="submit" value="修改" />
	</form>
	
	<%
		//关闭
		pStmt.close();
		conn.close();

	%>

</body>
</html>

2)modify.jsp###

<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%@ page import="java.sql.*"%>

<html>
<head>

<title>My JSP 'modify.jsp' starting page</title>

</head>

<body>
	<%
		String url = "jdbc:mysql://localhost:3306/testdb"; //数据库连接串
		Class.forName("org.gjt.mm.mysql.Driver").newInstance(); //加载驱动程序
		Connection conn = DriverManager.getConnection(url, "root", "root"); //建立连接

		String sql = "update users set username=?,password=? where id=?";
		PreparedStatement pStmt = conn.prepareStatement(sql);

		String id = request.getParameter("id");
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		
		pStmt.setString(1, username);
		pStmt.setString(2, password);
		pStmt.setString(3, id);
		
		pStmt.executeUpdate();

		//关闭
		pStmt.close();
		conn.close();
		
		response.sendRedirect("index.jsp");
	%>

</body>
</html>

删除模块##

1)delete.jsp###

<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%@ page import="java.sql.*"%>

<html>
<head>

<title>My JSP 'delete.jsp' starting page</title>

</head>

<body>
	<%
		String url = "jdbc:mysql://localhost:3306/testdb"; //数据库连接串
		Class.forName("org.gjt.mm.mysql.Driver").newInstance(); //加载驱动程序
		Connection conn = DriverManager.getConnection(url, "root", "root"); //建立连接

		String sql = "delete from users where id=?";
		PreparedStatement pStmt = conn.prepareStatement(sql);

		String id = request.getParameter("id");

		pStmt.setString(1, id);
		pStmt.executeUpdate();
		
		//关闭
		pStmt.close();
		conn.close();
		
		response.sendRedirect("index.jsp");
	%>
</body>
</html>
  • 打赏
  • 点赞
  • 收藏
  • 分享
共有 人打赏支持
粉丝 4
博文 2
码字总数 1855
×
sugary
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: