jsp資料庫增刪改查項目,jsp增刪改查代碼怎麼寫

本文目錄一覽:

怎麼用jsp做登錄,增刪查改

1. 先學會一種資料庫的使用。推薦MySQL吧。可以上什麼菜鳥教程啊什麼的去參考。 2天速成

2. 學會使用Java中的JDBC來連接資料庫,並進行增刪改查操作。注意,所有資料庫語句都要使用PrepareStatement。 半天搞定

3. 上各種菜鳥教程,w3school等,掌握html css javascript的使用。1天半速成

4. 嘗試使用eclipse新建dynamic web project,並成功創建一個jsp的Hello World頁面。 半天搞定

5. 學習並使用javabean來將jsp頁面和java程序相結合起來,並能在頁面上輸出各種java程序的結果。將jsp和html知識融合,能做出有點像樣的頁面。1天

6. 融匯貫通1-5,邊查漏補缺邊完成你boss的任務。不要忘記搜索引擎永遠是最好的老師。1天

jsp怎麼連接資料庫做增刪改查

資料庫連接類:

package cn.hpu.bbs.util;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

public class DB {

 

 // 定義MySQL的資料庫驅動程序

 public static final String DBDRIVER = “com.mysql.jdbc.Driver” ;

 //定義mysql的資料庫連接地址:

 public static final String DBDURL = “jdbc:mysql://localhost/bbs2014” ;

 //mysql資料庫的連接用戶名

 public static final String DBUSER = “root” ;

 //mysql資料庫的連接密碼

 public static final String DBPASS = “1234” ;

 

 public static Connection createConn(){

  Connection conn =null;

  try {

   Class.forName(DBDRIVER);

   conn=DriverManager.getConnection(DBDURL,DBUSER,DBPASS);

  } catch (ClassNotFoundException e) {

   e.printStackTrace();

  } catch (SQLException e) {

   e.printStackTrace();

  }

  

  return conn;

 }

 

 public static PreparedStatement prepare(Connection conn,String sql){

  PreparedStatement ps=null;

  try {

   ps=conn.prepareStatement(sql);

  } catch (SQLException e) {

   // TODO Auto-generated catch block

   e.printStackTrace();

  }

  return ps;

 }

 

 public static void close(Connection conn){

  if(conn==null) return;

  try {

   conn.close();

   conn=null;

  } catch (SQLException e) {

   // TODO Auto-generated catch block

   e.printStackTrace();

  }

 }

 

 public static void close(Statement stmt){

  if(stmt==null) return;

  try {

   stmt.close();

   stmt=null;

  } catch (SQLException e) {

   // TODO Auto-generated catch block

   e.printStackTrace();

  }

 }

 

 public static void close(ResultSet rs){

  if(rs==null) return;

  try {

   rs.close();

   rs=null;

  } catch (SQLException e) {

   // TODO Auto-generated catch block

   e.printStackTrace();

  }

 }

}

Category的一個JavaBean:

package cn.hpu.bbs.model;

public class Category {

private int id;

private String name;

private String description;

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 getDescription() {

return description;

}

public void setDescription(String description) {

this.description = description;

}

}

對資料庫和Category的操作類://說白了就是增刪查修

pre name=”code” class=”java”package cn.hpu.bbs.service;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.util.ArrayList;

import java.util.List;

import cn.hpu.bbs.model.Category;

import cn.hpu.bbs.util.DB;

