数据库用的是mysql。
表的结构如下:
//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(); } }
运行结果如下: