设计一个数据表用于保存图书信息,需要保存图书的书名、价格、作者、出版社等信息。开发一个带界面的程序,用户可以向该数据表中填加记录、删除记录,修改已有图书记录,并可根据书名、价格、作者等条件查询图书

 数据库用的是mysql。

表的结构如下:7e6b8748fcf543df90dba033b509e850.png

//GetMysqlconnection.java

 

import java.sql.*;
public class GetMysqlconnection {
    public static Connection connectMysql(String Mysqlname,String id,String p){
        Connection con=null;
        String uri =
                "jdbc:mysql://localhost:3306/students?"+
                        "useSSL=true&serverTimezone=GMT";
        try{  Class.forName("com.mysql.cj.jdbc.Driver"); //加载JDBC_MySQL驱动
        }
        catch(Exception e){}
        try{
            con = DriverManager.getConnection(uri,id,p); //连接代码
        }
        catch(SQLException e){}
        return con;
    }
}
//Operator.java
import java.sql.*;
public class Operator {
    Connection con;
    Statement sql;
    ResultSet rs;
    /*Operator(Connection con,Statement sql,ResultSet rs){
        this.con=con;
        this.sql=sql;
        this.rs=rs;
    }*/
    Operator(){
        try {
            con=GetMysqlconnection.connectMysql("student","root","你的密码");
            sql=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
            rs= sql.executeQuery("SELECT * FROM book");
        }catch (SQLException e){
            System.out.println("发生错误:"+e);
        }
    }
 }
 
//WindowDate.java
import java.awt.*;
import java.sql.*;
import javax.swing.*;
public class WindowDate extends JFrame {
    String[] tableHead;
    String[][] content;
    JTable table;
    JPanel panel=new JPanel();
    JButton button1,button2,button3,button4;
    Operator operator=new Operator();
    WindowDate(){
        try {
            ResultSetMetaData metaData = operator.rs.getMetaData();
            int columnCount=metaData.getColumnCount();
            tableHead=new String[columnCount];
            for (int i=1;i<=columnCount;i++){
                tableHead[i-1]=metaData.getColumnName(i);
            }
            operator.rs.last();
            int recordAmount=operator.rs.getRow();
        content=new String[recordAmount][columnCount];
            int i=0;
            operator.rs.beforeFirst();
            while (operator.rs.next()){
                for(int j=1;j<=columnCount;j++){
                    content[i][j-1]=operator.rs.getString(j);
                }
                i++;
            }
        }catch (SQLException e){
            System.out.println("发生错误:"+e);
        }
        table=new JTable(content,tableHead);
        JLabel hint=new JLabel("一次只能添加一组数据;书名为空再点删除即可删除");
        button1=new JButton("添加");
        button2=new JButton("删除");
        button3=new JButton("修改");
        button4=new JButton("查询");
        AddListener add1=new AddListener();
        add1.setOperator(this);
        DeleteListener delete2=new DeleteListener();
        delete2.setOperator(this);
        ChangeListener change=new ChangeListener();
        change.setOperator(this);
        InquireListener inquire=new InquireListener();
        inquire.setOperator(this);
        button1.addActionListener(add1);
        button2.addActionListener(delete2);
        button3.addActionListener(change);
        button4.addActionListener(inquire);
        panel.add(button1);
        panel.add(button2);
        panel.add(button3);
        panel.add(button4);
        table.setSize(400,300);
        panel.setSize(400,100);
        //table.setFont(font);
        add(new JScrollPane(table),BorderLayout.CENTER);
        add(hint,BorderLayout.NORTH);
        add(panel,BorderLayout.SOUTH);
        setTitle("数据库");
        setBounds(750,400,400,200);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

}
//AddListener.java
import java.awt.event.*;
import java.sql.SQLException;

public class AddListener implements ActionListener {
    WindowDate windowDate;

    public void setOperator(WindowDate windowDate) {
        this.windowDate = windowDate;
    }
    public void actionPerformed(ActionEvent e) {
        try {
            String sed;
            sed = "('" + "" + "'," + 0 + ",'" + "" + "','" + "" + "')";
            String ddd="insert into book values"+sed;
            int ko=windowDate.operator.sql.executeUpdate(ddd);
            }catch (SQLException ee){
            System.out.println("发生异常:"+ee);
        }
        windowDate.validate();
        WindowDate windowDate1=new WindowDate();
        windowDate.remove(windowDate1);
        windowDate.setVisible(false);

    }
}

 

//DeleteListener.java

import java.awt.event.*;
import java.sql.SQLException;

public class DeleteListener implements ActionListener {
        WindowDate windowDate;
        public void setOperator(WindowDate windowDate){
                this.windowDate=windowDate;
        }
        public void actionPerformed(ActionEvent e) {
                try {
                        String sqlStr = "delete from book";
                        int ok = windowDate.operator.sql.executeUpdate(sqlStr);
                        String sed;
                        String[] edg=new String[4];
                        for (int i = 0; i < windowDate.content.length; i++) {
                                for (int j = 0; j < windowDate.content[i].length; j++) {
                                        edg[j] = windowDate.content[i][j];
                                }
                                sed = "('" + edg[0] + "'," + edg[1] + ",'" + edg[2] + "','" + edg[3] + "')";
                                String ddd = "insert into book values" + sed;
                                int ko = windowDate.operator.sql.executeUpdate(ddd);
                        }
                        String sqlStr1 = "delete from book where 书名 = ' '";
                        int ok1 = windowDate.operator.sql.executeUpdate(sqlStr1);
                        windowDate.validate();
                }catch (SQLException ee){
                        System.out.println("发生异常:"+ee);
                }
                windowDate.validate();
                WindowDate windowDate1=new WindowDate();
                windowDate.remove(windowDate1);
                windowDate.setVisible(false);
        }
}
//ChangeListener.java

import java.awt.event.*;
import java.sql.SQLException;
public class ChangeListener implements ActionListener {
    WindowDate windowDate;

    public void setOperator(WindowDate windowDate) {
        this.windowDate = windowDate;
    }

    public void actionPerformed(ActionEvent e) {
        try {
            String sqlStr = "delete from book";
            int ok = windowDate.operator.sql.executeUpdate(sqlStr);
            String sed;
            String[] edg=new String[4];
            for (int i = 0; i < windowDate.content.length; i++) {
                for (int j = 0; j < windowDate.content[i].length; j++) {
                    edg[j]=windowDate.content[i][j];
                }
                sed="('"+edg[0]+"',"+edg[1]+",'"+edg[2]+"','"+edg[3]+"')";
                String ddd="insert into book values"+sed;
                int ko=windowDate.operator.sql.executeUpdate(ddd);
            }
        }catch (SQLException ee) {
            System.out.println("发生异常:" + ee);
        }
            windowDate.validate();
            WindowDate windowDate1=new WindowDate();
            windowDate.remove(windowDate1);
            windowDate.setVisible(false);
    }
}

 

//InquireListener.java

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
public class InquireListener implements ActionListener {

    WindowDate windowDate;
    String str1;

    public void setOperator(WindowDate windowDate) {
        this.windowDate = windowDate;
    }

    public void actionPerformed(ActionEvent e) {
        String str = JOptionPane.showInputDialog(windowDate, "输入查询的条件:\n"+"例如:书名 = '高等数学'", "查询对话框", JOptionPane.PLAIN_MESSAGE);
        if (str != null) {
            str1 = "SELECT * FROM book where " + str;
            WindowDate3 windowDate3=new WindowDate3();
            windowDate3.setBounds(windowDate.getBounds());
        }
    }

    class WindowDate3 extends JFrame {
        String[] tableHead;
        String[][] content;
        JTable table;
        Operator operator = new Operator();

        WindowDate3() {
            try {
                operator.rs = operator.sql.executeQuery(str1);
                ResultSetMetaData metaData = operator.rs.getMetaData();
                int columnCount = metaData.getColumnCount();
                tableHead = new String[columnCount];
                for (int i = 1; i <= columnCount; i++) {
                    tableHead[i - 1] = metaData.getColumnName(i);
                }
                operator.rs.last();
                int recordAmount = operator.rs.getRow();
                content = new String[recordAmount][columnCount];
                int i = 0;
                operator.rs.beforeFirst();
                while (operator.rs.next()) {
                    for (int j = 1; j <= columnCount; j++) {
                        content[i][j - 1] = operator.rs.getString(j);
                    }
                    i++;
                }
            } catch (SQLException e) {
                JOptionPane.showMessageDialog(this,"没有匹配的书籍信息","错误",JOptionPane.ERROR_MESSAGE);
            }
            try {
                table = new JTable(content, tableHead);
                table.setSize(400, 300);
                add(new JScrollPane(table), BorderLayout.CENTER);
                setTitle("筛选结果");
                setBackground(Color.blue);
                setVisible(true);
                setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            }catch (NullPointerException ee){}
        }
    }
}

 //Mainclass.java

import javax.swing.*;
import java.sql.SQLException;

public class Mainclass {
    public static void main(String []args) {
       WindowDate win=new WindowDate();
       try{
        win.operator.rs=win.operator.sql.executeQuery("SELECT * FROM book where 书名 = '高等数学'");
          }catch (SQLException e){
           System.out.println("发生错误:"+e);
       }
        SwingUtilities.updateComponentTreeUI(win);
        win.invalidate();
        win.validate();
        win.repaint();
    }
}

运行结果如下:

74f399f0a51c4f27aaa21f85680cdf14.png

 

 

 

  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值