数据库编程+图形用户界面设计

题目简介:

编写图形用户界面程序实现student表格数据的浏览,添加,查找,修改,删除,排序。界面自由设计。

先提供运行界面:

①分层结构:

②界面设计:

(3)数据库:

接下来是代码:

①包connection(连接数据库)

(1)类DAO

package connection;
import java.sql.Connection;
import java.sql.DriverManager;
import javax.swing.JOptionPane;

public class DAO {
    private static String driverName = "com.mysql.cj.jdbc.Driver";
    private static String userName = "root";//填写自己要访问的数据库的用户名//hh
    private static String password = "123456";//填写自己要访问的数据库用户名的密码//hh

    private static DAO dao=new DAO();
    public DAO(){
      try{
          Class.forName("com.mysql.cj.jdbc.Driver");//加载驱动
         // Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
      }catch(ClassNotFoundException e){
          e.printStackTrace();
      }
    }
    public static Connection getConn() {
        String url1 = "jdbc:mysql://localhost:3306/student";//填写自己建立访问的数据库名//hh
        String url2 = "?user=" + userName + "&password=" + password;//不要改
        String url3 = "&userUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=UTC";
        String url = url1 + url2 + url3;
        Connection conn = null;
        try {
            conn = null;
            conn = DriverManager.getConnection(url);

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

(2)类SQL:

package connection;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class SQL {
    public static void main(String[] args) {
        //定义需要的对象
        Connection con=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        try{
        Class.forName("com.mysql.cj.jdbc.Driver");//加载驱动
        String url="jdbc:mysql://localhost:3306/student?useSSL=false&serverTimezone=UTC"; //连接字符串
        con=DriverManager.getConnection(url,"root","123456");//连接数据库
        ps=con.prepareStatement("select *from students");//4、执行查询(如果是增加、删除、修改则用executeUpdate()
        rs=ps.executeQuery();
        while(rs.next()) {
            String number = rs.getString(1);
            String name = rs.getString(2);
            String score = rs.getString(3);
            System.out.println(number + "\t" + name + "\t" + score);
         }
        }catch(ClassNotFoundException e){
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try{
                if(rs!=null){
                    rs.close();
                }
                if(ps!=null){
                    ps.close();
                }
                if(con!=null){
                    con.close();
                }
            }catch (SQLException e){
                e.printStackTrace();
            }
        }
    }
}

②包student

(1)类MainFrame

package student;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.AbstractAction;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import connection.*;
import students.*;

import javax.swing.*;
import java.awt.*;

public class MainFrame extends JFrame {
    JMenuBar menubar=null;
    JMenu jm=null;
    JPanel jp;//图片
    Icon icon;
    JLabel j1;
    public MainFrame(){
        setSize(600,400);
        setTitle("学生管理系统");
        setJMenuBar(getJMenuBar1());
        setLocationRelativeTo(null);
        setResizable(true);
    }
    private JMenuBar getJMenuBar1() {
        final JLabel label=new JLabel();

        label.setForeground(new Color(13, 14, 13));
        label.setFont(new Font("",Font.BOLD,18));
        label.setText("学生管理系统");
        label.setBounds(39,28,170,36);
        getContentPane().add(label);

        if(menubar==null) {
            menubar = new JMenuBar();
            JMenu jm1 = new JMenu("菜单");
            JMenu jm2 = new JMenu("帮助");
            menubar.add(jm1);
            menubar.add(jm2);
            JPanel jp=new JPanel();
            Icon iocn=new ImageIcon("src/picture/pic.jpg");
            JLabel j1=new JLabel(iocn);
            jp.add(j1);
            this.add(jp);

            JMenuItem studentsort = new JMenuItem("按分数排序");
            JMenuItem studentinsert = new JMenuItem("添加学生信息");
            JMenuItem studentquery = new JMenuItem("学生信息查询");
            JMenuItem studentupdate = new JMenuItem("修改学生信息");
            JMenuItem studentdelete = new JMenuItem("学生信息删除");
            JMenuItem studentexit = new JMenuItem("退出系统");
            JMenuItem studentallquery = new JMenuItem("全部学生信息查询");

            JMenuItem introduce = new JMenuItem("软件说明");

            jm1.add(studentsort);
            jm1.add(studentinsert);
            jm1.add(studentquery);
            jm1.add(studentupdate);
            jm1.add(studentdelete);
            jm1.add(studentallquery);
            jm1.add(studentexit);


            jm2.add(introduce);

            studentinsert.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    StudentInsert si = new StudentInsert();
                    si.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                    si.setVisible(true);
                }
            });
            studentquery.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    StudentQuery sq = new StudentQuery();
                    sq.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                    sq.setVisible(true);
                }
            });
            studentallquery.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    StudentAllquery sa = new  StudentAllquery();
                    sa.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                    sa.setVisible(true);
                }
            });
            studentupdate.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    StudentUpdate su = new StudentUpdate();
                    su.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                    su.setVisible(true);
                }
            });
            studentdelete.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    StudentDelete sd = new StudentDelete();
                    sd.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                    sd.setVisible(true);
                }
            });
           studentsort.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    StudentSort st = new StudentSort();
                    st.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                    st.setVisible(true);
                }
            });
            studentexit.addActionListener(new ActionListener() {//退出
                public void actionPerformed(ActionEvent arg0) {
                    // TODO Auto-generated method stub
                    System.exit(1);
                }
            });
            introduce.addActionListener(new ActionListener() {//软件说明
                public void actionPerformed(ActionEvent arg0) {
                    // TODO Auto-generated method stub
                    Introduce id = new Introduce();
                    id.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                    id.setVisible(true);
                }
            });
        }
            return menubar;
    }
    public static void main(String[] args) {
        MainFrame mf=new MainFrame();
        mf.setVisible(true);
        mf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }

    }

(3)包students

(1)类Introduce

package students;

import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Introduce extends JFrame {
    JLabel jl1,jl2,jl3;
    JPanel jp1,jp2,jp3;
    public Introduce(){
        jl1=new JLabel("本系统提供查询、删除、修改、更新、排序功能");
        jl2=new JLabel("提高信息准确度;改进管理和服务;");
        jl3=new JLabel("更简便、信息化程度更高的管理流程;");

        jp1=new JPanel();
        jp2=new JPanel();
        jp3=new JPanel();
        setLayout(new GridLayout(10, 1));

        jp1.add(jl1);
        jp2.add(jl2);
        jp3.add(jl3);

        this.add(jp1);
        this.add(jp2);
        this.add(jp3);

        setTitle("说明");
        setSize(600,500);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setVisible(true);
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Introduce id=new Introduce();
    }
}

(2)类StudentAllquery(全部查询)

package students;
import connection.DAO;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextField;

public class StudentAllquery extends JFrame{
    JTabbedPane tab=new JTabbedPane();
    JLabel jl1,jl2,jl3;
    JTextField jf1,jf2,jf3;//文本
    JButton jb1;//按钮
    JPanel jp1,jp2,jp3;

    public StudentAllquery() {
        jp1 = new JPanel();
        jb1 = new JButton("全部查询");
        //jp4=new JPanel();
        jf1 = new JTextField(10);
        Object[][] data = new Object[50][3];
        Object[] col = {"学号", "姓名", "分数"};
        JTable query = new JTable(data, col);
        JScrollPane view = new JScrollPane(query);


      //  jp1.add(jf1);
        jp1.add(jb1);
        jp1.add(view);
        query.setVisible(false);
        query.setEnabled(false); // 设置表格中数据不能被选定
        tab.add("查询数据", jp1);
        this.add(jp1);

        setTitle("全部学生查询");
        setSize(600, 400);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setLocationRelativeTo(null);
        setVisible(true);

        jb1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                Connection conn = null;
                Statement ps = null;
                ResultSet rs = null;
                conn = DAO.getConn();
                try {
                    for (int i = 0; i < 50; i++)  //清除数据
                        for (int j = 0; j < 3; j++)
                            data[i][j] = "";
                    String sql = "SELECT * FROM students";
                    conn = new DAO().getConn();
                      ps = conn.createStatement();
                      rs = ps.executeQuery(sql);
                    int row = 0;
                    while (rs.next()) {
                        data[row][0] = rs.getString(1);
                        data[row][1] = rs.getString(2);
                        data[row][2] = rs.getString(3);
                        row++;
                    }
                    query.setVisible(false);   //刷新表格
                    query.setVisible(true);
                  //  JOptionPane.showMessageDialog(null, "查询成功!!");
                } catch (SQLException e1) {
                    e1.printStackTrace();
                }
            }
        });
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        StudentAllquery bc=new StudentAllquery();
    }
}

(3)类StudentDelete(删除信息)

package students;
import java.awt.GridLayout;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import connection.*;
public class StudentDelete extends JFrame{
    JLabel jl1;
    JTextField jf1;
    JButton jb1,jb2;
    JPanel jp1,jp2,jp3;
    public StudentDelete(){
        jl1=new JLabel("请输入你要删除的学生学号");
        jf1=new JTextField(18);
        jb1=new JButton("删除");
        jb2=new JButton("重置");
        jp1=new JPanel();
        jp2=new JPanel();
        jp3=new JPanel();
        setLayout(new GridLayout(3, 1));
        jp1.add(jl1);
        jp2.add(jf1);
        jp3.add(jb1);
        jp3.add(jb2);

        this.add(jp1);
        this.add(jp2);
        this.add(jp3);
        setTitle("学生删除");
        setSize(400,250);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setLocationRelativeTo(null);
        setVisible(true);

        jb1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub
                String studentno=jf1.getText().trim();
                try {
                    if(studentno.equals("")){
                        JOptionPane.showMessageDialog(null, "请输入你要删除的学生学号");
                    }else{
                        Connection conn=null;
                        PreparedStatement ps=null;
                        int i;
                        conn=DAO.getConn();
                        /*String sql="delete * from students where sno='"+studentno+"'";
                        //执行sql
                        ps=conn.prepareStatement(sql);*/
                        String sql2="delete  from students where sno=?";//准备sql语句,?是占位符,内容待定
                        PreparedStatement pst2=conn.prepareStatement(sql2);//创建封装语句的对象
                        pst2.setString(1,studentno);//给第1个占位符赋值
                       // pst2.executeUpdate();//执行sql语句
                        i=pst2.executeUpdate();
                        if(i==1){
                            JOptionPane.showMessageDialog(null, "删除学生成功");
                        }else{
                            JOptionPane.showMessageDialog(null, "删除学生失败");
                        }

                    }
                } catch (HeadlessException e) {
                    e.printStackTrace();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        });

        jb2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub
                jf1.setText("");
            }
        });
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
       StudentDelete bd=new StudentDelete();
    }
}

(4)类StudentInsert(添加信息)

package students;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import connection.*;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class StudentInsert extends JFrame{
  JLabel jl1,jl2,jl3;
    JButton jb1,jb2;
    JPanel jp1,jp2,jp3,jp4;
    JTextField jt1,jt2,jt3;
    public StudentInsert(){
        setSize(400,300);
        setTitle("添加学生信息");
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);

       setLayout(new GridLayout(6,1));
        jp1=new JPanel();//面板
        jp2=new JPanel();
        jp3=new JPanel();
        jp4=new JPanel();


        jl1=new JLabel("学生学号");//标签
        jl2=new JLabel("学生姓名");
        jl3=new JLabel("学生分数");

        jt1=new JTextField(15);
        jt2=new JTextField(15);
        jt3=new JTextField(15);

        jb1=new JButton("添加");//按钮
        jb2=new JButton("重置");

        jp1.add(jl1);jp1.add(jt1);
        jp2.add(jl2);jp2.add(jt2);
        jp3.add(jl3);jp3.add(jt3);
        jp4.add(jb1, BorderLayout.SOUTH);
        jp4.add(jb2,BorderLayout.SOUTH);

        this.add(jp1);
        this.add(jp2);
        this.add(jp3);
        this.add(jp4);

        jb1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String sno=jt1.getText().trim();
                String sname=jt2.getText().trim();
                String score=jt3.getText().trim();
                try{
                   if(sno.equals("")||sname.equals("")||score.equals("")){
                       JOptionPane.showMessageDialog(null,"请输入完整信息");
                   }else{
                       Connection conn=null;
                       PreparedStatement ps=null;
                       conn=DAO.getConn();
                     //  System.out.println(conn);
                    //   ps=conn.prepareStatement("insert into students values ('"+sno+"','"+sname+"','"+score+"')");
                       String sql1="insert into students values(?,?,?)";//准备sql语句,?是占位符,内容待定
                       PreparedStatement pst1=conn.prepareStatement(sql1);//创建封装语句的对象
                       pst1.setString(1,sno);//给第1个占位符赋值
                       pst1.setString(2,sname);//给第2个占位符赋值
                       pst1.setString(3, score);//给第3个占位符赋值
                    //   pst1.executeUpdate();//执行sql语句1
                       int i=pst1.executeUpdate();
                       if(i==1){
                           JOptionPane.showMessageDialog(null,"添加成功");
                       }else{
                           JOptionPane.showMessageDialog(null,"添加失败");
                       }
                   }
                } catch (HeadlessException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                } catch (SQLException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }
        });
        jb2.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub
                jt1.setText("");
                jt2.setText("");
                jt3.setText("");
            }
        });
    }
    public static void main(String[] args){
       StudentInsert b=new StudentInsert();
        b.setVisible(true);
        b.setResizable(false);
    }
}

(5)类StudentQuery

package students;
import java.awt.GridLayout;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import connection.*;
public class StudentQuery extends JFrame{
    JLabel jl1,jl2,jl3;
    JTextField jf1,jf2,jf3;//文本
    JButton jb1;//按钮
    JPanel jp1,jp2,jp3;

    public StudentQuery(){
        jl1=new JLabel("查询学生的学号:");
        jl2=new JLabel("学生姓名:");
        jl3=new JLabel("学生分数");
       // jl4=new JLabel("入库总数:");
        jf1=new JTextField(15);
        jf2=new JTextField(15);
        jf3=new JTextField(15);
      //  jf4=new JTextField(15);
        jb1=new JButton("查询");

        jp1=new JPanel();
        jp2=new JPanel();
        jp3=new JPanel();
        //jp4=new JPanel();
        this.setLayout(new GridLayout(4, 1));

        jp1.add(jl1);
        jp1.add(jf1);
        jp1.add(jb1);
        jp2.add(jl2);
        jp2.add(jf2);
        jp3.add(jl3);
        jp3.add(jf3);
        this.add(jp1);
        this.add(jp2);
        this.add(jp3);

        setTitle("学生查询");
        setSize(400,300);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setLocationRelativeTo(null);
        setVisible(true);

        jb1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                String studentno=jf1.getText().trim();
                Connection conn=null;
                PreparedStatement ps=null;
                ResultSet rs=null;
                conn=DAO.getConn();
                try {
                    if(studentno.equals("")){
                        JOptionPane.showMessageDialog(null,"请输入你要查询的学生学号");
                    }else{
                        String sql="select * from students where sno='"+studentno+"'";
                        ps=conn.prepareStatement(sql);
                        rs=ps.executeQuery();
                        if(rs.next()){
                            jf2.setText(rs.getString(2));
                            jf3.setText(rs.getString(3));
                        }else{
                            JOptionPane.showMessageDialog(null,"你查询的学生不存在");
                        }
                    }
                } catch (HeadlessException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                } catch (SQLException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }
        });
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
       StudentQuery bc=new StudentQuery();
    }
}

(6)类StudentSort(排序)

package students;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import java.util.Vector;

import connection.*;
public class StudentSort extends JFrame{
    JTabbedPane tab=new JTabbedPane();
    JLabel jl1,jl2,jl3;
    JTextField jf1,jf2,jf3;//文本
    JButton jb1;//按钮
    JPanel jp1,jp2,jp3;

    public StudentSort() {
        jp1 = new JPanel();
        jb1 = new JButton("学生信息排序");
        //jp4=new JPanel();
        jf1 = new JTextField(10);
        Object[][] data = new Object[50][3];
        Object[] col = {"学号", "姓名", "分数"};
        JTable query = new JTable(data, col);
        JScrollPane view = new JScrollPane(query);


        //  jp1.add(jf1);
        jp1.add(jb1);
        jp1.add(view);
        query.setVisible(false);
        query.setEnabled(false); // 设置表格中数据不能被选定
        tab.add("排序数据", jp1);
        this.add(jp1);

        setTitle("全部学生排序");
        setSize(600, 400);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setLocationRelativeTo(null);
        setVisible(true);

        jb1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                Connection conn = null;
                Statement ps = null;
                ResultSet rs = null;
                conn = DAO.getConn();
                try {
                    for (int i = 0; i < 50; i++)  //清除数据
                        for (int j = 0; j < 3; j++)
                            data[i][j] = "";
                    String sql = "select * from students order by score";
                    conn = new DAO().getConn();
                    ps = conn.createStatement();
                    rs = ps.executeQuery(sql);
                    int row = 0;
                    while (rs.next()) {
                        data[row][0] = rs.getString(1);
                        data[row][1] = rs.getString(2);
                        data[row][2] = rs.getString(3);
                        row++;
                    }
                    query.setVisible(false);   //刷新表格
                    query.setVisible(true);
                    //  JOptionPane.showMessageDialog(null, "查询成功!!");
                } catch (SQLException e1) {
                    e1.printStackTrace();
                }
            }
        });
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        StudentSort bc=new  StudentSort();
    }
}

