Test3.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. package C;
  2. import javax.swing.*;
  3. import java.util.*;
  4. import java.awt.*;
  5. import java.awt.event.*;
  6. import java.sql.Connection;
  7. import java.sql.Driver;
  8. import java.sql.DriverManager;
  9. import java.sql.PreparedStatement;
  10. import java.sql.ResultSet;
  11. import java.sql.Statement;
  12. public class Test3 extends JFrame implements ActionListener {
  13. //定义一些控件
  14. JPanel jp1,jp2;
  15. JLabel jl1,jl2;
  16. JButton jb1,jb2,jb3,jb4,jb5;
  17. JTable jt;
  18. JScrollPane jsp;
  19. JTextField jtf;
  20. StuModel sm;
  21. //定义连接数据库的变量
  22. Statement stat = null;
  23. PreparedStatement ps;
  24. Connection ct = null;
  25. ResultSet rs = null;
  26. public static void main(String[] args){
  27. Test3 test3 = new Test3();
  28. }
  29. //构造函数
  30. public Test3(){
  31. jp1 = new JPanel();
  32. jtf = new JTextField(10);
  33. jb1 = new JButton("查询");
  34. jb1.addActionListener(this);
  35. jl1 = new JLabel("请输入设备名称:");
  36. jp1.add(jl1);
  37. jp1.add(jtf);
  38. jp1.add(jb1);
  39. jb5 = new JButton("返回");
  40. jb5.addActionListener(this);
  41. jp1.add(jb5);
  42. jb2 = new JButton("添加");
  43. jb2.addActionListener(this);
  44. jb3 = new JButton("修改");
  45. jb3.addActionListener(this);
  46. jb4 = new JButton("删除");
  47. jb4.addActionListener(this);
  48. jp2 = new JPanel();
  49. jp2.add(jb2);
  50. jp2.add(jb3);
  51. jp2.add(jb4);
  52. //创建模型对象
  53. sm = new StuModel();
  54. //初始化
  55. jt = new JTable(sm);
  56. jsp = new JScrollPane(jt);
  57. //将jsp放入到jframe中
  58. this.add(jsp);
  59. this.add(jp1,"North");
  60. this.add(jp2,"South");
  61. this.setSize(600, 400);
  62. this.setLocation(300, 200);
  63. this.setDefaultCloseOperation(EXIT_ON_CLOSE);
  64. this.setVisible(true);
  65. }
  66. public void actionPerformed(ActionEvent arg0) {
  67. //判断是哪个按钮被点击
  68. if(arg0.getSource() == jb1){
  69. System.out.println("用户希望被查询...");
  70. //因为把对表的数据封装到StuModel中,可以比较简单的完成查询
  71. String name = this.jtf.getText().trim();
  72. //写一个sql语句
  73. String sql = "select * from stu where 设备名称 = '"+name+"' ";
  74. //构建一个数据模型类,并更新
  75. sm = new StuModel(sql);
  76. //更新jtable
  77. jt.setModel(sm);
  78. }
  79. else if(arg0.getSource() == jb5){
  80. System.out.println("返回...");
  81. String sql = "select * from stu";
  82. sm = new StuModel(sql);
  83. jt.setModel(sm);
  84. }
  85. //一、弹出添加界面
  86. else if(arg0.getSource() == jb2){
  87. System.out.println("添加...");
  88. StuAddDiag sa = new StuAddDiag(this,"添加设备",true);
  89. //重新再获得新的数据模型,
  90. sm = new StuModel();
  91. jt.setModel(sm);
  92. }else if(arg0.getSource() == jb4){
  93. //二、删除记录
  94. //1.得到的ID
  95. int rowNum = this.jt.getSelectedRow();//getSelectedRow会返回给用户点中的行
  96. //如果该用户一行都没有选,就返回-1
  97. if(rowNum == -1){
  98. //提示
  99. JOptionPane.showMessageDialog(this, "请选中一行");
  100. return ;
  101. }
  102. //得到设备ID
  103. String 设备Id = (String)sm.getValueAt(rowNum, 0);
  104. System.out.println("Id: "+设备Id);
  105. //连接数据库,完成删除任务
  106. try{
  107. //1.加载驱动
  108. Class.forName("com.mysql.jdbc.Driver");
  109. //2.连接数据库
  110. String url = "jdbc:mysql://localhost:3306/spdb1";
  111. String user = "root";
  112. String passwd = "qsefthuko";
  113. ct = DriverManager.getConnection(url, user, passwd);
  114. System.out.println("连接成功");
  115. ps = ct.prepareStatement("delete from stu where 设备Id = ?");
  116. ps.setString(1,设备Id);
  117. ps.executeUpdate();
  118. }catch(Exception e){
  119. e.printStackTrace();
  120. }finally{
  121. try{
  122. if(rs!= null){
  123. rs.close();
  124. rs = null;
  125. }
  126. if(ps!= null){
  127. ps.close();
  128. ps = null;
  129. }
  130. if(ct != null){
  131. ct.close();
  132. ct = null;
  133. }
  134. } catch(Exception e){
  135. e.printStackTrace();
  136. }
  137. }
  138. sm = new StuModel();
  139. //更新jtable
  140. jt.setModel(sm);
  141. }else if(arg0.getSource() == jb3){
  142. System.out.println("11111");
  143. //三、用户希望修改
  144. int rowNum = this.jt.getSelectedRow();
  145. if(rowNum == -1){
  146. //提示
  147. JOptionPane.showMessageDialog(this, "请选择一行");
  148. return ;
  149. }
  150. //显示对话框
  151. System.out.println( "12435");
  152. StuUpDiag su = new StuUpDiag(this, "修改设备", true, sm, rowNum);
  153. sm = new StuModel();
  154. jt.setModel(sm);
  155. }
  156. }
  157. }