//我根据图片的信息来写文件内容吧!
这是dao方法
package com.zking.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import com.zking.entity.Student;
import com.zking.util.DBHelper;
public class StudentDao {
/**
* 模糊查询
* @param col 字段名
* @param str 关键字
* @return 返回查询到的集合
*/
public ArrayList<Student> getByLike(String col,String str){
ArrayList<Student> slist = new ArrayList<>();
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
con = DBHelper.getCon();
ps = con.prepareStatement("select * from Student where "+col+" like '%"+str+"%'");
rs = ps.executeQuery();
//处理结果:集合/影响行数
while(rs.next()) {
int sid = rs.getInt(1);
String sname = rs.getString(2);
String ssex = rs.getString(3);
int sage = rs.getInt(4);
double score = rs.getDouble(5);
String saddress = rs.getString(6);
String slike = rs.getString(7);
//实例化学生对象
Student stu = new Student(sid, sname, ssex, sage, score, saddress, slike);
//把学生对象放到集合中
slist.add(stu);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHelper.closeDB(con, ps, rs);
}
return slist;
}
public Student getById(int sid ){
Student stu = null;
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
con = DBHelper.getCon();
ps = con.prepareStatement("select * from Student where sid="+sid);
rs = ps.executeQuery();
//处理结果:集合/影响行数
if(rs.next()) {
String sname = rs.getString(2);
String ssex = rs.getString(3);
int sage = rs.getInt(4);
double score = rs.getDouble(5);
String saddress = rs.getString(6);
String slike = rs.getString(7);
String sgxqm = rs.getString(8);
//实例化学生对象
stu = new Student(sid, sname, ssex, sage, score, saddress, slike,sgxqm);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHelper.closeDB(con, ps, rs);
}
return stu;
}
/**
* 添加学生
* @param stu 添加的学生对象
* @return 成功返回1,失败返回0
*/
public int addStu(Student stu){
Connection con = null;
PreparedStatement ps = null;
int i = 0;
try {
con = DBHelper.getCon();
ps = con.prepareStatement("insert into Student(sname,ssex,slike,saddress,sgxqm) values(?,?,?,?,?)");
ps.setString(1, stu.getSname());
ps.setString(2, stu.getSsex());
ps.setString(3, stu.getSlike());
ps.setString(4, stu.getSaddress());
ps.setString(5, stu.getSgxqm());
//处理结果:集合/影响行数
i = ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHelper.closeDB(con, ps, null);
}
return i;
}
/**
* 修改方法
* @param sid 要修改的学生学号
* @param stu 修改后的学生信息
* @return 成功返回1,失败返回0
*/
public int upStu(int sid,Student stu){
Connection con = null;
PreparedStatement ps = null;
int i = 0;
try {
con = DBHelper.getCon();
ps = con.prepareStatement("update Student set sname=?,ssex=?,slike=?,saddress=?,sgxqm=? where sid="+sid);
ps.setString(1, stu.getSname());
ps.setString(2, stu.getSsex());
ps.setString(3, stu.getSlike());
ps.setString(4, stu.getSaddress());
ps.setString(5, stu.getSgxqm());
//处理结果:集合/影响行数
i = ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHelper.closeDB(con, ps, null);
}
return i;
}
/**
* 删除学生
* @param sid 要删除的学生学号
* @return 成功就返回1
*/
public int delById(int sid){
Connection con = null;
PreparedStatement ps = null;
int i = 0;
try {
con = DBHelper.getCon();
ps = con.prepareStatement("delete student where sid="+sid);
//处理结果:集合/影响行数
i = ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHelper.closeDB(con, ps, null);
}
return i;
}
}
package com.zking.entity;
public class Student {
private int sid;
private String sname;
private String ssex;
private int sage;
private double score;
private String saddress;
private String slike;
private String sgxqm;
public Student(int sid, String sname, String ssex, int sage, double score, String saddress, String slike,
String sgxqm) {
super();
this.sid = sid;
this.sname = sname;
this.ssex = ssex;
this.sage = sage;
this.score = score;
this.saddress = saddress;
this.slike = slike;
this.sgxqm = sgxqm;
}
public Student(String sname, String ssex, String saddress, String slike, String sgxqm) {
super();
this.sname = sname;
this.ssex = ssex;
this.saddress = saddress;
this.slike = slike;
this.sgxqm = sgxqm;
}
public String getSgxqm() {
return sgxqm;
}
public void setSgxqm(String sgxqm) {
this.sgxqm = sgxqm;
}
public Student() {
}
public Student(String sname, String ssex, int sage, double score, String saddress, String slike) {
this.sname = sname;
this.ssex = ssex;
this.sage = sage;
this.score = score;
this.saddress = saddress;
this.slike = slike;
}
public Student(int sid, String sname, String ssex, int sage, double score, String saddress, String slike) {
this.sid = sid;
this.sname = sname;
this.ssex = ssex;
this.sage = sage;
this.score = score;
this.saddress = saddress;
this.slike = slike;
}
public int getSid() {
return sid;
}
public void setSid(int sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getSsex() {
return ssex;
}
public void setSsex(String ssex) {
this.ssex = ssex;
}
public int getSage() {
return sage;
}
public void setSage(int sage) {
this.sage = sage;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
public String getSaddress() {
return saddress;
}
public void setSaddress(String saddress) {
this.saddress = saddress;
}
public String getSlike() {
return slike;
}
public void setSlike(String slike) {
this.slike = slike;
}
@Override
public String toString() {
return "Student [sid=" + sid + ", sname=" + sname + ", ssex=" + ssex + ", sage=" + sage + ", score=" + score
+ ", saddress=" + saddress + ", slike=" + slike + "]";
}
}
package com.zking.ui;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import com.zking.dao.StudentDao;
import com.zking.entity.Student;
public class Index_UI extends JFrame {
//上
JPanel topjp = new JPanel();
//下拉框
JComboBox<String> jcb = new JComboBox<>();
//文本框
JTextField jtf = new JTextField(10);
//按钮
JButton jbcx = new JButton("查询");
//中
JTable jt = new JTable();
//表模式
DefaultTableModel dtm = new DefaultTableModel();
//滚动面板-给表格添加滚动面板
JScrollPane jsp = new JScrollPane(jt);
//下
JPanel bottomjp = new JPanel();
JButton jbadd = new JButton("添加");
JButton jbdel = new JButton("删除");
JButton jbup = new JButton("修改");
JButton jbxq = new JButton("详情");
JButton jbexit = new JButton("退出");
public Index_UI() {
this.setTitle("主窗体");
this.setSize(500, 500);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
//上面的画布
topjp.add(jcb);
jcb.addItem("姓名");
jcb.addItem("性别");
jcb.addItem("地址");
topjp.add(jtf);
topjp.add(jbcx);
//中间
//设置表模式
jt.setModel(dtm);
//给表格添加标题
dtm.addColumn("学号");
dtm.addColumn("姓名");
dtm.addColumn("性别");
dtm.addColumn("年龄");
dtm.addColumn("地址");
//调用showInfo方法 给表格绑定数据
showInfo("sname", "");
//下面的画布
bottomjp.add(jbadd);
bottomjp.add(jbdel);
bottomjp.add(jbup);
bottomjp.add(jbxq);
bottomjp.add(jbexit);
//给查询按钮添加事件-匿名内部类
jbcx.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
//获取下拉框中选中的选项
String col = jcb.getSelectedItem()+"";
if(col.equals("姓名")) {
col = "sname";
}else if(col.equals("性别")) {
col = "ssex";
}else {
col = "saddress";
}
//获取关键字
String str = jtf.getText();
System.out.println("字段:"+col);
System.out.println("关键字:"+str);
//调用showInfo方法给表格绑定数据
showInfo(col, str);
}
});
//给添加按钮添加事件
jbadd.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
//打开添加窗体-同时把本窗体传过去
new AddStu_UI(Index_UI.this);
}
});
//给删除按钮添加事件
jbdel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
// 获取选中的行下标-未选中返回-1
int row = jt.getSelectedRow();
if (row >= 0) {
// 获取选中行中的学生学号(第一列)
String id = dtm.getValueAt(row, 0) + "";
// 把字符串编号 转换为int
int sid = Integer.valueOf(id);
if (JOptionPane.showConfirmDialog(null, "确认要删除吗?") == 0) {
// 调用删除方法
int i = new StudentDao().delById(sid);
if (i == 1) {
JOptionPane.showMessageDialog(null, "删除成功");
// 刷新本窗体数据
showInfo("saddress", "");
} else {
JOptionPane.showMessageDialog(null, "删除失败");
}
} else {
JOptionPane.showMessageDialog(null, "取消删除");
}
}else {
JOptionPane.showMessageDialog(null, "请选中要删除的学生");
}
}
});
//给修改按钮添加事件
jbup.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
//获取要修改的学生学号
//获取选中的行
int row = jt.getSelectedRow();
//获取学号
String id = dtm.getValueAt(row, 0)+"";
int sid = Integer.valueOf(id);
//根据学号查询到学生对象
Student stu = new StudentDao().getById(sid);
System.out.println(stu);
//打开修改窗体
new UpStu_UI(stu,Index_UI.this);
}
});
//上边的画布
this.getContentPane().add("North",topjp);
this.getContentPane().add("Center",jsp);
this.getContentPane().add("South",bottomjp);
this.setVisible(true);
}
/**
* 给表格绑定数据
* @param col 字段名
* @param str 关键字
*/
public void showInfo(String col, String str) {
//清空表格中的老数据
int count = jt.getRowCount();//获取到表格中的所有数据的总行数
System.out.println("数据行:"+count);
for(int i=0;i<count;i++) {
dtm.removeRow(0);
}
// 实例化学生dao类
StudentDao sd = new StudentDao();
ArrayList<Student> slist = sd.getByLike(col, str);
// 遍历集合
for (Student s : slist) {
// 创建一个集合,用来接收要往表格中添加的数据
Vector<Object> v = new Vector<>();
v.add(s.getSid());
v.add(s.getSname());
v.add(s.getSsex());
v.add(s.getSage());
v.add(s.getSaddress());
// 行:row
dtm.addRow(v);
}
}
package com.zking.ui;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import com.zking.dao.StudentDao;
import com.zking.entity.Student;
/**
* 添加学生
* @author d
*
*/
public class UpStu_UI extends JFrame {
public UpStu_UI(Student stu,Index_UI index) {
this.setTitle("修改学生");
this.setSize(350, 500);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//画布
JPanel jp = new JPanel();
//布局对象-网袋布局
GridBagLayout gbl = new GridBagLayout();
//约束对象
GridBagConstraints gbc = new GridBagConstraints();
//设置画布布局
jp.setLayout(gbl);
//用户名文本
JLabel jlname = new JLabel("姓名:");
//约束坐标位置
gbc.gridx = 0;
gbc.gridy = 0;
//设置组件
gbl.setConstraints(jlname, gbc);
//把组件添加到画布上
jp.add(jlname);
//用户文本框
JTextField jtname = new JTextField(10);
gbc.gridx = 1;
gbc.gridy = 0;
//设置组件
gbl.setConstraints(jtname, gbc);
//把组件添加到画布上
jp.add(jtname);
//性别文本
JLabel jlsex = new JLabel("性别:");
//约束位置
gbc.gridx = 0;
gbc.gridy = 1;
//设置组件位置
gbl.setConstraints(jlsex, gbc);
//添加到画布
jp.add(jlsex);
//单选按钮
JRadioButton jrboy = new JRadioButton("女");
gbc.insets = new Insets(0, -80, 0, 0);
gbc.gridx = 1;
gbc.gridy = 1;
gbl.setConstraints(jrboy, gbc);
jp.add(jrboy);
JRadioButton jrgirl = new JRadioButton("男");
//上左下右
gbc.insets = new Insets(0, -180, 0, 0);
gbc.gridx = 2;
gbc.gridy = 1;
gbl.setConstraints(jrgirl, gbc);
jp.add(jrgirl);
//单选按钮分组
ButtonGroup bg = new ButtonGroup();
bg.add(jrboy);
bg.add(jrgirl);
gbc.insets = new Insets(0, 0, 0, 0);
//爱好文本-第四行、第一列
JLabel jllike = new JLabel("爱好:");
gbc.gridx = 0;
gbc.gridy = 2;
gbl.setConstraints(jllike, gbc);
jp.add(jllike);
//爱好多选框
JCheckBox jcba = new JCheckBox("草莓袜子");
gbc.insets = new Insets(0, -20, 0, 0);
gbc.gridx = 1;
gbc.gridy = 2;
gbl.setConstraints(jcba, gbc);
jp.add(jcba);
JCheckBox jcbb = new JCheckBox("幽兰拿铁");
gbc.gridx = 2;
gbc.gridy = 2;
gbl.setConstraints(jcbb, gbc);
jp.add(jcbb);
JCheckBox jcbc = new JCheckBox("蓝莓肚兜");
gbc.gridx = 1;
gbc.gridy = 3;
gbl.setConstraints(jcbc, gbc);
jp.add(jcbc);
JCheckBox jcbd = new JCheckBox("小熊裤衩");
gbc.gridx = 2;
gbc.gridy = 3;
gbl.setConstraints(jcbd, gbc);
jp.add(jcbd);
//地址文本-第六行,第一列
JLabel jladdress = new JLabel("地址:");
gbc.insets = new Insets(0, 0, 0, 0);
gbc.gridx = 0;
gbc.gridy = 4;
gbl.setConstraints(jladdress, gbc);
jp.add(jladdress);
//下拉框-第六行 第二列
JComboBox<String> jc = new JComboBox<>();
jc.addItem("长沙");
jc.addItem("衡阳");
jc.addItem("郴州");
jc.addItem("娄底");
jc.addItem("岳阳");
jc.addItem("永州");
jc.addItem("阜南");
jc.addItem("张家界");
jc.addItem("临湘");
jc.addItem("平江");
jc.addItem("等等");
gbc.gridx = 1;
gbc.gridy = 4;
gbl.setConstraints(jc, gbc);
jp.add(jc);
//个人说明文本
JLabel jlgrsm = new JLabel("个人说明:");
gbc.gridx = 0;
gbc.gridy = 5;
gbl.setConstraints(jlgrsm, gbc);
jp.add(jlgrsm);
//文本域
JTextArea jta = new JTextArea(5, 10);
//滚动面板
JScrollPane jsp = new JScrollPane(jta);
gbc.insets = new Insets(10, 0, 10, 0);
gbc.gridx = 1;
gbc.gridy = 5;
gbl.setConstraints(jsp, gbc);
jp.add(jsp);
//按钮
JButton jbzc = new JButton("修改");
gbc.gridx = 0;
gbc.gridy = 6;
gbl.setConstraints(jbzc, gbc);
jp.add(jbzc);
JButton jbqx = new JButton("取消");
gbc.gridx = 1;
gbc.gridy = 6;
gbl.setConstraints(jbqx, gbc);
jp.add(jbqx);
//给组件赋值
jtname.setText(stu.getSname());
//性别
//默认选中女
jrboy.setSelected(true);
if(stu.getSsex().equals("男")) {
//如果性别为男,就设置男的单选按钮被选中
jrgirl.setSelected(true);
}
//爱好
String slike = stu.getSlike();
if(slike.contains("草莓袜子")) {
jcba.setSelected(true);//设置选中
}
if(slike.contains("幽兰拿铁")) {
jcbb.setSelected(true);
}
if(slike.contains("蓝莓肚兜")) {
jcbc.setSelected(true);
}
if(slike.contains("小熊裤衩")) {
jcbd.setSelected(true);
}
//地址
jc.setSelectedItem(stu.getSaddress());
//个性签名
jta.setText(stu.getSgxqm());
//给修改按钮添加事件
jbzc.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
// 获取组件的值
String sname = jtname.getText();
String ssex = "男";
if (jrboy.isSelected()) {
ssex = "女";
}
String slike = "";
if (jcba.isSelected()) {
slike += "草莓袜子";
}
if (jcbb.isSelected()) {
slike += "幽兰拿铁";
}
if (jcbc.isSelected()) {
slike += "蓝莓肚兜";
}
if (jcbd.isSelected()) {
slike += "小熊裤衩";
}
String saddress = jc.getSelectedItem() + "";
String sgxqm = jta.getText();
// 实例化学生对象
Student s = new Student(sname, ssex, saddress, slike, sgxqm);
// 调用修改方法
int i = new StudentDao().upStu(stu.getSid(), s);
if (i == 1) {
JOptionPane.showMessageDialog(null, "修改成功");
// 刷新数据
index.showInfo("ssex", "");
// 释放本窗体资源
UpStu_UI.this.dispose();
} else {
JOptionPane.showMessageDialog(null, "修改失败");
}
}
});
//给取消按钮添加事件
jbqx.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
//释放本窗体资源
UpStu_UI.this.dispose();
}
});
this.getContentPane().add(jp);
this.setVisible(true);
}
}
接下来就是util帮助类啦!
package com.zking.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class DBHelper {
private static String cname = "com.microsoft.sqlserver.jdbc.SQLServerDriver";//注册驱动类的类的路径
private static String url = "jdbc:sqlserver://localhost:1433;DatabaseName=T277";
private static String uname = "sa";
private static String upwd = "8023";
static {
try {
Class.forName(cname);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 连接数据库
* @return
*/
public static Connection getCon() {
Connection con = null;
try {
con = DriverManager.getConnection(url, uname, upwd);
} catch (Exception e) {
e.printStackTrace();
}
return con;
}
/**
* 关闭连接
* @param con Connection对象
* @param ps PreparedStatement对象
* @param rs ResultSet 对象
*/
public static void closeDB(Connection con,PreparedStatement ps,ResultSet rs) {
try {
if(con!=null) {
con.close();
}
if(ps!=null) {
ps.close();
}
if(rs!=null) {
rs.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}