精华内容
下载资源
问答
  • //连接服务器和数据库sample String userName = "sa"; //默认用户名 String userPwd = "123456"; //密码 Connection conn; Class.forName(driverName); conn = DriverManager.getConnection...
  • 删除数据库数据

    2016-12-06 22:28:21
    代码: index.jsp ; charset=UTF-8" pageEncoding="UTF-8"%> 查看所有图书 查看所有图书 book_list.jsp ; chars

     代码:

    index.jsp

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

    book_list.jsp

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

    Book.java

    package com.bean;
    public class Book {
    	private int id;
    	// 图书名称
    	private String name;
    	// 价格
    	private String price;
    	// 数量
    	private int bookCount;
    	// 作者
    	private String author;
    	public int getId() {
    		return id;
    	}
    	public void setId(int id) {
    		this.id = id;
    	}
    	public String getName() {
    		return name;
    	}
    	public void setName(String name) {
    		this.name = name;
    	}
    	public String getPrice() {
    		return price;
    	}
    	public void setPrice(String price) {
    		this.price = price;
    	}
    	public int getBookCount() {
    		return bookCount;
    	}
    	public void setBookCount(int bookCount) {
    		this.bookCount = bookCount;
    	}
    	public String getAuthor() {
    		return author;
    	}
    	public void setAuthor(String author) {
    		this.author = author;
    	}
    	
    	
    }
    

    FindServlet.java

    package com.Servlet;
    import java.io.IOException;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.ArrayList;
    import java.util.List;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import com.bean.Book;
    public class FindServlet extends HttpServlet {
    	private static final long serialVersionUID = 1L;
    
    	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		try {
    			Class.forName("com.mysql.jdbc.Driver");
    			String url = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8";
    			String username = "root";
    			String password = "root";
    			Connection conn = DriverManager.getConnection(url,username,password);
    			Statement stmt = conn.createStatement();
    			String sql = "select * from book";
    			ResultSet rs = stmt.executeQuery(sql);
    			List<Book> list = new ArrayList<Book>();
    			while(rs.next()){
    				 Book book = new Book();
    				 book.setId(rs.getInt("id"));
                     book.setName(rs.getString("name"));
                     book.setPrice(rs.getString("price"));
                     book.setBookCount(rs.getInt("bookCount"));
                     book.setAuthor(rs.getString("author"));
    				 list.add(book);
    			}
    			request.setAttribute("list", list);
    			rs.close();		
    			stmt.close();	
    			conn.close();	
    		} catch (ClassNotFoundException e) {
    			e.printStackTrace();
    		} catch (SQLException e) {
    			e.printStackTrace();
    		}
    		// 请求转发到book_list.jsp
    		request.getRequestDispatcher("book_list.jsp").forward(request, response);
    	}
    }
    

    DeleteServlet.java

    package com.Servlet;
    
    import java.io.IOException;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import com.connect.DButil;
    public class DeleteServlet extends HttpServlet {
    	private static final long serialVersionUID = 1L;
           
    	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		int id = Integer.valueOf(request.getParameter("id"));
    		try {
    			Connection conn = DButil.open();
    			String sql = "delete from book where id=?";
    			PreparedStatement ps = conn.prepareStatement(sql);
    			// 对SQL语句中的第一个占位符赋值
    			ps.setInt(1, id);
    			ps.executeUpdate();
    			ps.close();
    			conn.close();
    		} catch (Exception e) {
    			e.printStackTrace();
    		} 
    		// 重定向到FindServlet
    		response.sendRedirect("FindServlet");
    	}
    }
    

    截图:


    展开全文
  • 在做一个联系人的通讯录,怎么才能删除网页中的数据(联系人数据),但不删除mysql中的数据。求大虾解答,不胜感激!!
  • JSP中使用数据库
  • request.getRequestDispatcher("userList.jsp").forward(request,response); } catch (SQLException e) { e.printStackTrace(); } } protected void doGet(HttpServletRequest request, HttpServletResponse...

    package com.anbow.selvet;

    import com.anbow.JDBC.JDBC;

    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.SQLException;

    @WebServlet("/Delete")
    public class UserDelete extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    Connection connection = JDBC.getConnection();
    System.out.println(connection);
    String id = request.getParameter(“id”);
    String sql = “delete from userlist where id = ?”;

        try {
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1,id);
            ps.executeUpdate();
            request.getRequestDispatcher("userList.jsp").forward(request,response);
    
        } catch (SQLException e) {
            e.printStackTrace();
        }
    
    
    }
    
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    

    this.doPost(request,response);
    }
    }

    **

    需要给删除按钮传一个id

    **

    展开全文
  • jsp数据库基础讲了增、查,下面来说说删,删应该是常用的数据库基础,增删改查中最最简单的了。不多说,马上开始  1、在FormAction文件夹下新建delete.jsp文件   2、在delete.jsp文件中添加如下内容 <%@ page ...

    项目地址:https://gitee.com/martinHuang/jsp-basic/  

    jsp数据库基础讲了增、查,下面来说说删,删应该是常用的数据库基础,增删改查中最最简单的了。不多说,马上开始

      1、在FormAction文件夹下新建delete.jsp文件

       

    2、在delete.jsp文件中添加如下内容

    <%@ page language="java" contentType="text/html; charset=utf-8"
        pageEncoding="utf-8"%>
    <%@ page import="com.Database.*" %>
    <%@ page import="java.sql.*" %>
    <%
       request.setCharacterEncoding("utf-8");
       String id = request.getParameter("id");
       conDB db = new conDB();
       db.connectDB();
       Statement state = db.getConnection().createStatement();
       String sql = "delete from personinfo where id = "+id;
       int flag= state.executeUpdate(sql);
       if(flag > 0)
       {
    	   out.println("<script>alert('删除成功!');history.go(-1)</script>");
       }
       else
       {
    	   out.println("<script>alert('删除失败!');history.go(-1)</script>");
       }
       state.close();
    %>
    

    3、在WebContent文件夹下新建removeData.html文件


    4、在removeData.html文件中添加如下内容

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>通过表单删除数据</title>
    </head>
    <body>
       <form action="FormAction/delete.jsp" method="post">
           <input type="text" name="id" placeholder="请输入要删除的ID">
           <input type="submit" value="确定">
       </form>
    </body>
    </html>


    5、测试运行


    我输入4

    数据库删除前状态


    点击确定


    再次查看数据库


    OK~

    核心部分的方法跟上篇插入的差不多,具体可参考

    http://blog.csdn.net/mgsky1/article/details/52232174

    展开全文
  • JSP数据库的操作极简入门,案例实操。内容包括:JSP数据库的增删改查-搭建环境、JSP数据库的增删改查-page...增加及数据展示、JSP数据库的增删改查-修改及数据展示、JSP数据库的增删改查-删除数据展示。
  • spring框架下jsp脚本语言confirm脚本语言删除数据库数据怎么做,具体说一说怎么写confirm脚本代码?求大神解答啊
  • 使用servlet,jdbc将mysql中数据显示在jsp页面中: 效果如下:         //包bean.Student package bean; public class Student { private String sno; priva

     用servlet,jdbc将mysql中数据显示在jsp页面中:

    效果如下:

         

     

    //包bean.Student

    package bean;
    
    public class Student {
    	private String sno;
    	private String sname;
    	private String sex;
    	private int age;
    	private String sdept;
    	public String getSno() {
    		return sno;
    	}
    	public void setSno(String sno) {
    		this.sno = sno;
    	}
    	public String getSname() {
    		return sname;
    	}
    	public void setSname(String sname) {
    		this.sname = sname;
    	}
    	public String getSex() {
    		return sex;
    	}
    	public void setSex(String sex) {
    		this.sex = sex;
    	}
    	public int getAge() {
    		return age;
    	}
    	public void setAge(int age) {
    		this.age = age;
    	}
    	public String getSdept() {
    		return sdept;
    	}
    	public void setSdept(String sdept) {
    		this.sdept = sdept;
    	}
    	
    }
    

     

     //包dao.DBConnection

    package dao;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.Statement;
    import java.util.ArrayList;
    
    import bean.Student;
    
    public class DBConnection {
    	
    	/**
    	 * 驱动类名称
    	 */
    	private static final String DRIVER_CLASS = "com.mysql.jdbc.Driver";
    	
    	/**
    	 * 数据库连接字符串
    	 */
    	private static final String DATABASE_URL = "jdbc:mysql://localhost:3306/db3";
    	
    	/**
    	 * 数据库用户名
    	 */
    	private static final String USER_NAME = "root";
    	
    	/**
    	 * 数据库密码
    	 */
    	private static final String PASSWORD = "123456";
    	
    	/**
    	 * 数据库连接类
    	 */
    	private static Connection conn;
    	
    	/**
    	 * 数据库操作类
    	 */
    	private static Statement stmt;
    	
    	
    	
    	// 加载驱动
    	static{
    		try {
    			Class.forName(DRIVER_CLASS);
    		} catch (Exception e) {
    			System.out.println("加载驱动错误");
    			System.out.println(e.getMessage());
    		}
    	}
    	
    	// 取得连接
    	private static Connection getConnection(){
    		
    		try {
    			conn = DriverManager.getConnection(DATABASE_URL, USER_NAME, PASSWORD);
    		} catch (Exception e) {
    			System.out.println("取得连接错误");
    			System.out.println(e.getMessage());
    		}
    		return conn;
    	}
    	
    	
    	public void ExecuteDel(String sql){
    		
    		try {
    			stmt = getConnection().createStatement();
    		} catch (Exception e) {
    			System.out.println("statement取得错误");
    			System.out.println(e.getMessage());
    		}
    		
    		try {
    			int rows = stmt.executeUpdate(sql);
    			if(rows >= 1){
    				System.out.println("成功删除.....");
    			} else {
    				System.out.println("删除失败.....");
    			}
    			
    		} catch (Exception e) {
    			// TODO: handle exception
    		}
    		
    		
    	}
    	
    	public ArrayList<Student> getStudentList(String sql){
    		
    		ArrayList<Student> list = new ArrayList<Student>();
    		
    		// 取得数据库操作对象
    		try {
    			stmt = getConnection().createStatement();
    		} catch (Exception e) {
    			System.out.println("statement取得错误");
    			System.out.println(e.getMessage());
    			return null;
    		}
    		
    		try {
    			
    			// 查询数据库对象,返回记录集(结果集)
    			ResultSet rs = stmt.executeQuery(sql);
    			
    			// 循环记录集,查看每一行每一列的记录
    			while (rs.next()) {
    				// 第一列 sno
    				String sno = rs.getString(1);
    				
    				// 第2列 sname
    				String sname = rs.getString(2);
    				
    				// 性别
    				String ssex = rs.getString(3);
    				
    				// 年龄
    				int sage = rs.getInt(4);
    				
    				// 系
    				String sdept = rs.getString(5);
    				
    				Student stu = new Student();
    				stu.setSno(sno);
    				stu.setSname(sname);
    				stu.setAge(sage);
    				stu.setSex(ssex);
    				stu.setSdept(sdept);
    				
    				
    				list.add(stu);
    			}
    			
    		} catch (Exception e) {
    			System.out.println(e.getMessage());
    		}
    		return list;
    	}
    }
    

     //包servlet.StuServlet

    package servlet;
    
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.ArrayList;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import bean.Student;
    
    import dao.DBConnection;
    
    public class StuServlet extends HttpServlet {
    
    
    	public void doGet(HttpServletRequest request, HttpServletResponse response)
    			throws ServletException, IOException {
    
    		this.doPost(request, response);
    	}
    
    	public void doPost(HttpServletRequest request, HttpServletResponse response)
    			throws ServletException, IOException {
    
    		DBConnection db = new DBConnection();
    		
    		String sql = "select * from Student";
    		
    		ArrayList<Student> list = db.getStudentList(sql);
    		
    		request.setAttribute("list", list);
    		request.getRequestDispatcher("index.jsp").forward(request, response);
    	}
    
    	
    
    }
    

     

     // DelServlet

    package servlet;
    
    import java.io.IOException;
    import java.io.PrintWriter;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import dao.DBConnection;
    
    public class DelServlet extends HttpServlet {
    
    	public void doGet(HttpServletRequest request, HttpServletResponse response)
    			throws ServletException, IOException {
    		
    		this.doPost(request, response);
    	}
    
    	
    	public void doPost(HttpServletRequest request, HttpServletResponse response)
    			throws ServletException, IOException {
    		
    		String sno = request.getParameter("sno");
    		
    		//在mysql中语句:delete from student where Sno = '3';
    		String sql = "delete from student where Sno = ' " + sno + " ' ";
    		
    		DBConnection db = new DBConnection();
    		db.ExecuteDel(sql);
    		
    		request.getRequestDispatcher("stuservlet").forward(request, response);
    		
    	}
    
    	
    }
    

     

     // web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.5" 
    	xmlns="http://java.sun.com/xml/ns/javaee" 
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    	
      <servlet>
        <servlet-name>StuServlet</servlet-name>
        <servlet-class>servlet.StuServlet</servlet-class>
      </servlet>
      <servlet>
        <servlet-name>DelServlet</servlet-name>
        <servlet-class>servlet.DelServlet</servlet-class>
      </servlet>
    
    
      <servlet-mapping>
        <servlet-name>StuServlet</servlet-name>
        <url-pattern>/stuservlet</url-pattern>
      </servlet-mapping>
      <servlet-mapping>
        <servlet-name>DelServlet</servlet-name>
        <url-pattern>/delservlet</url-pattern>
      </servlet-mapping>
      
      
      <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
      </welcome-file-list>
    </web-app>
    

     

     / / index.jsp

    <%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <title>list</title>
    	<meta http-equiv="pragma" content="no-cache">
    	<meta http-equiv="cache-control" content="no-cache">
    	<meta http-equiv="expires" content="0">    
    	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    	<meta http-equiv="description" content="This is my page">
    	<!--
    	<link rel="stylesheet" type="text/css" href="styles.css">
    	-->
      </head>
      
      <body>
      	
      	<table border="1">
      		<tr>
      			<td>编号</td>
      			<td>姓名</td>
      			<td>性别</td>
      			<td>年龄</td>
      			<td>所在系</td>
      			<td>&nbsp;</td>
      		</tr>
      		
      		<c:forEach items="${list}" var="stu">
      			<tr>
      				<td>${stu.sno }</td>
      				<td>${stu.sname }</td>
      				<td>${stu.sex }</td>
      				<td>${stu.age }</td>
      				<td>${stu.sdept }</td>
      				<td><a href="delservlet?sno=${stu.sno }">删除</a></td>
      			</tr>
      		</c:forEach>
      	</table>
      	
      	
      </body>
    </html>
    


    展开全文
  • JSP删除数据

    2012-02-18 13:51:05
    使用JSP删除数据库中国的一条数据,相当简单的呦 大家看看
  • 一、项目文件目录结构图示 ...index.jsp <%@ page language="java" import="java.util.*" pageEncoding="gb2312"%> <%@ taglib tagdir="/WEB-INF/tags" prefix="inquire"%> <% Strin...
  • 能够实现简单的jsp数据库的查询,增加,删除,修改,但是如果要实现的自建数据源。是很简单的用例,希望对大家的学习有所帮助。
  • JavaWeb——JDBC 操作数据库 五、删除数据 在前面几篇博客中,已经实现在 JSP 页面查询、修改和增加数据,现在实现从 JSP 页面删除数据。 上一篇
  • JSP数据库类库.rar

    2019-07-09 13:26:04
    服务器端的Web服务器负责执行JSP程序,JSP程序通过JDBC(Java DataBase Connectivity)接口和数据库服务器相连,并取得数据库中的数据。 当然也可以通过JDBC向数据库发送SQL命令,对数据库进行新增、删除和修改...
  • Springmvc+Spring+maven+c3p0+jsp查询数据库中所有数据并显示 上篇Springmvc+Spring+maven+c3p0不加入jsp的链接 (https://blog.csdn.net/duduyingya/article/details/114992131) 1 新的项目结构 2 向pom.xml加入新...
  • jsp页面已经从数据库中提取数据,可以删除数据,就是在修改数据时,不知道怎么修改
  • Web jsp开发学习——连接数据库数据的增加和删除 1.首先在newlist界面增加三个图表,带上事件 newlist.jsp <%@ page language="java" contentType="text/html; charset=UTF-8" import="java....
  • 基本设置请参阅“JSP初学者数据库教程(一)查询数据数据删除共有三个页面,del_1.jsp del_2.jsp del_3.jspdel_1.jsp 代码 %@page contentType="text/html;charset=gb2312" language="java" import="java.sql....
  • Java Web JSP访问数据库

    2020-06-21 09:04:12
    JSP中可以写Java代码,很明显可以通过Java代码来访问数据库。在Java技术系列中访问数据库的技术叫做JDBC。 与JDBC相关的API存放在java.sql包中,主要包括以下类或接口。 1.Connection 负责连接数据库 2.Statement ...
  • jsp中的代码: <b>用户列表页面</b> <br> <a href="<%=path%>/manager.action">刷新</a> <br> <table width="80%" border="1" cellpadding="0" style=...
  • 代码如下: 数据库操作的代码: ...charset=GB2312"%> *" %> <jsp:useBean id="NewsBean" scope=...操作都显示成功,但是只有发布时数据库才有更改,修改和删除都对数据库没有改变?我也看不出代码哪里有错误求指点!
  • deleteShopping.jsp下面这张 ![图片说明](https://img-ask.csdn.net/upload/201605/11/1462963167_777137.png) 数据库表![图片说明](https://img-ask.csdn.net/upload/201605/11/1462962850_644405.png) 最后。。!...
  • jsp数据库连接管理

    2012-10-09 16:04:05
    适合初学者练习jsp连接mysql数据库。简单但的数据库操作。 增加、删除、修改数据库数据
  • jsp加javajs
  • 数据库建立: 1—————————————————...--删除数据表 DROP TABLE users; --创建数据表 CREATE TABLE users(  userid VARCHAR2(20),  password VARCHAR2(20),  CONSTRAINT pk_uid PRIMARY KEY(userid)
  • 1.1JDBC简述 在实际项目中,网页通常要与数据库...它提供了一系列的API,让Java语言编写的代码连接数据库,对数据库进行添加、删除、修改、查询等操作。 JDBC相关的API存放在java.sql包中,主要包括以下类和接口...
  • //数据源  String user="sa"; String password="12345678";  conn=DriverManager.getConnection(connectDB,user,password);//连接数据库对象  // pstmt=conn.createStatement();//创建SQL命令对象  pstmt =...
  • jsp操作数据库之查询

    千次阅读 2017-03-11 15:40:35
    ; PreparedStatement pst=null; ResultSet rs=null;%> id 用户名 密码 用户类型 数据操作 删除该记录 更新该记录

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 827
精华内容 330
关键字:

jsp删除数据库数据