0
点赞
收藏
分享

微信扫一扫

【汇智学堂】-AJAX+JAVA之一(查询功能的Entity+DAO)


Entity:

package com.inout.entity;

import java.util.Date;

public class Inout {

    private String projectname;
    private String investor;
    private String investdate;
    private String money;
    private String useforwhat;
    private int id;
    private String classification;

    public Inout(){

    }

    public Inout(int id,String a,String b,String c,String d,String e,String f){
        this.id=id;
        this.projectname=a;
        this.investor=b;
        this.investdate=c;
        this.money=d;
        this.useforwhat=e;
        this.classification=f;
    }

    public String getProjectname() {
        return projectname;
    }

    public void setProjectname(String projectname) {
        this.projectname = projectname;
    }

    public String getInvestor() {
        return investor;
    }

    public void setInvestor(String investor) {
        this.investor = investor;
    }

    public String getInvestdate() {
        return investdate;
    }

    public void setInvestdate(String investdate) {
        this.investdate = investdate;
    }

    public String getMoney() {
        return money;
    }

    public void setMoney(String money) {
        this.money = money;
    }

    public String getUseforwhat() {
        return useforwhat;
    }

    public void setUseforwhat(String useforwhat) {
        this.useforwhat = useforwhat;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getClassification() {
        return classification;
    }

    public void setClassification(String classification) {
        this.classification = classification;
    }
}

DAO中基础类:BaseDao

package com.inout.dao;

import java.sql.*;

public class BaseDao {

    public static final String DRIVER = "com.mysql.jdbc.Driver";
    public static final String URL = "jdbc:mysql://127.0.0.1:3306/test?characterEncoding=UTF-8";
    public static final String USER = "root";
    public static final String PASSWORD = "123456";

    Connection conn = null;
    PreparedStatement pstmt = null;	ResultSet rs = null;
    public void getConnection() {
        try {
            Class.forName(DRIVER);
            conn = DriverManager.getConnection(URL, USER, PASSWORD);
        }
        catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public int executeUpdate(String sql, Object... obj) {
        int num = 0;
        getConnection();
        try {
            pstmt = conn.prepareStatement(sql);
            for (int i = 0; i < obj.length; i++) {
                pstmt.setObject(i + 1, obj[i]);
            }
            num = pstmt.executeUpdate();
        }
        catch (SQLException e) {
            e.printStackTrace();
        }
        finally {
            closeAll();
        }
        return num;
    }
    public ResultSet executeQuery(String sql, Object... obj) {
        getConnection();
        try {
            pstmt = conn.prepareStatement(sql);
            for (int i = 0; i < obj.length; i++) {
                pstmt.setObject(i + 1, obj[i]);
            }
            rs = pstmt.executeQuery();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return rs;
    }
    public void closeAll() {
        try {
            if(rs!= null) {
                rs.close();
            }

        }
        catch (SQLException e) {
            e.printStackTrace();
        }		try {
            pstmt.close();
        }
        catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            conn.close();
        }
        catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

DAO接口:InoutDao

package com.inout.dao;

import java.net.PortUnreachableException;
import java.util.List;
import com.inout.entity.Inout;

public interface InoutDao {
    public List<Inout> findInoutByProjectname(String projectname);
    public  List<Inout> findAllInout(); 
    public List<Inout> getInoutByPage(int start, int size);
    }

DAO实现类:

package com.inout.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.inout.entity.Inout;

public class InoutDaoImpl extends BaseDao implements InoutDao {
    public List<Inout> findInoutByProjectname(String projectname){
        Inout inout = null;
        List<Inout> ilist=new ArrayList<Inout>();
        String sql = "select id, projectname,investor,investdate,money,useforwhat,classification from tb_inout where projectname like \"%\"?\"%\"";
        rs = executeQuery(sql, projectname);

        try {
            while (rs.next()) {          
                inout = new Inout(rs.getInt(1),rs.getString(2), rs.getString(3),rs.getString(4),rs.getString(5),rs.getString(6),rs.getString(7));
                ilist.add(inout);
            }
        }
        catch (SQLException e) {
            e.printStackTrace();
        }
        return ilist;
    }
}

附-表 tb_inout结构:

【汇智学堂】-AJAX+JAVA之一(查询功能的Entity+DAO)_java


举报

相关推荐

0 条评论