本文目錄一覽:
怎麼用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