jsp操作MySQL实现查询/插入/删除功能示例(2)


<%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="utf-8"%>
<%@ page contentType="text/html;charset=utf-8"%>
<%
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=utf-8");
%>
<html>
<body>
<%
Connection con=null;
String url="jdbc:mysql://localhost/html_db?user=root&password=123456";
//html_db为数据库名
Class.forName("org.gjt.mm.mysql.Driver").newInstance();//新建实例
Connection conn= DriverManager.getConnection(url);//建立连接
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select * from person_tb";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {%>
ID:<%=rs.getString("id")%> </br>
姓名:<%=rs.getString("name")%></br>
性别:<%=rs.getString("sex")%></br>
年龄:<%=rs.getString("age")%></br></br>
<%}%>
<%out.print("恭喜你!数据库操作成功! ");%>
<%
rs.close();
stmt.close();
conn.close();
%>
</br>
<input type="button" value="返回" />
</body>
</html>


insert_test.jsp页面代码如下:

复制代码 代码如下:


<%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="utf-8"%>
<%@ page contentType="text/html;charset=utf-8"%>
<%
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=utf-8");
%>
<htnl>
<head>
<title>往数据库插入数据</title>
</head>
<body>
<%
String id=request.getParameter("id"); //从表单获得
String name=request.getParameter("name"); //从表单获得
String sex=request.getParameter("sex"); //从表单获得
String age=request.getParameter("age"); //从表单获得

try
{
/** 连接数据库参数 **/
String driverName = "com.mysql.jdbc.Driver"; //驱动名称
String DBUser = "root"; //mysql用户名
String DBPasswd = "123456"; //mysql密码
String DBName = "html_db"; //数据库名

String connUrl = "jdbc:mysql://localhost/" + DBName + "?user=" + DBUser + "&password=" + DBPasswd;
Class.forName(driverName).newInstance();
Connection conn = DriverManager.getConnection(connUrl);
Statement stmt = conn.createStatement();
stmt.executeQuery("SET NAMES UTF8");
String insert_sql = "insert into person_tb values('" + id + "','" + name + "','" + sex + "','" + age + "')";
String query_sql = "select * from person_tb";

try {
stmt.execute(insert_sql);
}catch(Exception e) {
e.printStackTrace();
}
try {
ResultSet rs = stmt.executeQuery(query_sql);
while(rs.next()) {
%>
ID:<%=rs.getString("id")%> </br>
姓名:<%=rs.getString("name")%> </br>
性别:<%=rs.getString("sex")%> </br>
年龄:<%=rs.getString("age")%> </br> </br>
<%
}
}catch(Exception e) {
e.printStackTrace();
}
//rs.close();
stmt.close();
conn.close();
}catch (Exception e) {
e.printStackTrace();
}
%>
</br>
<input type="button" value="返回" />&nbsp;&nbsp;
</body>
</html>


delete_test.jsp页面的代码如下:

复制代码 代码如下:


<%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="UTF-8"%>
<%@ page contentType="text/html;charset=UTF-8"%>
<%&nbsp;
&nbsp; &nbsp;request.setCharacterEncoding("UTF-8");&nbsp;
&nbsp; &nbsp;response.setCharacterEncoding("UTF-8");&nbsp;
&nbsp; &nbsp;response.setContentType("text/html; charset=UTF-8");&nbsp;
%>
<htnl>
<head>
<title>删除人员</title>
</head>
<body>
<%
String id=request.getParameter("id"); //从表单获得&nbsp;
String name=request.getParameter("name"); //从表单获得
try&nbsp;
{&nbsp;
/** 连接数据库参数 **/
String driverName = "com.mysql.jdbc.Driver"; //驱动名称
String DBUser = "root"; //mysql用户名
String DBPasswd = "hel610"; //mysql密码
String DBName = "html_db"; //数据库名
String connUrl = "jdbc:mysql://localhost/" + DBName + "?user=" + DBUser + "&password=" + DBPasswd;
String query_sql = "select id,name,sex,age from person_tb";
String delete_sql = "";
Class.forName(driverName).newInstance();
Connection conn = DriverManager.getConnection(connUrl);
Statement stmt = conn.createStatement();
try{&nbsp;
if(id != null && id != ""){
out.println("删除了id=" + id + "的人。</br></br>");
delete_sql = "delete from person_tb where"){
out.println("删除了name=" + name + "的人。</br></br>");
delete_sql = "delete from person_tb where";
}else{
out.println("请至少输入ID、姓名中的一个!</br></br>");
}
}catch(Exception e){
e.printStackTrace();
} &nbsp;&nbsp;
try {&nbsp;
stmt.execute(delete_sql);&nbsp;
}catch(Exception e){
e.printStackTrace();
}
try {&nbsp;
ResultSet rs = stmt.executeQuery(query_sql); &nbsp;&nbsp;
while(rs.next()) {
%>
ID:<%=rs.getString("id")%>
</br> 姓名:<%=rs.getString("name")%>
</br> 性别:<%=rs.getString("sex")%>
</br> 年龄:<%=rs.getString("age")%>
</br>
</br>
<%
} &nbsp; &nbsp;
}catch(Exception e) {
e.printStackTrace();
}&nbsp;
//rs.close();
stmt.close();
conn.close();&nbsp;
}catch (Exception e) {
e.printStackTrace();
}&nbsp;
%>
</br>
<input type="button" value="返回" />
</body>
</html>


在浏览器直接访问index_test.jsp页面,点击删除按钮,填入ID或者姓名之中的任何一个并提交即可进行删除:

jsp操作MySQL实现查询/插入/删除功能示例

 
执行删除前(下左图)、后(下右图)数据库的内容显示为:

jsp操作MySQL实现查询/插入/删除功能示例

 

jsp操作MySQL实现查询/插入/删除功能示例


总的来说,查询、插入与删除的功能都实现了。

现在发现的问题是:本来是实现的是弹出层的位置是可以移动的,但现在移动不了了,具体原因在于鼠标拖动部分的第一句代码:var objDiv = document.getElementById("popDiv");即指定拖动对象时,没有指定好。正在找方法解决。有谁有比较好的方法,请帮下忙吧,多谢了!

您可能感兴趣的文章:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wzfjgy.html