public class CategoryService {

public void add(Category c){

Connection conn=DB.createConn();

String sql=”insert into category (name,description) values (?,?)”;

PreparedStatement ps=DB.prepare(conn, sql);

try {

ps.setString(1, c.getName());

ps.setString(2, c.getDescription());

ps.executeUpdate();

} catch (SQLException e) {

e.printStackTrace();

}

DB.close(ps);

DB.close(conn);

}

public ListCategory list(){

Connection conn=DB.createConn();

String sql=”select * from category”;

PreparedStatement ps=DB.prepare(conn, sql);

ListCategory categories=new ArrayListCategory();

try {

ResultSet rs=ps.executeQuery();

Category c=null;

while(rs.next()){

c=new Category();

c.setId(rs.getInt(“id”));

c.setName(rs.getString(“name”));

c.setDescription(rs.getString(“description”));

categories.add(c);

}

} catch (SQLException e) {

e.printStackTrace();

}

DB.close(ps);

DB.close(conn);

return categories;

}

public void delete(Category c){

deleteById(c.getId());

}

public void deleteById(int id){

Connection conn=DB.createConn();

String sql=”delete from category where id=?”;

PreparedStatement ps=DB.prepare(conn, sql);

try {

ps.setInt(1, id);

ps.executeUpdate();

} catch (SQLException e) {

e.printStackTrace();

}

DB.close(ps);

DB.close(conn);

}

public void update(Category c){

Connection conn=DB.createConn();

String sql=”update category set name = ? , description = ? where id = ?”;

PreparedStatement ps=DB.prepare(conn, sql);

try {

ps.setString(1, c.getName());

ps.setString(2, c.getDescription());

ps.setInt(3, c.getId());

ps.executeUpdate();

} catch (SQLException e) {

e.printStackTrace();

}

DB.close(ps);

DB.close(conn);

}

public Category loadById(int id){

Connection conn=DB.createConn();

String sql=”select * from category where id=?”;

PreparedStatement ps=DB.prepare(conn, sql);

Category c=null;

try {

ps.setInt(1, id);

ResultSet rs=ps.executeQuery();

if(rs.next()){

c=new Category();

c.setId(rs.getInt(“id”));

c.setName(rs.getString(“name”));

c.setDescription(rs.getString(“description”));

}

} catch (SQLException e) {

e.printStackTrace();

}

DB.close(ps);

DB.close(conn);

return c;

}

}

如何在JSP頁面中實現對資料庫的增刪查改?

首先我覺得你的問題不太明確,做增刪改查,的話一般不用ajax,除非其中要用到單獨的驗證欄位的時候採用,比如在註冊時驗證用戶名,這裡用到ajax查詢用戶名是否存在,返回給頁面直接用流打回頁面就行(比如:此用戶名可用之類的)而其他的查詢比如顯示所有或者查詢的結果為對象的話那看你用什麼框架(controller),struts直接封裝到值棧中,在頁面用標籤顯示就行,不知道能不能幫到你

jsp連接mysql資料庫後增刪改查怎麼寫

建議使用MVC模式做,JSP頁面提交相應的操作後,提交給Servlet,Servlet中調用Model中定義的增刪改查方法,方法調用後返回結果,然後通過Servlet返回給JSP頁面。對於前台的增刪改查跟資料庫中中新建查詢的操作是一樣的,只是JSP頁面增刪改查是調用資料庫查詢語句封裝的函數方法而已!

jsp增刪改查怎麼寫

package test.dao;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.Statement;

import java.util.ArrayList;

import java.util.List;

import test.entity.Emp;

import test.util.BaseDao;

public class EmpDao{

//查找資料庫Emp表所有人信息

public ListEmp findAll() throws Exception {

Connection conn=BaseDao.getCon();

Statement stat=conn.createStatement();

String sql=”select * from tblemp,tbldept where tbldept.deptid=tblemp.deptid”;

ResultSet rs = stat.executeQuery(sql);

ListEmp list=new ArrayListEmp();

while(rs.next()){

Emp e=new Emp();

e.setEmpid(rs.getInt(“empid”));

e.setEname(rs.getString(“ename”));

e.setDname(rs.getString(“dname”));

list.add(e);

}

BaseDao.close(conn);

return list;

}

//往Emp表添加新員工

public void save(Emp e) throws Exception {

Connection conn=BaseDao.getCon();

String sql=”insert into tblemp(ename,egendar,deptid) values(?,?,?)”;

PreparedStatement prep=conn.prepareStatement(sql);

prep.setString(1, e.getEname());

prep.setDouble(2, e.getEgendar());

prep.setInt(3, e.getDeptid());

prep.executeUpdate();

BaseDao.close(conn);

}

//根據id刪除該員工

public void delete(int id) throws Exception {

Connection conn=BaseDao.getCon();

String sql=”delete from tblemp where empid=?”;

PreparedStatement prep=conn.prepareStatement(sql);

prep.setLong(1, id);

prep.executeUpdate();

BaseDao.close(conn);

}

//根據id查找該員工信息

public Emp findById(int id) throws Exception {

Connection conn=BaseDao.getCon();

Emp e=null;

String sql=”select * from tblemp,tbldept where empid=? and tbldept.deptid=tblemp.deptid”;

PreparedStatement prep=conn.prepareStatement(sql);

prep.setLong(1, id);

ResultSet rs=prep.executeQuery();

if(rs.next()){

e=new Emp();

e.setEmpid(id);

e.setEname(rs.getString(“ename”));

e.setEgendar(rs.getInt(“egendar”));

e.setDname(rs.getString(“dname”));

e.setDeptid(rs.getInt(“deptid”));

}

return e;

}

//根據id修改該員工

public void update(Emp e) throws Exception {

Connection conn=BaseDao.getCon();

String sql=”update tblemp set ename=?,egendar=?,deptid=? where empid=?”;

PreparedStatement prep=conn.prepareStatement(sql);

prep.setString(1, e.getEname());

prep.setInt(2, e.getEgendar());

prep.setInt(3, e.getDeptid());

prep.setLong(4, e.getEmpid());

prep.executeUpdate();

BaseDao.close(conn);

}

}

