⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 userinfodao.java

📁 《struts 应用开发完全手册》中的源代码第一到第五章。
💻 JAVA
字号:
package com.dao;
import java.sql.*;
import java.util.*;
import com.domain.UserInfoForm;

public class UserInfoDao {
    private JDBConnection connection = null;
    public UserInfoDao() {
        connection = new JDBConnection();
        this.connection.creatConnection(); //利用构造方法调用类中的对象对数据库创建连接
    }

    //查询用户信息的操作
        public List userList() {
            List list = new ArrayList();
            UserInfoForm form = null;
            String sql = "select * from tb_user order by id";
            ResultSet rs = connection.executeQuery(sql);
            try {
                while (rs.next()) {
                    form = new UserInfoForm();
                    form.setId(Integer.valueOf(rs.getString(1)));
                    form.setName(rs.getString(2));
                    form.setAge(Integer.valueOf(rs.getString(3)));
                    form.setProfession(rs.getString(4));
                    list.add(form);
                }
            } catch (SQLException ex) {
            }
            connection.closeConnection();
            return list;
        }


//添加操作
        public void addUserInfo(UserInfoForm form) {
            String sql = "insert into tb_user values ('" + form.getName() +
                         "','" +
                         form.getAge() + "','" + form.getProfession() + "')"
                         ;
            connection.executeUpdate(sql);
            connection.closeConnection();
        }

        //条件查询操作
        public UserInfoForm userList(Integer id) {

             UserInfoForm form = null;
             String sql = "select * from tb_user where id="+id+"";
             ResultSet rs = connection.executeQuery(sql);
             try {
                 while (rs.next()) {
                     form = new UserInfoForm();
                     form.setId(Integer.valueOf(rs.getString(1)));
                     form.setName(rs.getString(2));
                     form.setAge(Integer.valueOf(rs.getString(3)));
                     form.setProfession(rs.getString(4));
                 }
             } catch (SQLException ex) {
             }
             connection.closeConnection();
             return form;
        }




        //修改操作
        public void updateUserInfo(UserInfoForm form) {
            String sql = "update tb_user set name='" + form.getName() + "',age=" +
                         form.getAge() + ",profession='" + form.getProfession() +
                         "' where id='" + form.getId() + "'";
            connection.executeUpdate(sql);
            connection.closeConnection();
  }


  //删除操作
      public void deleteUserInfo(Integer id) {
          String sql = "delete from tb_user where id=" + id + "";
          connection.executeUpdate(sql);
          connection.closeConnection();
      }



}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -