id | name | sex | age | tel |
---|---|---|---|---|
学号 | 姓名 | 性别 | 年龄 | 电话 |
link.java:连接数据库,并实现增删改查功能
Student.java:获取数据库数据
test.java:主函数
Windows_xg.java:修改窗口设计
Windows.java:主窗口设计
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
public class link {
private final static String url = "jdbc:mysql://localhost:3306/student?serverTimezone=GMT"; //设置连接路径
private final static String username = "root"; //数据库用户名
private final static String password = "1234"; //数据库连接密码
private Connection conn;
public Connection getCon() {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
System.out.println("驱动加载成功");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
conn = DriverManager.getConnection(url, username, password);
System.out.println("连接成功");
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public List<Student> list(){
//查询语句的执行结果为一个表格,即多个学生对象的集合,所有可以创建数组列表对象来存储
List<Student> lst = new ArrayList<Student>();
//获取连接对象
conn = getCon();
try {
//连接对象conn调用createStatement()方法,创建一个执行SQL语句的对象st
Statement st = conn.createStatement();
//执行SQL语句的对象st调用executeQuery()方法,执行查询语句,将查询到的结果返回到一个结果集中
ResultSet rs = st.executeQuery("select*from student_ei");
//遍历结果集对象
while(rs.next()){
Student student = new Student();
student.setId(rs.getString("id"));
student.setName(rs.getString("name"));
student.setSex(rs.getString("sex"));
student.setAge(rs.getString("age"));
student.setTel(rs.getString("tel"));
lst.add(student);
}
}catch (Exception e){
System.out.println(e.getMessage());
}
return lst;
}
public List<Student> query(int jxf_id) throws SQLException {
//获取连接对象
conn = getCon();
Statement state=conn.createStatement();//容器
String sql="select * from student_ei where id= '"+jxf_id+"'"; //sql语句
ResultSet rs=state.executeQuery(sql); //将sql语句传至数据库,返回的值为一个字符集用一个变量接收
List<Student> lst_q = new ArrayList<Student>();
while(rs.next()){ //next()获取里面的内容
//System.out.println(rs.getString(1)+" "+rs.getString(2)+" "+rs.getString(3)+" "+rs.getString(4)+" "+rs.getString(5));
//getString(n)获取第n列的内容
//数据库中的列数是从1开始的
Student student_q = new Student();
student_q.setId(rs.getString("id"));
student_q.setName(rs.getString("name"));
student_q.setSex(rs.getString("sex"));
student_q.setAge(rs.getString("age"));
student_q.setTel(rs.getString("tel"));
lst_q.add(student_q);
}
conn.close();
return lst_q;
}
public void increase( String jxf_id_t, String jxf_name_t,String jxf_sex_t,String jxf_age_t,String jxf_tel_t) throws SQLException {
conn = getCon();
Statement state=conn.createStatement();//容器
int jxf_id_t_i=Integer.parseInt(jxf_id_t);
int jxf_age_t_i=Integer.parseInt(jxf_age_t);
String sql="insert into student_ei values ('"+jxf_id_t_i+"','"+jxf_name_t+"','"+jxf_sex_t+"','"+jxf_age_t_i+"','"+jxf_tel_t+"')";//sql语句
state.executeUpdate(sql); //将sql语句传至数据库,返回的值为一个字符集用一个变量接收
System.out.print("添加完毕");
conn.close();
}
public void modify(String s_jcbx,String s_id,String s_nr) throws SQLException{
conn = getCon();
Statement state=conn.createStatement();//容器
if(s_jcbx == "学号") {
int s_id_i=Integer.parseInt(s_id);
int s_nr_i=Integer.parseInt(s_nr);
String sql="update student_ei set id = '"+s_nr_i+"' where id = '"+s_id_i+"'";
state.executeUpdate(sql); //将sql语句传至数据库,返回的值为一个字符集用一个变量接收
System.out.print("添加完毕");
conn.close();
}
else if(s_jcbx == "姓名") {
String sql="update student_ei set name = '"+s_nr+"' where id = '"+s_id+"'";
state.executeUpdate(sql); //将sql语句传至数据库,返回的值为一个字符集用一个变量接收
System.out.print("添加完毕");
conn.close();
}
else if(s_jcbx == "性别") {
String sql="update student_ei set sex = '"+s_nr+"' where id = '"+s_id+"'";
state.executeUpdate(sql); //将sql语句传至数据库,返回的值为一个字符集用一个变量接收
System.out.print("添加完毕");
conn.close();
}
else if(s_jcbx == "年龄") {
int s_id_i=Integer.parseInt(s_id);
int s_nr_i=Integer.parseInt(s_nr);
String sql="update student_ei set age = '"+s_nr_i+"' where id = '"+s_id_i+"'";
state.executeUpdate(sql); //将sql语句传至数据库,返回的值为一个字符集用一个变量接收
System.out.print("添加完毕");
conn.close();
}
else if(s_jcbx == "电话"){
String sql="update student_ei set tel = '"+s_nr+"' where id = '"+s_id+"'";//sql语句
state.executeUpdate(sql); //将sql语句传至数据库,返回的值为一个字符集用一个变量接收
System.out.print("添加完毕");
conn.close();
}
}
}
public class Student {
private String id;
private String name;
private String sex;
private String age;
private String tel;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
}
public class test {
public static void main(String arg[]){
new Windows();
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Windows_xg implements ActionListener{
JFrame jfrm_xg = new JFrame();
JPanel jp_xg = new JPanel();
JPanel jp_xg_id = new JPanel();
JPanel jp_xg_jcb = new JPanel();
JPanel jp_xg_nr = new JPanel();
JPanel jp_xg_jbt = new JPanel();
JLabel lab_xg_id = new JLabel("学生学号:",JLabel.CENTER);
JLabel lab_xg_jcb = new JLabel("修改项目:",JLabel.CENTER);
JLabel lab_xg_nr = new JLabel("修改内容:",JLabel.CENTER);
JTextField jxf_xg_id = new JTextField(10);//文本框长度为10
JTextField jxf_xg_nr = new JTextField(10);//文本框长度为10
JButton jbtn_xg_xg = new JButton("修改");//查找添加
String[] listData = new String[]{"学号", "姓名", "性别", "年龄", "电话"," "};
final JComboBox<String> jcbx = new JComboBox<String>(listData);
public Windows_xg(){
jfrm_xg.setVisible(true);//窗体可见
jfrm_xg.setTitle("学生信息管理系统_修改");//窗口标题
jfrm_xg.setSize(240,220);//设置了一个长为800,高为600的框图。
jfrm_xg.setLocation(900,400);//设置一个左上角顶点在(240,135)的窗体。
jfrm_xg.add(jp_xg);
jp_xg.add(jp_xg_id);
jp_xg.add(jp_xg_jcb);
jp_xg.add(jp_xg_nr);
jp_xg.add(jp_xg_jbt);
jp_xg_id.add(lab_xg_id);
jp_xg_jcb.add(lab_xg_jcb);
jp_xg_nr.add(lab_xg_nr);
jp_xg_id.add(jxf_xg_id);
jp_xg_nr.add(jxf_xg_nr);
jp_xg_jbt.add(jbtn_xg_xg);
jp_xg_jcb.add(jcbx);
jbtn_xg_xg.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==jbtn_xg_xg)
{
String s_jcbx=(String) jcbx.getSelectedItem();
System.out.print(s_jcbx + "\n");
String s_id = jxf_xg_id.getText();
String s_nr = jxf_xg_nr.getText();
link link = new link();
try {
link.modify(s_jcbx,s_id,s_nr);
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
public class Windows implements ActionListener{
JFrame jfrm = new JFrame();
JPanel jp_total = new JPanel(new BorderLayout());//边界布局
JPanel jp_1 = new JPanel();
JScrollPane jp_c =new JScrollPane();
JTextField jxf = new JTextField(10);//文本框长度为10
JButton jbtn_cz = new JButton("查找");//查找按钮
JButton jbtn_tj = new JButton("添加");//查找添加
JButton jbtn_xg = new JButton("修改");//查找修改
JButton jbtn_sx = new JButton("刷新");//查找修改
//表格
JTable jtb=new JTable();
DefaultTableModel tableModel = (DefaultTableModel) jtb.getModel();
public Windows(){
jfrm.setVisible(true);//窗体可见
jfrm.setTitle("学生信息管理系统");//窗口标题
jfrm.setSize(800,600);//设置了一个长为800,高为600的框图。
//jfrm.setBounds(1,2,20,10);//设置一个左上角顶点在(1,2),长为20,宽为10的窗体。
jfrm.setLocation(240,135);//设置一个左上角顶点在(240,135)的窗体。
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭窗口的时候退出程序
jfrm.add(jp_total);
jp_total.add(jp_1,BorderLayout.NORTH);
jp_1.add(jxf);
jbtn_cz.addActionListener(this);
jbtn_tj.addActionListener(this);
jbtn_xg.addActionListener(this);
jbtn_sx.addActionListener(this);
jp_1.add(jbtn_cz);
jp_1.add(jbtn_tj);
jp_1.add(jbtn_xg);
jp_1.add(jbtn_sx);
jp_c.setViewportView(jtb);
jp_total.add(jp_c,BorderLayout.CENTER);
//表格
tableModel.addColumn("学号");
tableModel.addColumn("姓名");
tableModel.addColumn("性别");
tableModel.addColumn("年龄");
tableModel.addColumn("电话");
//表格
link link = new link();
List<Student> lst = link.list();
for (int i = 0; i < lst.size(); i++) {
Student student = (Student) lst.get(i);
tableModel.addRow(new Object[] { student.getId(), student.getName(), student.getSex(),
student.getAge(), student.getTel()});}
jp_c.setViewportView(jtb);
//居中显示
DefaultTableCellRenderer dc=new DefaultTableCellRenderer();
dc.setHorizontalAlignment(JLabel.CENTER);
jtb.setDefaultRenderer(Object.class, dc);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
Object eventSource = e.getSource();
if(eventSource == jbtn_cz)//查找
{
System.out.print("查找");
JFrame jfrm_cz = new JFrame();
jfrm_cz.setVisible(true);//窗体可见
jfrm_cz.setTitle("学生信息管理系统_查找");//窗口标题
jfrm_cz.setSize(800,600);//设置了一个长为800,高为600的框图。
jfrm_cz.setLocation(260,155);//设置一个左上角顶点在(240,135)的窗体。
String jxf_id = jxf.getText();
int jxf_id_i=Integer.parseInt(jxf_id);
//query(jxf_id_i);
JScrollPane jp_c_q =new JScrollPane();
jfrm_cz.add(jp_c_q);
JTable jtb_q=new JTable();
DefaultTableModel tableModel_q = (DefaultTableModel) jtb_q.getModel();
//表格
tableModel_q.addColumn("学号");
tableModel_q.addColumn("姓名");
tableModel_q.addColumn("性别");
tableModel_q.addColumn("年龄");
tableModel_q.addColumn("电话");
link link = new link();
List<Student> lst_q = null;
try {
lst_q = link.query(jxf_id_i);
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
for (int i = 0; i < lst_q.size(); i++) {
Student student = (Student) lst_q.get(i);
tableModel_q.addRow(new Object[] { student.getId(), student.getName(), student.getSex(),
student.getAge(), student.getTel()});}
jp_c_q.setViewportView(jtb_q);
//居中显示
DefaultTableCellRenderer dc=new DefaultTableCellRenderer();
dc.setHorizontalAlignment(JLabel.CENTER);
jtb_q.setDefaultRenderer(Object.class, dc);
}
else if(eventSource == jbtn_tj)//添加
{
System.out.print("添加\n");
JFrame jfrm_tj = new JFrame();
jfrm_tj.setVisible(true);//窗体可见
jfrm_tj.setTitle("学生信息管理系统_添加");//窗口标题
jfrm_tj.setSize(240,280);//设置了一个长为800,高为600的框图。
jfrm_tj.setLocation(900,400);//设置一个左上角顶点在(240,135)的窗体。
JPanel jp_tj = new JPanel();
JPanel jp_tj_id = new JPanel();
JPanel jp_tj_name = new JPanel();
JPanel jp_tj_sex = new JPanel();
JPanel jp_tj_age = new JPanel();
JPanel jp_tj_tel = new JPanel();
JPanel jp_tj_tj = new JPanel();
jfrm_tj.add(jp_tj);
jp_tj.add(jp_tj_id);
jp_tj.add(jp_tj_name);
jp_tj.add(jp_tj_sex);
jp_tj.add(jp_tj_age);
jp_tj.add(jp_tj_tel);
jp_tj.add(jp_tj_tj);
JLabel lab_id = new JLabel("学号:",JLabel.CENTER);
JLabel lab_name = new JLabel("姓名:",JLabel.CENTER);
JLabel lab_sex = new JLabel("性别:",JLabel.CENTER);
JLabel lab_age = new JLabel("年龄:",JLabel.CENTER);
JLabel lab_tel = new JLabel("电话:",JLabel.CENTER);
jp_tj_id.add(lab_id);
jp_tj_name.add(lab_name);
jp_tj_sex.add(lab_sex);
jp_tj_age.add(lab_age);
jp_tj_tel.add(lab_tel);
JTextField jxf_id = new JTextField(10);//文本框长度为10
JTextField jxf_name = new JTextField(10);//文本框长度为10
JTextField jxf_sex = new JTextField(10);//文本框长度为10
JTextField jxf_age = new JTextField(10);//文本框长度为10
JTextField jxf_tel = new JTextField(10);//文本框长度为10
jp_tj_id.add(jxf_id);
jp_tj_name.add(jxf_name);
jp_tj_sex.add(jxf_sex);
jp_tj_age.add(jxf_age);
jp_tj_tel.add(jxf_tel);
JButton jbtn_tj_tj = new JButton("添加");//查找添加
jp_tj_tj.add(jbtn_tj_tj);
jbtn_tj_tj.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==jbtn_tj_tj) {
System.out.print("添加完毕123");
String jxf_id_t = jxf_id.getText();
String jxf_name_t = jxf_name.getText();
String jxf_sex_t = jxf_sex.getText();
String jxf_age_t = jxf_age.getText();
String jxf_tel_t = jxf_tel.getText();
link link = new link();
try {
link.increase(jxf_id_t, jxf_name_t, jxf_sex_t, jxf_age_t, jxf_tel_t);
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}});
}
else if(eventSource == jbtn_xg)//修改
{
System.out.print("修改\n");
new Windows_xg();
}
else if(eventSource == jbtn_sx)//修改
{
System.out.print("刷新\n");
int rows = jtb.getRowCount();
DefaultTableModel tableModel = (DefaultTableModel) jtb.getModel();
for(int i=0;i < rows;i++) {
tableModel.removeRow(0);// rowIndex是要删除的行序号
}
//表格
link link = new link();
List<Student> lst = link.list();
for (int i = 0; i < lst.size(); i++) {
Student student = (Student) lst.get(i);
tableModel.addRow(new Object[] { student.getId(), student.getName(), student.getSex(),
student.getAge(), student.getTel()});}
jp_c.setViewportView(jtb);
//居中显示
DefaultTableCellRenderer dc=new DefaultTableCellRenderer();
dc.setHorizontalAlignment(JLabel.CENTER);
jtb.setDefaultRenderer(Object.class, dc);
}
}
}