目录
零、项目功能实现
1、总功能
对界面中的表格进行点击可以对其姓名、年龄、性别、所在地区等进行修改,点击添加校友可以对其进行添加,返回到表格界面时,对数据进行更新。
2、代码操作解释
dao:对各自管理的信息进行增删改查的操作,定义函数操作
model:对需要的信息元素进行声明,以及getter和setter的函数,以及构造函数对对象进行设置
util:工具类-》对数据库进行连接、对字符串是否为空进行判断
view:界面设计以及对应代码操作的调用
3、对于界面的操作设置
均可以在eclipse中的插件windowbuilder中进行对应的设置,操作简单,多多操作看看代码就可以实现
4、表格与数据库中的数据进行连接
需要设置专门的fillsalumnus等的函数对其进行连接,将表格进行填充,代码在下
public void fillsalumnus(Alumnus alumnus) {
DefaultTableModel dtm = (DefaultTableModel) table.getModel();
dtm.setRowCount(0);
Connection con = null;
try {
con = dbUtil.getCon();//获取数据库的连接
ResultSet rs = alumnusDao.alumnusList(con, alumnus);
while (rs.next()) {
Vector v = new Vector();
v.add(rs.getInt("aid"));
v.add(rs.getString("aname"));
v.add(rs.getInt("aage"));
v.add(rs.getString("agender"));
v.add(rs.getString("afrom"));
dtm.addRow(v);
}//填充表格
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
dbUtil.closeCon(con);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
5、jtable的一些小小操作代码
获取鼠标点击的表格的行数:
注意:表格中每一行的元素,列是从下标0开始获取,下为得到表格第hang行第0列的元素值等等,注意数据类型的对应
int aid = (int)this.table.getValueAt(hang,0);
String aname = (String) this.table.getValueAt(hang,1);
int aage;
aage = (int)this.table.getValueAt(hang,2);
String agender = (String) this.table.getValueAt(hang,3);
String afrom = (String) this.table.getValueAt(hang,4);
String achieve = this.textField_1.getText();//获取文本框textField_1中的字符串
6、界面背景设置
选取jlable等标签(将其大小拉至所需界面大小,可以记住),把文本去掉,设置其icon(可以选取绝对路径,图片大小与jlable的框框大小一致就不用调节大小了,最省力的办法)
一、运行界面展示
二、部分代码展示及个人理解
1、AlumnusDao
package com.java.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import com.java.model.Alumnus;
import com.java.util.StringUtil;
public class AlumnusDao {
//添加校友信息
public int alumnusAdd(Connection con, Alumnus alumnus) throws Exception{
String sql="insert into alumnus values(null,?,?,?,?,?)";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setString(1, alumnus.getAname());
pstmt.setInt(2, alumnus.getAage());
pstmt.setString(3, alumnus.getAgender());
pstmt.setString(4, alumnus.getAfrom());
pstmt.setString(5, alumnus.getAchieve());
return pstmt.executeUpdate();
}
//展示校友信息
public ResultSet alumnusList(Connection con,Alumnus alumnus) throws Exception{
StringBuffer sb=new StringBuffer("select * from alumnus");
if(StringUtil.isNotEmpty(alumnus.getAname())) {
sb.append(" and aname = '"+alumnus.getAname()+"'");
}
PreparedStatement pstmt=con.prepareStatement(sb.toString().replaceFirst("and", "where"));
return pstmt.executeQuery();
}
//删除校友信息
public int alumnusDelete(Connection con,String aname)throws Exception{
String sql="delete from alumnus where aname=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setString(1, aname);
return pstmt.executeUpdate();
}
//修改校友信息
public int alumnusModify(Connection con, Alumnus alumnus)throws Exception{
String sql="update alumnus set aage=?,agender=?,afrom=?,achieve=?,aname=? where aid=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, alumnus.getAage());
pstmt.setString(2, alumnus.getAgender());
pstmt.setString(3, alumnus.getAfrom());
pstmt.setString(4,alumnus.getAchieve());
pstmt.setString(5,alumnus.getAname());
pstmt.setInt(6, alumnus.getAid());
return pstmt.executeUpdate();
}
}
2、Alumnus
package com.java.model;
/**
* 校友Model类
* @author somewon
*
*/
public class Alumnus {
private int aid;
private String aname;
private int aage;
private String agender;
private String afrom;
private String achieve;
public Alumnus() {
super();
}
public Alumnus(String aname) {
this.aname = aname;
}
public Alumnus(int aid, String aname, int aage, String agender, String afrom, String achieve) {
super();
this.aid = aid;
this.aname = aname;
this.aage = aage;
this.agender = agender;
this.afrom = afrom;
this.achieve = achieve;
}
//成员get和set
public int getAid() {
return aid;
}
public void setAid(int aid) {
this.aid = aid;
}
public String getAname() {
return aname;
}
public void setAname(String aname) {
this.aname = aname;
}
public int getAage() {
return aage;
}
public void setAage(int aage) {
this.aage = aage;
}
public String getAgender() {
return agender;
}
public void setAgender(String agender) {
this.agender = agender;
}
public String getAfrom() {
return afrom;
}
public void setAfrom(String afrom) {
this.afrom = afrom;
}
public String getAchieve() {
return achieve;
}
public void setAchieve(String achieve) {
this.achieve = achieve;
}
}
3、AlumnusController
package com.java.view;
import com.java.dao.AlumnusDao;
import com.java.model.Alumnus;
import com.java.util.DbUtil;
import com.java.util.StringUtil;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.Vector;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class AlumnusController extends JFrame {
private JPanel contentPane;
private JTextField textField;
DbUtil dbUtil = new DbUtil();
AlumnusDao alumnusDao = new AlumnusDao();
private JTable table;
private JTextField textField_1;
/**
* Launch the application.
*/
public void fillsalumnus(Alumnus alumnus) {
DefaultTableModel dtm = (DefaultTableModel) table.getModel();
dtm.setRowCount(0);
Connection con = null;
try {
con = dbUtil.getCon();
ResultSet rs = alumnusDao.alumnusList(con, alumnus);
while (rs.next()) {
Vector v = new Vector();
v.add(rs.getInt("aid"));
v.add(rs.getString("aname"));
v.add(rs.getInt("aage"));
v.add(rs.getString("agender"));
v.add(rs.getString("afrom"));
dtm.addRow(v);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
dbUtil.closeCon(con);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//成就的框显现文字
protected void chengjiu() {
// TODO Auto-generated method stub
//选中行
int hang = this.table.getSelectedRow();
//获取数据
String issuer = (String) this.table.getValueAt(hang,1); //所在行的校友名字
Alumnus alumnus = new Alumnus(issuer);
Connection con = null;
//获得连接
try {
con = dbUtil.getCon();
ResultSet rs = alumnusDao.alumnusList(con, alumnus);
if(rs.next()) {
String chengjiu = rs.getString("achieve");
this.textField_1.setText(chengjiu);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
AlumnusController frame = new AlumnusController();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public AlumnusController() {
setFont(new Font("幼圆", Font.PLAIN, 14));
setTitle("校友系统");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 900, 633);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JDesktopPane desktopPane = new JDesktopPane();
desktopPane.setBackground(new Color(199, 39, 12));
desktopPane.setBounds(0, 0, 893, 590);
contentPane.add(desktopPane);
// this.fillsalumnus(new Alumnus());
JButton btnNewButton_1 = new JButton("修改");
btnNewButton_1.setBounds(533, 66, 69, 29);
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
xiugai();
fillsalumnus(new Alumnus());
}
});
desktopPane.setLayout(null);
btnNewButton_1.setBackground(new Color(255, 255, 255));
btnNewButton_1.setFont(new Font("宋体", Font.PLAIN, 14));
desktopPane.add(btnNewButton_1);
JButton btnNewButton_1_2 = new JButton("删除");
btnNewButton_1_2.setBounds(650, 66, 69, 29);
btnNewButton_1_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Deletealumnus();
fillsalumnus(new Alumnus());
}
});
btnNewButton_1_2.setBackground(new Color(255, 255, 255));
btnNewButton_1_2.setFont(new Font("宋体", Font.PLAIN, 14));
desktopPane.add(btnNewButton_1_2);
JButton btnNewButton_1_1 = new JButton("添加校友");
btnNewButton_1_1.setBounds(755, 10, 115, 29);
btnNewButton_1_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tianjia();
fillsalumnus(new Alumnus());
}
});
btnNewButton_1_1.setBackground(new Color(255, 255, 255));
btnNewButton_1_1.setFont(new Font("宋体", Font.PLAIN, 14));
desktopPane.add(btnNewButton_1_1);
textField = new JTextField();
textField.setBounds(533, 143, 213, 29);
textField.setFont(new Font("宋体", Font.PLAIN, 14));
textField.setColumns(10);
desktopPane.add(textField);
JButton btnNewButton = new JButton("Q");
btnNewButton.setFont(new Font("微軟正黑體", Font.BOLD, 13));
btnNewButton.setBounds(769, 142, 46, 29);
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
chaxun();
}
});
desktopPane.add(btnNewButton);
JButton btnNewButton_2 = new JButton("退出");
btnNewButton_2.setBounds(533, 491, 69, 29);
btnNewButton_2.setBackground(new Color(255, 255, 255));
btnNewButton_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tuichu();
}
});
btnNewButton_2.setFont(new Font("宋体", Font.PLAIN, 14));
desktopPane.add(btnNewButton_2);
JButton btnNewButton_2_1 = new JButton("清除");
btnNewButton_2_1.setBounds(650, 491, 69, 29);
btnNewButton_2_1.setBackground(new Color(255, 255, 255));
btnNewButton_2_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
qingchu();
}
});
btnNewButton_2_1.setFont(new Font("宋体", Font.PLAIN, 14));
desktopPane.add(btnNewButton_2_1);
JLabel lblNewLabel_4 = new JLabel("成就");
lblNewLabel_4.setBounds(533, 209, 49, 27);
lblNewLabel_4.setFont(new Font("宋体", Font.PLAIN, 18));
desktopPane.add(lblNewLabel_4);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(2, 7, 474, 316);
desktopPane.add(scrollPane);
table = new JTable();
table.setBackground(new Color(255, 255, 255));
table.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
chengjiu();
}
});
table.setFont(new Font("宋体", Font.PLAIN, 12));
table.setModel(new DefaultTableModel(
new Object[][]{
},
new String[]{
"id", "姓名", "\u5E74\u9F84", "\u6027\u522B", "\u6240\u5728\u5730\u533A"
}
));
scrollPane.setViewportView(table);
JLabel lblNewLabel_4_1 = new JLabel("姓名");
lblNewLabel_4_1.setBounds(533, 115, 49, 27);
lblNewLabel_4_1.setFont(new Font("宋体", Font.PLAIN, 18));
desktopPane.add(lblNewLabel_4_1);
textField_1 = new JTextField();
textField_1.setBackground(new Color(255, 255, 255));
textField_1.setBounds(533, 234, 286, 227);
desktopPane.add(textField_1);
textField_1.setColumns(10);
JLabel lblNewLabel = new JLabel("");
lblNewLabel.setIcon(new ImageIcon("C:\\Users\\1\\Desktop\\实训2.collection\\hongdihaita1.png"));
lblNewLabel.setBounds(174, 318, 453, 302);
desktopPane.add(lblNewLabel);
JLabel lblNewLabel_1 = new JLabel("");
lblNewLabel_1.setHorizontalAlignment(SwingConstants.LEFT);
lblNewLabel_1.setIcon(new ImageIcon("C:\\Users\\1\\Desktop\\实训2.collection\\hongdi.png"));
lblNewLabel_1.setBounds(490, 108, 175, 80);
desktopPane.add(lblNewLabel_1);
JLabel lblNewLabel_1_1 = new JLabel("");
lblNewLabel_1_1.setIcon(new ImageIcon("C:\\Users\\1\\Desktop\\实训2.collection\\hongdi.png"));
lblNewLabel_1_1.setHorizontalAlignment(SwingConstants.LEFT);
lblNewLabel_1_1.setBounds(646, 107, 109, 84);
desktopPane.add(lblNewLabel_1_1);
JLabel lblNewLabel_2 = new JLabel("");
lblNewLabel_2.setHorizontalAlignment(SwingConstants.LEFT);
lblNewLabel_2.setIcon(new ImageIcon("C:\\Users\\1\\Desktop\\实训2.collection\\hongdifang.png"));
lblNewLabel_2.setBounds(492, 168, 164, 198);
desktopPane.add(lblNewLabel_2);
JLabel lblNewLabel_2_1 = new JLabel("");
lblNewLabel_2_1.setIcon(new ImageIcon("C:\\Users\\1\\Desktop\\实训2.collection\\hongdifang.png"));
lblNewLabel_2_1.setHorizontalAlignment(SwingConstants.LEFT);
lblNewLabel_2_1.setBounds(684, 166, 164, 198);
desktopPane.add(lblNewLabel_2_1);
JLabel lblNewLabel_2_2 = new JLabel("");
lblNewLabel_2_2.setIcon(new ImageIcon("C:\\Users\\1\\Desktop\\实训2.collection\\hongdifang.png"));
lblNewLabel_2_2.setHorizontalAlignment(SwingConstants.LEFT);
lblNewLabel_2_2.setBounds(500, 298, 164, 198);
desktopPane.add(lblNewLabel_2_2);
JLabel lblNewLabel_2_3 = new JLabel("");
lblNewLabel_2_3.setIcon(new ImageIcon("C:\\Users\\1\\Desktop\\实训2.collection\\hongdifang.png"));
lblNewLabel_2_3.setHorizontalAlignment(SwingConstants.LEFT);
lblNewLabel_2_3.setBounds(681, 301, 164, 198);
desktopPane.add(lblNewLabel_2_3);
JLabel lblNewLabel_1_2 = new JLabel("");
lblNewLabel_1_2.setIcon(new ImageIcon("C:\\Users\\1\\Desktop\\实训2.collection\\hongdi.png"));
lblNewLabel_1_2.setHorizontalAlignment(SwingConstants.LEFT);
lblNewLabel_1_2.setBounds(516, 52, 102, 53);
desktopPane.add(lblNewLabel_1_2);
JLabel lblNewLabel_1_2_1 = new JLabel("");
lblNewLabel_1_2_1.setIcon(new ImageIcon("C:\\Users\\1\\Desktop\\实训2.collection\\hongdi.png"));
lblNewLabel_1_2_1.setHorizontalAlignment(SwingConstants.LEFT);
lblNewLabel_1_2_1.setBounds(634, 52, 98, 55);
desktopPane.add(lblNewLabel_1_2_1);
JLabel lblNewLabel_1_2_1_1 = new JLabel("");
lblNewLabel_1_2_1_1.setIcon(new ImageIcon("C:\\Users\\1\\Desktop\\实训2.collection\\hongdi.png"));
lblNewLabel_1_2_1_1.setHorizontalAlignment(SwingConstants.LEFT);
lblNewLabel_1_2_1_1.setBounds(765, 134, 54, 48);
desktopPane.add(lblNewLabel_1_2_1_1);
JLabel lblNewLabel_1_3 = new JLabel("");
lblNewLabel_1_3.setIcon(new ImageIcon("C:\\Users\\1\\Desktop\\实训2.collection\\hongdi.png"));
lblNewLabel_1_3.setHorizontalAlignment(SwingConstants.LEFT);
lblNewLabel_1_3.setBounds(590, 190, 175, 80);
desktopPane.add(lblNewLabel_1_3);
JLabel lblNewLabel_1_3_1 = new JLabel("");
lblNewLabel_1_3_1.setIcon(new ImageIcon("C:\\Users\\1\\Desktop\\实训2.collection\\hongdifang.png"));
lblNewLabel_1_3_1.setHorizontalAlignment(SwingConstants.LEFT);
lblNewLabel_1_3_1.setBounds(581, 389, 175, 80);
desktopPane.add(lblNewLabel_1_3_1);
JLabel lblNewLabel_1_2_2 = new JLabel("");
lblNewLabel_1_2_2.setIcon(new ImageIcon("C:\\Users\\1\\Desktop\\实训2.collection\\hongdi.png"));
lblNewLabel_1_2_2.setHorizontalAlignment(SwingConstants.LEFT);
lblNewLabel_1_2_2.setBounds(520, 479, 102, 53);
desktopPane.add(lblNewLabel_1_2_2);
JLabel lblNewLabel_1_2_3 = new JLabel("");
lblNewLabel_1_2_3.setIcon(new ImageIcon("C:\\Users\\1\\Desktop\\实训2.collection\\hongdi.png"));
lblNewLabel_1_2_3.setHorizontalAlignment(SwingConstants.LEFT);
lblNewLabel_1_2_3.setBounds(642, 479, 93, 52);
desktopPane.add(lblNewLabel_1_2_3);
JLabel lblNewLabel_1_4 = new JLabel("");
lblNewLabel_1_4.setIcon(new ImageIcon("C:\\Users\\1\\Desktop\\实训2.collection\\hongdi.png"));
lblNewLabel_1_4.setHorizontalAlignment(SwingConstants.LEFT);
lblNewLabel_1_4.setBounds(177, 297, 175, 80);
desktopPane.add(lblNewLabel_1_4);
JLabel lblNewLabel_1_5 = new JLabel("");
lblNewLabel_1_5.setIcon(new ImageIcon("C:\\Users\\1\\Desktop\\实训2.collection\\hongdi.png"));
lblNewLabel_1_5.setHorizontalAlignment(SwingConstants.LEFT);
lblNewLabel_1_5.setBounds(340, 293, 175, 80);
desktopPane.add(lblNewLabel_1_5);
JLabel lblNewLabel_3 = new JLabel("欢迎");
lblNewLabel_3.setFont(new Font("华文行楷", Font.PLAIN, 40));
lblNewLabel_3.setBounds(6, 337, 132, 74);
desktopPane.add(lblNewLabel_3);
JLabel lblNewLabel_3_1 = new JLabel("进入");
lblNewLabel_3_1.setFont(new Font("华文行楷", Font.PLAIN, 40));
lblNewLabel_3_1.setBounds(82, 401, 132, 74);
desktopPane.add(lblNewLabel_3_1);
this.fillsalumnus(new Alumnus());
}
protected void qingchu() {
// TODO Auto-generated method stub
textField.setText("");
textField_1.setText("");
}
protected void tuichu() {
// TODO Auto-generated method stub
MainFrm MainFrm = new MainFrm();
MainFrm.setVisible(true);
this.setVisible(false);
}
//查询校友信息
protected void chaxun() {
// TODO Auto-generated method stub
String aname = this.textField.getText();
Alumnus alumnus = new Alumnus();
alumnus.setAname(aname);
//传入名字并调用查询方法
this.fillsalumnus(alumnus);
}
//删除校友
private void Deletealumnus() {
// TODO Auto-generated method stub
//选中需要删除的行
int hang = this.table.getSelectedRow();
if(hang==-1){
JOptionPane.showMessageDialog(null, "请选中需要修改的行!");
}
//获取数据
String issuer = (String) this.table.getValueAt(hang,1);
//判断数据
if (StringUtil.isEmpty(issuer)) {
JOptionPane.showMessageDialog(null, "没有选择需要删除的校友信息!");
return;
}
Alumnus alumnus = new Alumnus(issuer);
Connection con = null;
try {
//获得连接
con = dbUtil.getCon();
int currentAlumnus = alumnusDao.alumnusDelete(con, issuer);
if (currentAlumnus != 0) {
JOptionPane.showMessageDialog(null, "删除成功!");
} else {
JOptionPane.showMessageDialog(null, "删除失败!");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
JOptionPane.showMessageDialog(null, "删除失败!");
} finally {
try {
dbUtil.closeCon(con);
} catch (Exception e) {
e.printStackTrace();
}
}
}
//修改校友信息
protected void xiugai() {
// TODO Auto-generated method stub
//获取表格数据
int hang = this.table.getSelectedRow();
if(hang==-1){
JOptionPane.showMessageDialog(null, "请选中需要修改的行!");
}
int aid = (int)this.table.getValueAt(hang,0);
String aname = (String) this.table.getValueAt(hang,1);
int aage;
aage = (int)this.table.getValueAt(hang,2);
String agender = (String) this.table.getValueAt(hang,3);
String afrom = (String) this.table.getValueAt(hang,4);
String achieve = this.textField_1.getText();//获取文本框textField_1中的字符串
//判断数据
if ("".equals(String.valueOf(aid))) {
JOptionPane.showMessageDialog(null, "校友id不能为空!");
return;
}
if (StringUtil.isEmpty(aname)) {
JOptionPane.showMessageDialog(null, "校友姓名不能为空!");
return;
}
if ("".equals(String.valueOf(aage))) {
JOptionPane.showMessageDialog(null, "校友年龄不能为空!");
return;
}
if (StringUtil.isEmpty(agender)) {
JOptionPane.showMessageDialog(null, "校友性别不能为空!");
return;
}
if (StringUtil.isEmpty(afrom)) {
JOptionPane.showMessageDialog(null, "校友所在区域不能为空!");
return;
}
if (StringUtil.isEmpty(achieve)) {
JOptionPane.showMessageDialog(null, "校友成就不能为空!");
return;
}
Alumnus alumnus = new Alumnus(aid, aname, aage, agender, afrom, achieve);
Connection con = null;
try {
//获得连接
con = dbUtil.getCon();
int currentAlumnus = alumnusDao.alumnusModify(con, alumnus);
if (currentAlumnus != -1) {
this.fillsalumnus(alumnus);
JOptionPane.showMessageDialog(null, "修改成功!");
} else {
JOptionPane.showMessageDialog(null, "修改失败!");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
//JOptionPane.showMessageDialog(null, "注册失败");
} finally {
try {
dbUtil.closeCon(con);
} catch (Exception e) {
e.printStackTrace();
}
}
}
//添加校友信息
protected void tianjia() {
// TODO Auto-generated method stub
//跳转界面
AlumnusEditFrm AlumnusEditFrm = new AlumnusEditFrm();
AlumnusEditFrm.setVisible(true);
this.setVisible(false);
}
}
4、AlumnusEditFrm
package com.java.view;
import com.java.dao.AlumnusDao;
import com.java.model.Alumnus;
import com.java.util.DbUtil;
import com.java.util.StringUtil;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.Vector;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class AlumnusEditFrm extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
private JTextField textField_3;
private JTextField textField_4;
DbUtil dbUtil = new DbUtil();
AlumnusDao alumnusDao = new AlumnusDao();
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
AlumnusEditFrm frame = new AlumnusEditFrm();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public AlumnusEditFrm() {
setTitle("校友信息编辑界面");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 755, 505);
contentPane = new JPanel();
contentPane.setBackground(new Color(199, 46, 18));
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblNewLabel = new JLabel("姓名");
lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 18));
lblNewLabel.setBounds(10, 98, 49, 27);
contentPane.add(lblNewLabel);
JLabel lblNewLabel_1 = new JLabel("年龄");
lblNewLabel_1.setFont(new Font("宋体", Font.PLAIN, 18));
lblNewLabel_1.setBounds(269, 98, 49, 27);
contentPane.add(lblNewLabel_1);
JLabel lblNewLabel_2 = new JLabel("性别");
lblNewLabel_2.setFont(new Font("宋体", Font.PLAIN, 18));
lblNewLabel_2.setBounds(10, 163, 49, 27);
contentPane.add(lblNewLabel_2);
JLabel lblNewLabel_3 = new JLabel("所在地区");
lblNewLabel_3.setFont(new Font("宋体", Font.PLAIN, 18));
lblNewLabel_3.setBounds(269, 163, 82, 27);
contentPane.add(lblNewLabel_3);
JLabel lblNewLabel_4 = new JLabel("成就");
lblNewLabel_4.setFont(new Font("宋体", Font.PLAIN, 18));
lblNewLabel_4.setBounds(10, 230, 49, 27);
contentPane.add(lblNewLabel_4);
textField = new JTextField();
textField.setBounds(69, 99, 158, 27);
contentPane.add(textField);
textField.setColumns(10);
textField_1 = new JTextField();
textField_1.setColumns(10);
textField_1.setBounds(328, 98, 158, 27);
contentPane.add(textField_1);
textField_2 = new JTextField();
textField_2.setColumns(10);
textField_2.setBounds(69, 163, 158, 27);
contentPane.add(textField_2);
textField_3 = new JTextField();
textField_3.setColumns(10);
textField_3.setBounds(361, 163, 158, 27);
contentPane.add(textField_3);
textField_4 = new JTextField();
textField_4.setColumns(10);
textField_4.setBounds(69, 230, 282, 196);
contentPane.add(textField_4);
JButton btnNewButton = new JButton("添加");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tianjia();
}
});
btnNewButton.setFont(new Font("宋体", Font.PLAIN, 14));
btnNewButton.setBounds(456, 399, 76, 27);
contentPane.add(btnNewButton);
JButton btnNewButton_1 = new JButton("返回");
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
fanhui();
}
});
btnNewButton_1.setFont(new Font("宋体", Font.PLAIN, 14));
btnNewButton_1.setBounds(574, 399, 76, 27);
contentPane.add(btnNewButton_1);
JLabel lblNewLabel_3_1 = new JLabel("欢迎");
lblNewLabel_3_1.setFont(new Font("华文行楷", Font.PLAIN, 40));
lblNewLabel_3_1.setBounds(556, 68, 132, 74);
contentPane.add(lblNewLabel_3_1);
JLabel lblNewLabel_3_1_1 = new JLabel("进入");
lblNewLabel_3_1_1.setFont(new Font("华文行楷", Font.PLAIN, 40));
lblNewLabel_3_1_1.setBounds(609, 117, 132, 74);
contentPane.add(lblNewLabel_3_1_1);
JLabel lblNewLabel_5 = new JLabel("");
lblNewLabel_5.setIcon(new ImageIcon("C:\\Users\\1\\Desktop\\实训2.collection\\hongdihaita5.png"));
lblNewLabel_5.setBounds(430, 205, 391, 263);
contentPane.add(lblNewLabel_5);
JLabel lblNewLabel_6 = new JLabel("");
lblNewLabel_6.setIcon(new ImageIcon("C:\\Users\\1\\Desktop\\实训2.collection\\hongdifang.png"));
lblNewLabel_6.setBounds(1, 88, 58, 43);
contentPane.add(lblNewLabel_6);
JLabel lblNewLabel_6_1 = new JLabel("");
lblNewLabel_6_1.setIcon(new ImageIcon("C:\\Users\\1\\Desktop\\实训2.collection\\hongdifang.png"));
lblNewLabel_6_1.setBounds(1, 159, 58, 35);
contentPane.add(lblNewLabel_6_1);
JLabel lblNewLabel_6_2 = new JLabel("");
lblNewLabel_6_2.setIcon(new ImageIcon("C:\\Users\\1\\Desktop\\实训2.collection\\hongdifang.png"));
lblNewLabel_6_2.setBounds(1, 222, 58, 35);
contentPane.add(lblNewLabel_6_2);
JLabel lblNewLabel_6_3 = new JLabel("");
lblNewLabel_6_3.setIcon(new ImageIcon("C:\\Users\\1\\Desktop\\实训2.collection\\hongdifang.png"));
lblNewLabel_6_3.setBounds(260, 88, 58, 43);
contentPane.add(lblNewLabel_6_3);
JLabel lblNewLabel_7 = new JLabel("");
lblNewLabel_7.setIcon(new ImageIcon("C:\\Users\\1\\Desktop\\实训2.collection\\hongdifang.png"));
lblNewLabel_7.setBounds(260, 159, 91, 39);
contentPane.add(lblNewLabel_7);
JLabel lblNewLabel_8 = new JLabel("");
lblNewLabel_8.setIcon(new ImageIcon("C:\\Users\\1\\Desktop\\实训2.collection\\hongdifang.png"));
lblNewLabel_8.setBounds(546, 61, 158, 118);
contentPane.add(lblNewLabel_8);
}
protected void fanhui() {
// TODO Auto-generated method stub
AlumnusController AlumnusController = new AlumnusController();
AlumnusController.setVisible(true);
this.setVisible(false);
}
protected void tianjia() {
// TODO Auto-generated method stub
//获取表格数据
int aid = -1;
String aname = this.textField.getText();
int aage = Integer.parseInt(this.textField_1.getText());
String agender = this.textField_2.getText();
String afrom = this.textField_3.getText();
String achieve = this.textField_4.getText();//获取文本框textField_1中的字符串
//判断数据
if ("".equals(String.valueOf(aid))) {
JOptionPane.showMessageDialog(null, "校友id不能为空!");
return;
}
if (StringUtil.isEmpty(aname)) {
JOptionPane.showMessageDialog(null, "校友姓名不能为空!");
return;
}
if ("".equals(String.valueOf(aage))) {
JOptionPane.showMessageDialog(null, "校友年龄不能为空!");
return;
}
if (StringUtil.isEmpty(agender)) {
JOptionPane.showMessageDialog(null, "校友性别不能为空!");
return;
}
if (StringUtil.isEmpty(afrom)) {
JOptionPane.showMessageDialog(null, "校友所在区域不能为空!");
return;
}
if (StringUtil.isEmpty(achieve)) {
JOptionPane.showMessageDialog(null, "校友成就不能为空!");
return;
}
Alumnus alumnus = new Alumnus(aid, aname, aage, agender, afrom, achieve);
Connection con = null;
try {
//获得连接
con = dbUtil.getCon();
int currentAlumnus = alumnusDao.alumnusAdd(con, alumnus);
if (currentAlumnus != -1) {
JOptionPane.showMessageDialog(null, "添加成功!");
this.setVisible(false);
AlumnusController AlumnusController = new AlumnusController();
AlumnusController.setVisible(true);
} else {
JOptionPane.showMessageDialog(null, "添加失败!");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
//JOptionPane.showMessageDialog(null, "注册失败");
} finally {
try {
dbUtil.closeCon(con);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
三、所用博客分享
1、mysql下载安装:这边建议下载最简单的mysql文件夹
csdn上面搜索下载安装建议就可以直接搜到
2、eclipse的插件window builder下载建议:重新下载最新版的eclipse再进行下载安装
3、idea自带的数据库连接是真的很舒服
四、总结
怎么说捏,实训下来,总的来说,就是没有什么太大的用处,只是给我熬夜多了一个理由。可以借此多多认识身边的大佬吧,看看代码,代课老师应该有对应的代码分享,什么图书管理系统啊,学生信息管理系统啊,代码都有相似的地方,如果想做得出彩,可以多多设计一下界面,毕竟这个windobuilder自带的皮肤包是真的不怎么好看,可以多多搜索看看怎么设置前端,这个小小的项目还有很多可以改善的地方,代码仅供参考,具体设计还是有一个美工很厉害的大佬带着比较好哈。