(7)类StudentUpdate(更新信息)

package students;
import java.awt.GridLayout;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import connection.*;
public class StudentUpdate extends JFrame{
    JLabel jl1,jl2,jl3;
    JTextField jt1,jt2,jt3;//文本
    JButton jb1,jb2;
    JPanel jp1,jp2,jp3,jp4;

    public  StudentUpdate (){
        jl1=new JLabel("学生学号");
        jl2=new JLabel("学生姓名");
        jl3=new JLabel("学生分数");

        jt1=new JTextField(15);
        jt2=new JTextField(15);
        jt3=new JTextField(15);

        jb1=new JButton("查询");
        jb2=new JButton("修改");

        jp1=new JPanel();
        jp2=new JPanel();
        jp3=new JPanel();
        jp4=new JPanel();
        setLayout(new GridLayout(4, 1));

        jp1.add(jl1);
        jp1.add(jt1);
        jp2.add(jl2);
        jp2.add(jt2);
        jp3.add(jl3);
        jp3.add(jt3);
        jp4.add(jb1);
        jp4.add(jb2);
        this.add(jp1);this.add(jp2);
        this.add(jp3);this.add(jp4);

        setTitle("学生修改");
        setSize(500,350);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setVisible(true);

        jb1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub
                String studentno=jt1.getText().trim();
                Connection conn=null;
                PreparedStatement ps=null;
                ResultSet rs=null;
                conn=DAO.getConn();
                try {
                    if(studentno.equals("")){
                        JOptionPane.showMessageDialog(null,"请输入你要查询的学生学号");
                    }else{
                        String sql="select * from students where sno='"+studentno+"'";
                        ps=conn.prepareStatement(sql);
                        rs=ps.executeQuery();
                        if(rs.next()){
                            jt2.setText(rs.getString(2));
                            jt3.setText(rs.getString(3));
                        }else{
                            JOptionPane.showMessageDialog(null,"你查询的学生不存在");
                        }
                    }
                } catch (HeadlessException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                } catch (SQLException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }
        });

        jb2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String sno1=jt1.getText().trim();
                String sname1=jt2.getText().trim();
                String score1=jt3.getText().trim();
                try{
                    if(sno1.equals("")||sname1.equals("")||score1.equals("")){
                        JOptionPane.showMessageDialog(null,"请输入完整信息");
                    }else{
                        Connection conn=null;
                        PreparedStatement pst1=null;
                        conn=DAO.getConn();
                        String sql1="update students set sname=?,score=? where sno=?";//准备sql语句,?是占位符,内容待定
                        pst1=conn.prepareStatement(sql1);//创建封装语句的对象
                        pst1.setString(1,sname1);//给第2个占位符赋值
                        pst1.setString(2,score1);//给第1个占位符赋值
                        pst1.setString(3,sno1);//给第2个占位符赋值

                        int i=pst1.executeUpdate();
                        if(i==1){
                            JOptionPane.showMessageDialog(null,"修改成功");
                        }else{
                            JOptionPane.showMessageDialog(null,"修改失败");
                        }
                    }
                } catch (HeadlessException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                } catch (SQLException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }
        });
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        StudentUpdate bu=new  StudentUpdate();
    }
}

注意!!

如果是新版本的数据库和JDBC驱动等,那么在数据库连接时需要加入时区检验

String url1 = "jdbc:mysql://localhost:3306/student";//填写自己建立访问的数据库名//hh
String url2 = "?user=" + userName + "&password=" + password;//不要改
String url3 = "&userUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=UTC";
String url = url1 + url2 + url3;

发布者

deng

听闻余生久不遇,相逢别错过。

《数据库编程+图形用户界面设计》上有1条评论

发表评论

电子邮件地址不会被公开。 必填项已用*标注