jsp对数据库表的增删改
1.添加数据<%@ page language="java" import="java.sql.*" pageEncoding="UTF-8"%><html><body><%Class.forName("com.mysql.jdbc.Driver");Connection conn = DriverManager.getConnection("jdbc:m
·
1.添加数据
<%@ page language="java" import="java.sql.*" pageEncoding="UTF-8"%>
<html>
<body>
<%
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/student?useUnicode=true&characterEncoding=UTF-8","root","123456");Statement stat=conn.createStatement();
System.out.println(conn);
//添加数据
String sql="insert into xmq(sno,name,sex) values('0011','张诺','男')";
int i=stat.executeUpdate(sql);
out.println("成功添加"+i+"行");
System.out.println(conn);
stat.close();
conn.close();
%>
</body>
</html>
2.删除数据
<%@ page language="java" import="java.sql.*" pageEncoding="UTF-8"%>
<html>
<body>
<%
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/student?useUnicode=true&characterEncoding=UTF-8","root","123456");
Statement stat=conn.createStatement();
//删除数据
String sql="delete from xmq where sno='0002'";
int i=stat.executeUpdate(sql);
out.println("成功删除"+i+"行");
stat.close();
conn.close();
%>
</body>
</html>
3.修改数据
<%@ page language="java" import="java.sql.*" pageEncoding="UTF-8"%>
<html>
<body>
<%
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/student?useUnicode=true&characterEncoding=UTF-8","root","123456");Statement stat=conn.createStatement();
//修改数据
String sql="update xmq set sex='女' where sno='0008'";
int i=stat.executeUpdate(sql);
out.println("成功修改"+i+"行");
stat.close();
conn.close();
%>
</body>
</html>
更多推荐
已为社区贡献1条内容
所有评论(0)