//我把我寫過的給你參考,你只需要修改成自己的欄位就能用了。

需要用到哪個方法,就調用它。

EmpDao dao = new EmpDao();

//增加就dao.save(數據);

//刪除就dao.delete(id);

//查找就dao.findAll();

//修改就dao.update(內容);

希望幫到你

原創文章,作者:TYGI,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/135645.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
TYGI的頭像TYGI
上一篇 2024-10-04 00:14
下一篇 2024-10-04 00:14

相關推薦

  • Python周杰倫代碼用法介紹

    本文將從多個方面對Python周杰倫代碼進行詳細的闡述。 一、代碼介紹 from urllib.request import urlopen from bs4 import Bea…

    編程 2025-04-29
  • Python字元串寬度不限制怎麼打代碼

    本文將為大家詳細介紹Python字元串寬度不限制時如何打代碼的幾個方面。 一、保持代碼風格的統一 在Python字元串寬度不限制的情況下,我們可以寫出很長很長的一行代碼。但是,為了…

    編程 2025-04-29
  • Python基礎代碼用法介紹

    本文將從多個方面對Python基礎代碼進行解析和詳細闡述,力求讓讀者深刻理解Python基礎代碼。通過本文的學習,相信大家對Python的學習和應用會更加輕鬆和高效。 一、變數和數…

    編程 2025-04-29
  • Python 常用資料庫有哪些?

    在Python編程中,資料庫是不可或缺的一部分。隨著互聯網應用的不斷擴大,處理海量數據已成為一種趨勢。Python有許多成熟的資料庫管理系統,接下來我們將從多個方面介紹Python…

    編程 2025-04-29
  • 掌握magic-api item.import,為你的項目注入靈魂

    你是否曾經想要導入一個模塊,但卻不知道如何實現?又或者,你是否在使用magic-api時遇到了無法導入的問題?那麼,你來到了正確的地方。在本文中,我們將詳細闡述magic-api的…

    編程 2025-04-29
  • openeuler安裝資料庫方案

    本文將介紹在openeuler操作系統中安裝資料庫的方案,並提供代碼示例。 一、安裝MariaDB 下面介紹如何在openeuler中安裝MariaDB。 1、更新軟體源 sudo…

    編程 2025-04-29
  • 倉庫管理系統代碼設計Python

    這篇文章將詳細探討如何設計一個基於Python的倉庫管理系統。 一、基本需求 在著手設計之前,我們首先需要確定倉庫管理系統的基本需求。 我們可以將需求分為以下幾個方面: 1、庫存管…

    編程 2025-04-29
  • Python滿天星代碼:讓編程變得更加簡單

    本文將從多個方面詳細闡述Python滿天星代碼,為大家介紹它的優點以及如何在編程中使用。無論是剛剛接觸編程還是資深程序員,都能從中獲得一定的收穫。 一、簡介 Python滿天星代碼…

    編程 2025-04-29
  • 寫代碼新手教程

    本文將從語言選擇、學習方法、編碼規範以及常見問題解答等多個方面,為編程新手提供實用、簡明的教程。 一、語言選擇 作為編程新手,選擇一門編程語言是很關鍵的一步。以下是幾個有代表性的編…

    編程 2025-04-29
  • Python實現簡易心形代碼

    在這個文章中,我們將會介紹如何用Python語言編寫一個非常簡單的代碼來生成一個心形圖案。我們將會從安裝Python開始介紹,逐步深入了解如何實現這一任務。 一、安裝Python …

    編程 2025-04-29

發表回復

登錄後才能評論