| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 561 人关注过本帖
标题:java通讯录文件版(没用数据库做)
只看楼主 加入收藏
麒麟崽子
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2009-8-12
结帖率:100%
收藏
 问题点数:0 回复次数:0 
java通讯录文件版(没用数据库做)
package com.softeem.jps;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class LoginuserDAO {
public static void main(String[] args) {
    
    LoginuserDAO s = new LoginuserDAO();
    // 查询
    List<Loginuser> h = s.selectAll();
    int i = 0;
    Iterator it = h.iterator();
    while (it.hasNext()) {
        System.out.println(h.get(i).getUserId());
        System.out.println("\t" + h.get(i).getUserName());
        System.out.println("\t" + h.get(i).getUserPass());
        i++;
        it.next();
    }
    
    
    
    
}
    public void insert(Loginuser user) {
        Connection conn = null;
        try {
            conn = DBConnection.getConnection();

            String sql = "insert into loginuser(username,userpass) values(?,?)";
            PreparedStatement pst = conn.prepareStatement(sql);
            pst.setString(1, user.getUserName());
            pst.setString(2, user.getUserPass());
            pst.executeUpdate();
            pst.close();

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBConnection.closeConnection(conn);
        }
    }

    /*
     * (non-Javadoc)
     *
     * @see com.edu.jdbc.ILoginuserDAO#update(com.edu.jdbc.Loginuser)
     */
    public void update(Loginuser user) {
        Connection conn = null;
        try {
            conn = DBConnection.getConnection();

            String sql = "update loginuser set username=?,userpass=? where userid=?";
            PreparedStatement pst = conn.prepareStatement(sql);
            pst.setString(1, user.getUserName());
            pst.setString(2, user.getUserPass());
            pst.setInt(3, user.getUserId());
            pst.executeUpdate();
            pst.close();

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBConnection.closeConnection(conn);
        }
    }

    /*
     * (non-Javadoc)
     *
     * @see com.edu.jdbc.ILoginuserDAO#delete(com.edu.jdbc.Loginuser)
     */
    public void delete(Loginuser user) {
        Connection conn = null;
        try {
            conn = DBConnection.getConnection();

            String sql = "delete from loginuser where userid=?";
            PreparedStatement pst = conn.prepareStatement(sql);
            pst.setInt(1, user.getUserId());
            pst.executeUpdate();
            pst.close();

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBConnection.closeConnection(conn);
        }
    }

    /*
     * (non-Javadoc)
     *
     * @see com.edu.jdbc.ILoginuserDAO#selectAll()
     */
    public ArrayList<Loginuser> selectAll() {
        ArrayList<Loginuser> users = new ArrayList<Loginuser>();
        Connection conn = null;
        try {
            conn = DBConnection.getConnection();

            String sql = "select * from loginuser";
            PreparedStatement pst = conn.prepareStatement(sql);
            ResultSet rs = pst.executeQuery();

            while (rs.next()) {
                Loginuser user = new Loginuser();
                user.setUserId(rs.getInt("userid"));
                user.setUserName(rs.getString("username"));
                user.setUserPass(rs.getString("userpass"));
                users.add(user);
            }

            rs.close();
            pst.close();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBConnection.closeConnection(conn);
        }

        return users;
    }

    /*
     * (non-Javadoc)
     *
     * @see com.edu.jdbc.ILoginuserDAO#selectByID(int)
     */
    public Loginuser selectByID(int userId) {
        Connection conn = null;
        Loginuser user = new Loginuser();
        try {
            conn = DBConnection.getConnection();

            String sql = "select * from loginuser where userid=?";
            PreparedStatement pst = conn.prepareStatement(sql);
            pst.setInt(1, userId);
            ResultSet rs = pst.executeQuery();

            while (rs.next()) {
                user.setUserId(rs.getInt("userid"));
                user.setUserName(rs.getString("username"));
                user.setUserPass(rs.getString("userpass"));
            }

            rs.close();
            pst.close();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBConnection.closeConnection(conn);
        }

        return user;
    }

    public boolean checkLogin(Loginuser user) {
        boolean flag = false;
        Connection conn = null;
        try {// 1.2
            conn = DBConnection.getConnection();
            // 3
            String sql = "select count(*) from loginuser where username=? and userpass=?";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, user.getUserName());
            ps.setString(2, user.getUserPass());
            // 4
            ResultSet rs = ps.executeQuery();
            // 5
            while (rs.next()) {
                System.out.println(rs.getInt(1));
                if (rs.getInt(1) > 0) {
                    flag = true;
                }
            }// 6
            rs.close();
            ps.close();

        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {// 6
            DBConnection.closeConnection(conn);
        }

        return flag;
    }

}
搜索更多相关主题的帖子: DAO封装 
2009-08-16 18:24
快速回复:java通讯录文件版(没用数据库做)
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.023148 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved