JSP的一个增删改查例子和总结

更新时间:2023-05-12 02:58:39 阅读: 评论:0

JSP的⼀个增删改查例⼦和总结
总结的⼏点:
  1、在jsp中注意<%! %>声明代码块中的变量只会在项⽬开始的时候第⼀次运⾏jsp的时候执⾏⼀遍,有点类似于java类中的static代码块,所以如果是会改变的值不应该声明在这⾥⾯。⽽是卸载<%%>代码块中
  2、使⽤js中的location.href有时候就是⽆法⽣效,也就是⽆法跳转到你想要的页⾯。你可以在location.href语句后⾯加上
  3、进⾏编辑⼀条信息或者删除信息的时候id字段可以使⽤隐藏域或者直接使⽤el传递。这样就不需要通过js找到id列或者其他了
  4、在增加的时候注意在rvlet或者对应的jsp进⾏对象的补全
  5、在修改的时候如果有那种类似于下拉列表或者单选按钮的东西,可以使⽤jstl中的<c:if>实现选择。
例⼦:
CREATE TABLE profile(
id NUMBER PRIMARY KEY,
name VARCHAR2(20),
birthday DATE,
gender VARCHAR2(10),
career VARCHAR2(20),
address VARCHAR2(50),
mobile VARCHAR2(11)
);
CREATE SEQUENCE q_profile;
package com.dao;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import s.dbutils.QueryRunner;
import s.dbutils.ResultSetHandler;
import s.dbutils.handlers.BeanHandler;
import s.dbutils.handlers.BeanListHandler;
import com.domain.Profile;
import com.jdbc.JdbcUtils;
public class ProfileDao {
/**
* zeng
* @param p
* @throws SQLException
*/
public void addProfile(Profile p) throws SQLException{
String sql="INSERT INTO profile VALUES (q_profile.NEXTVAL,?,?,?,?,?,?)";
QueryRunner qr=new QueryRunner();
Connection Connection();
Object[] params={p.getName(),p.getBirthday(),p.getGender(),p.getCareer(),p.getAddress(),p.getMobile()};
qr.update(con, sql,params);
}
public void deleteById(int id) throws SQLException{
String sql="DELETE FROM profile WHERE id=?";
String sql="DELETE FROM profile WHERE id=?";
QueryRunner qr=new QueryRunner();
Connection Connection();
Object[] params={id};
qr.update(con,sql, params);
}
public void update(Profile p) throws SQLException{
String sql="UPDATE profile SET name=?,birthday=?,gender=?,career=?,address=?,mobile=? WHERE id=?";
QueryRunner qr=new QueryRunner();
Connection Connection();
Object[] params={p.getName(),p.getBirthday(),p.getGender(),p.getCareer(),p.getAddress(),p.getMobile(),p.getId()}; //    System.out.String(params));
qr.update(con,sql, params);
}
public ArrayList<Profile> findAll() throws SQLException{
String sql="SELECT * FROM profile";
QueryRunner qr=new QueryRunner();
Connection Connection();
ResultSetHandler<List<Profile>> rsh=new BeanListHandler<Profile>(Profile.class);
ArrayList<Profile> profiles=(ArrayList<Profile>) qr.query(con, sql, rsh);
return profiles;
}
public Profile load(int id) throws SQLException{
String sql="SELECT * FROM profile WHERE id=?";
QueryRunner qr=new QueryRunner();
Object[] params={id};
Connection Connection();
ResultSetHandler<Profile> rsh=new BeanHandler<Profile>(Profile.class);
Profile profile= (Profile) qr.query(con, sql, rsh,params);
return profile;
}
}
package com.domain;
import java.io.Serializable;
import java.sql.Date;
public class Profile implements Serializable{
private static final long rialVersionUID = 1L;
private int id;
private String name;
private Date birthday;
private String gender;
private String career;
private String address;
private String mobile;
@Override
public String toString() {
return "Profile [id=" + id + ", name=" + name + ", birthday="
+ birthday + ", gender=" + gender + ", career=" + career
+ ", address=" + address + ", mobile=" + mobile + "]";
}
public int getId() {
return id;
}
}
public void tId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void tName(String name) {
this.name = name;
}
public Date getBirthday() {
return birthday;
}
public void tBirthday(Date birthday) {
this.birthday = birthday;
}
public String getGender() {
return gender;
}
public void tGender(String gender) {
}
public String getCareer() {
return career;
}
public void tCareer(String career) {
this.career = career;
}
public String getAddress() {
return address;
}
public void tAddress(String address) {
this.address = address;
}
public String getMobile() {
return mobile;
}
public void tMobile(String mobile) {
}
public Profile() {
super();
// TODO Auto-generated constructor stub
}
public Profile(int id, String name, Date birthday, String gender,            String career, String address, String mobile) {
super();
this.id = id;
this.name = name;
this.birthday = birthday;
this.career = career;
this.address = address;
}
}
package com.jdbc;
import java.sql.Connection;
import java.sql.SQLException;
import javax.sql.DataSource;
hange.v2.c3p0.ComboPooledDataSource;
hange.v2.c3p0.ComboPooledDataSource;
public class JdbcUtils {
/*
* 配置⽂件的恶魔⼈配置!要求你必须给出c3p0-config。xnl!
*/
private static ComboPooledDataSource dataSource=new ComboPooledDataSource("oracle-config"); /**
* 它是事务专⽤连接
*/
private static Connection con=null;
/**
* 使⽤连接池返回⼀个连接对象
* @return
* @throws SQLException
*/
public static Connection getConnection() throws SQLException{
//当con!=null,表⽰已经调⽤过beginTransaction⽅法了
if(con!=null) return con;
Connection();
}
/
**
* 返回连接池对象
* @return
*/
public static DataSource getDataSource(){
return dataSource;
}
/**
* 1、开启⼀个Connection,设置它的tAutoCommit(fal)
* 2、还要保证dao中使⽤的连接是我们刚刚创建的
* ------------------------
* 1、创建⼀个Connection,设置为⼿动提交
* 2、把这个Connection给dao⽤
* 3、还要让commitTransaction或rollbackTransaction可以获取到
* @throws SQLException
*/
public static void beignTransaction() throws SQLException{
if(con!=null) throw new SQLException("已经开始了事务,就不要继续开启事务了!");
con=getConnection();
con.tAutoCommit(fal);
}
/**
* 提交事务
* 获取之前开启的Connection,兵提交
* @throws SQLException
*/
public static void commitTransaction() throws SQLException{
if(con==null) throw new SQLException("还没有开启事务,不能提交!");
con.clo();
con=null;//因为前⾯的clo()不会销毁连接⽽是放回连接池
}
/**
* 回滚事务
* 获取之前开启的Connection,兵回滚
* @throws SQLException
*/
public static void rollbackTransaction() throws SQLException{
if(con==null) throw new SQLException("还没有开启事务,不能提交!");
con.clo();
con=null;//因为前⾯的clo()不会销毁连接⽽是放回连接池
}
public static void releaConnection(Connection connection) throws SQLException{ /*
*判斷它是不是中事務專⽤,如果是就不關閉
*如果不是就要關閉
*/
//如果con==null,說明沒有事務,那麼connection⼀定不是事務專⽤的
if(con==null)    connection.clo();
if(con!=connection) connection.clo();
}
}
package com.rvice;
import java.sql.SQLException;
import java.util.ArrayList;
import com.dao.ProfileDao;
import com.domain.Profile;
public class ProfileService {
private ProfileDao profileDao=new ProfileDao();
public void addProfile(Profile p){
try {
profileDao.addProfile(p);
} catch (SQLException e) {
e.printStackTrace();
}
}
public void deleteProfile(int id){
try {
profileDao.deleteById(id);
} catch (SQLException e) {
e.printStackTrace();
}
}
public void updateProfile(Profile p){
try {
profileDao.update(p);
} catch (SQLException e) {
e.printStackTrace();
}
}
public ArrayList<Profile> findAll(){
try {
return profileDao.findAll();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public Profile findById(int id){
try {
return profileDao.load(id);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}

本文发布于:2023-05-12 02:58:39,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/89/886025.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:时候   事务   连接   信息   开启   改查   提交   类似
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图