码迷,mamicode.com
首页 > Web开发 > 详细

我的第一个MVC项目

时间:2014-05-08 09:50:18      阅读:486      评论:0      收藏:0      [点我收藏+]

标签:style   blog   class   code   java   ext   

bubuko.com,布布扣
这是本人第一个完全按照MVC开发模式完成的项目,非常简陋,仅仅完成的查询,保存,删除基本功能,而且存在编码问题没有解决,以后会慢慢增加功能。慢慢改进
bubuko.com,布布扣pbubuko.com,布布扣ackage com.sdjt.dao;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ArrayListHandler;
import org.junit.Test;

import com.sdjt.utils.C3Utils;

/**
 * 实现操作增删改查
 * 
 * @author lx
 * 
 */

public class PersonDaoJdb {
    /*
     * 实现查询
     */

    public List<Object[]> query(String id, String name, Integer age, String sex) {
        List<Object[]> list = new ArrayList<>();
        try {

            String sql = "select p_id id,p_name name,p_age age,"
                    + "(case sex when ‘1‘ then ‘男‘ else ‘女‘ end) sex"
                    + " from person where 1=1";

            List<Object> param = new ArrayList<>();
            if (id != null && !id.trim().equals("")) {
                sql += " and p_id like ?";
                param.add("%" + id.trim() + "%");
            }
            if (name != null && !name.trim().equals("")) {
                sql += " and p_name like ?";
                param.add("%" + name.trim() + "%");
            }
            if (age != null) {
                sql += " and p_age=?";
                param.add(age);
            }
            if (sex != null && !sex.trim().equals("")) {
                sql += " and sex=?";
                param.add(sex.trim());
            }
            QueryRunner run = new QueryRunner(C3Utils.getDs());
        
            list = run.query(sql, new ArrayListHandler(), param.toArray());
            System.err.println("sql is:"+sql);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return list;
    }

    /**
     * 实现保存
     */

    public void save(String id, String name,String age, String sex) {

        try {

            String sql = "insert into person values(?,?,?,?)";
            QueryRunner run = new QueryRunner(C3Utils.getDs());
            run.update(sql, id,name,age,sex);

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public void delete(String id, String name,String age, String sex) {

        try {
            String sql = "delete from person where ";
            if (id != null && !id.trim().equals("")) {
                sql += "p_id= ?";
                QueryRunner run = new QueryRunner(C3Utils.getDs());
                run.update(sql,id);
                System.err.println("删除成功");

            } else {
                System.err.println("删除出错");

            }

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}

package com.sdjt.frame;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Label;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;

import com.sdjt.person.Action.PersonAction;
import com.sdjt.utils.MyModelFrame;

public class Frame extends JFrame {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    Label lable1 = new Label("编号");
    Label lable2 = new Label("姓名");
    Label lable3 = new Label("年龄");
    Label lable4 = new Label("性别");
    JButton save = new JButton("保存");
    JButton delete = new JButton("删除");
    JTextField idtext = new JTextField(5);
    JTextField nametext = new JTextField(5);
    JTextField agetext = new JTextField(5);
    JTable table;
    DefaultTableModel dt;
    // 下拉框
    JComboBox<String> sextext = new JComboBox<String>(new String[] { "", "男",
            "女" });


    public Frame() {
        // 控制层 接受用户信息
        PersonAction pa = new PersonAction(this);
        // 获取当前jframe上的对象
        Container con = getContentPane();
        con.setLayout(new BorderLayout());
        JPanel jp = new JPanel(new FlowLayout(FlowLayout.LEFT));
        jp.add(lable1);
        jp.add(idtext);
        jp.add(lable2);
        jp.add(nametext);
        jp.add(lable3);
        jp.add(agetext);
        jp.add(lable4);
        jp.add(sextext);
        JButton query = new JButton("查询");
        query.setActionCommand("query");
        jp.add(query);
        query.addActionListener(pa);
        save.setActionCommand("save");
        jp.add(save);
        save.addActionListener(pa);
        
        delete.setActionCommand("delete");
        jp.add(delete);
        delete.addActionListener(pa);

        // 实例化表格数据+表头
        dt = new MyModelFrame(null, new String[] { "编号", "name", "age", "sex" });
        con.add(jp, BorderLayout.NORTH);
        table = new JTable(dt);
        // //设置单行选择
        table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        // 将t放在中间用JScrollPanel设置table可以带上下滚动条
        con.add(new JScrollPane(table), BorderLayout.CENTER);

        // 监听窗口关闭按钮
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(300, 300, 700, 300);
        setVisible(true);
    }

    public JTable getTable() {
        return table;
    }

    public JTextField getIdtext() {
        return idtext;
    }

    public JTextField getNametext() {
        return nametext;
    }

    public JTextField getAgetext() {
        return agetext;
    }

    public JComboBox<String> getSextext() {
        return sextext;
    }

    public static void main(String[] args) {
        new Frame();
    }

    public DefaultTableModel getDt() {
        return dt;
    }

}
package com.sdjt.person.Action;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import javax.swing.JOptionPane;
import com.sdjt.frame.Frame;
import com.sdjt.person.serverce.PersonServer;

public class PersonAction implements ActionListener {

    // 声明serverce
    private PersonServer service = new PersonServer();
    private Frame fr;

    public PersonAction(Frame fr) {
        this.fr = fr;
    }

    @Override
    public void actionPerformed(ActionEvent e) {

        if (e.getActionCommand().equals("query")) {

            System.err.println("query..");
            // 删除原有的
            int rows = fr.getDt().getRowCount();
            for (int i = 0; i < rows; i++) {
                fr.getDt().removeRow(0);

            }

            // 查询
            String id = fr.getIdtext().getText();
            String name = fr.getNametext().getText();
            String age = fr.getAgetext().getText();
            String sex = fr.getSextext().getSelectedItem().toString();

            if (sex.equals("男")) {
                sex = "1";

            } else if (sex.equals("女")) {
                sex = "0";

            }

            Integer _age = age.equals("") ? null : Integer.valueOf(age);
            System.err.println(id + "," + name + "," + age + "," + sex);

            // 调用serverce

            // List<Object[]> list = service.query(id, name, _age, sex);
            List<Object[]> list = service.query(id, name, _age, sex);
            for (Object[] oo : list) {
                fr.getDt().addRow(oo);
                System.err.println("添加成功");

            }

        } else if (e.getActionCommand().equals("save")) {

            // 获取数据
            String id = fr.getIdtext().getText();
            String name = fr.getNametext().getText();
            String age = fr.getAgetext().getText();
            String sex = fr.getSextext().getSelectedItem().toString();
            if (id == null || id.trim().equals("")) {
                JOptionPane.showMessageDialog(fr, "必须输入编号");
                return;
            }
            if (sex == null || sex.trim().equals("")) {
                JOptionPane.showMessageDialog(fr, "必须输入性别");
                return;
            }
            // 调用保存

            service.save(id, name, age, sex);
            // 保存成功后
            fr.getDt().addRow(new String[] { id, name, age, sex });
            System.err.println("baocn");

        } else if (e.getActionCommand().equals("delete")) {
            // String id = fr.getIdtext().getText();
            String name = fr.getNametext().getText();
            String age = fr.getAgetext().getText();
            String sex = fr.getSextext().getSelectedItem().toString();
            int row = fr.getTable().getSelectedRow();
            if (row == -1) {
                JOptionPane.showMessageDialog(fr, "plz choose one row...");
                return;

            }
            String id1 = (String) fr.getDt().getValueAt(row, 0);
            int a = JOptionPane.showConfirmDialog(fr, "你确定要删除" + id1 + "吗?",
                    "删除", JOptionPane.YES_NO_OPTION);
            service.delete(id1, name, age, sex);
            fr.getDt().addRow(new String[] { id1, "已经被删除" });
            // System.err.println("--" + a);
            // Integer _age = age.equals("") ? null : Integer.valueOf(age);
            // List<Object[]> list = service.query(id1, name, _age, sex);
            // for (Object[] oo : list) {
            // fr.getDt().addRow(oo);
            // System.err.println("添加成功");
            //
            // }

        }

    }
}
package com.sdjt.person.serverce;

import java.util.List;

import com.sdjt.dao.PersonDaoJdb;


public class PersonServer {

    // 声明dao 注入dao
    private PersonDaoJdb dao = new PersonDaoJdb();

    public List<Object[]> query(String id, String name, Integer age, String sex) {
        return dao.query(id, name, age, sex);

    }

    public void save(String id, String name, String age, String sex) {

        sex = sex.equals("男") ? "1" : "0";
        dao.save(id, name, age, sex);

    }

    public void delete(String id, String name,String age, String sex) {

        dao.delete(id, name, age, sex);

    }

}
package com.sdjt.utils;

import java.sql.Connection;

import javax.sql.DataSource;

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class C3Utils {
    private static DataSource ds;
    static{
        ds=new ComboPooledDataSource();
        
    }

    public static DataSource getDs() {
        return ds;
    }

    public static Connection getCon() {
        Connection con = null;
        try {

            con = ds.getConnection();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return con;

    }
//    public static void main(String[] args) {
//        System.err.println(C3Utils.getCon());
//    }

}
package com.sdjt.utils;

import javax.swing.table.DefaultTableModel;

/**
 * 表格模版
 * 
 * @author lx
 * 
 */
public class MyModelFrame extends DefaultTableModel {

    public MyModelFrame(Object[][] data, Object[] header) {

        super(data, header);
    }
    //返回false不能修改数据
    @Override
    public boolean isCellEditable(int row, int column) {
        if(row==2){
            return true;
            
        }
        return false;
    }
    

}
<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
    <default-config>
        <!-- 最多可以创建多少个statement -->
        <!-- 最多可以创建多少个statement -->
        <property name="maxStatements">50</property>
        <!-- 最多有多少个连接 -->
        <property name="maxPoolSize">4</property>
        <property name="initialPoolSize">2</property>
        <property name="acquireIncrement">2</property>
        <property name="minPoolSize">2</property>
        <!-- 连接的超时间 -->
        <property name="checkoutTimeout">3000</property>
        <property name="driverClass">com.mysql.jdbc.Driver</property>
        <property name="jdbcUrl">jdbc:mysql:///abc
        </property>
        <property name="user">root</property>
        <property name="password">1234</property>
    </default-config>

    <!-- 配置其他的连接,用户的其他配置可以有多个 -->
    
    
    <named-config name="jtedu">
    
    <property name="maxStatements">50</property>
        <!-- 最多有多少个连接 -->
        <property name="maxPoolSize">4</property>
        <property name="initialPoolSize">2</property>
        <property name="acquireIncrement">2</property>
        <property name="minPoolSize">2</property>
        <!-- 连接的超时间 -->
        <property name="checkoutTimeout">3000</property>
        <property name="driverClass">com.mysql.jdbc.Driver</property>
        <property name="jdbcUrl">jdbc:mysql:///abc
        </property>
        <property name="user">root</property>
        <property name="password">1234</property>
     </named-config> 
</c3p0-config>
bubuko.com,布布扣

我的第一个MVC项目,布布扣,bubuko.com

我的第一个MVC项目

标签:style   blog   class   code   java   ext   

原文地址:http://www.cnblogs.com/xiaweifeng/p/3714524.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!