Java-最原始的开发模式,数据模型+主界面(半成版)

1!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//主界面
import javax.swing.*;
import com.mysql.jdbc.PreparedStatement;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Test2 extends JFrame implements ActionListener{
JPanel jp1;
JPanel jp2;
JTable jt;
JButton jb1;
JButton jb2;
JButton jb3;
JButton jb4;
JTextField jtf;
JLabel jl;
JScrollPane js;

public static void main(String[] args) {
    Test2 tt = new Test2();

}

public Test2() {
    jp1 = new JPanel();
    jl = new JLabel("用户名");
    jtf = new JTextField(10);
    jb1 = new JButton("查询");
    jb1.setActionCommand("查询");
    //注册监听
    jb1.addActionListener(this);
    //把按钮加入JPanel
    jp1.add(jtf);
    jp1.add(jl);
    jp1.add(jb1);

    //另一个JPanel
    jp2 = new JPanel();
    jb2 = new JButton("增加");
    jb3 = new JButton("修改");
    jb4 = new JButton("删除");
    jp2.add(jb2);
    jp2.add(jb3);
    jp2.add(jb4);
    //中间的表数据部分,移到了模型类去处理
    //创建模型
    studModel stud = new studModel();
    //将模型添加进去!
    jt = new JTable(stud);
    js = new JScrollPane(jt);
    this.add(js);
    this.add(jp1,"North");
    this.add(jp2,"South");
    this.setSize(400, 300);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setVisible(true);

}

@Override
public void actionPerformed(ActionEvent e) {
    if(e.getActionCommand().equals("查询")) {
    System.out.println("用户想查询");
    }
}

}

2!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//数据模型类,新建一个类窗口,处于同一个包即可
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

import javax.swing.table.*;

import com.mysql.jdbc.PreparedStatement;

public class studModel extends AbstractTableModel{
//连接数据库所需要的控件
Vector rowdata, columnNames;
PreparedStatement ps= null;
Connection ct= null;
ResultSet rs= null;

public studModel() {
    columnNames = new Vector();
    columnNames.add("学号");
    columnNames.add("姓名");
    columnNames.add("性别");
    columnNames.add("年龄");
    columnNames.add("专业");
    rowdata = new Vector();
    try {
        Class.forName("com.mysql.jdbc.Driver");
        ct = DriverManager.getConnection("jdbc:mysql://localhost:3306/stu?useSSL=false","root","root");
        ps= (PreparedStatement) ct.prepareStatement("select * from stud");
        rs =ps.executeQuery();
        while(rs.next()) {

            Vector hang = new Vector();
            hang.add(rs.getString(1));
            hang.add(rs.getString(2));
            hang.add(rs.getString(3));
            hang.add(rs.getInt(4));
            hang.add(rs.getString(5));
            //加入到rowdata中。
            rowdata.add(hang);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    finally {
        //关闭资源

        try {
            if(rs!=null){ rs.close();}
            if(ps!=null){ps.close();}
            if(ct!=null){ct.close();}
        } catch (SQLException e) {

            e.printStackTrace();
        }

    }
}
@Override
public int getColumnCount() {
    // TODO Auto-generated method stub
    return this.columnNames.size();
}
@Override
public int getRowCount() {
    // TODO Auto-generated method stub
    return this.rowdata.size();
}
@Override
public Object getValueAt(int row, int column) {
    // TODO Auto-generated method stub
    return ((Vector)this.rowdata.get(row)).get(column);
}

}

你可能感兴趣的:(Java-最原始的开发模式,数据模型+主界面(半成版))