1 package mingye; 2 import java.awt.BorderLayout; 3 import java.awt.Color; 4 import java.awt.Font; 5 import java.awt.event.ActionEvent; 6 import java.awt.event.ActionListener; 7 import java.awt.event.KeyEvent; 8 import java.awt.event.MouseAdapter; 9 import java.awt.event.MouseEvent; 10 import java.sql.*; 11 import javax.swing.*; 12 13 public class StudentTest { 14 15 private static String url="jdbc:sqlserver://localhost:1433;DatabaseName=aaa"; 16 private static String user="sa"; 17 private static String password="mingye"; 18 static Connection con=null; 19 JFrame f=new JFrame(); 20 JPanel p=new JPanel(); 21 JMenuBar mb=new JMenuBar(); 22 JMenu m=new JMenu("菜单选项(M)"); 23 24 public StudentTest(){ 25 26 p.setLayout(new BorderLayout()); 27 f.setJMenuBar(mb); 28 m.setMnemonic(KeyEvent.VK_M); 29 JLabel l=new JLabel(" 欢迎使用学生基本信息管理系统"); 30 mb.add(m); 31 32 33 JMenuItem mi1=new JMenuItem("录入学生基本信息(L)"); 34 mi1.setMnemonic(KeyEvent.VK_L); 35 mi1.addActionListener(new ActionListener(){ 36 public void actionPerformed(ActionEvent e){ 37 JDialog d=new JDialog(); 38 JLabel l1=new JLabel("学号:");//id 39 JLabel l2=new JLabel("姓名:");//name 40 JLabel l3=new JLabel("性别:");//sex 41 JLabel l4=new JLabel("专业:");//profession 42 JLabel l5=new JLabel("年级:");//grade 43 JLabel l6=new JLabel("出生:");//birthday 44 45 final JTextField t1=new JTextField(15); 46 final JTextField t2=new JTextField(15); 47 final JTextField t3=new JTextField(15); 48 final JTextField t4=new JTextField(15); 49 final JTextField t5=new JTextField(15); 50 51 final JRadioButton jr1=new JRadioButton("男"); 52 final JRadioButton jr2=new JRadioButton("女"); 53 ButtonGroup group=new ButtonGroup(); 54 group.add(jr1); 55 group.add(jr2); 56 JButton b1=new JButton("录入"); 57 b1.addActionListener(new ActionListener() { 58 public void actionPerformed(ActionEvent e) { 59 try { 60 Statement stmt=con.createStatement(); 61 String sql="select * from student2"; 62 PreparedStatement prepareStatement = con.prepareStatement(sql); 63 ResultSet rs=prepareStatement.executeQuery(); 64 65 66 String id; 67 String name; 68 String sex; 69 String profession; 70 String grade; 71 String birthday; 72 73 id=t1.getText(); 74 75 name=t2.getText(); 76 77 if(jr1.isSelected()) 78 sex=jr1.getText(); 79 else 80 sex=jr2.getText(); 81 82 profession=t3.getText(); 83 84 grade=t4.getText(); 85 86 birthday=t5.getText(); 87 88 sql="insert into student2 values(?,?,?,?,?,?)"; 89 PreparedStatement prepareStatement2 = con.prepareStatement(sql); 90 prepareStatement2.setString(1, id); 91 prepareStatement2.setString(2, name); 92 prepareStatement2.setString(3, sex); 93 prepareStatement2.setString(4, profession); 94 prepareStatement2.setString(5, grade); 95 prepareStatement2.setString(6, birthday); 96 int executeUpdate = prepareStatement2.executeUpdate(); 97 System.out.println(executeUpdate); 98 99 rs.close(); 100 stmt.close(); 101 102 JDialog d=new JDialog(); 103 JLabel l1=new JLabel(" 操作完成!"); 104 Font font =new Font("宋体",Font.BOLD,20); 105 l1.setFont(font); 106 l1.setForeground(Color.GREEN); 107 d.add(l1,BorderLayout.CENTER); 108 d.setBounds(650, 400, 200, 100); 109 d.setTitle("温馨提示"); 110 d.setVisible(true); 111 }catch(SQLException e1) { 112 e1.printStackTrace(); 113 } 114 } 115 }); 116 117 JButton b2=new JButton("重置"); 118 b2.addActionListener(new ActionListener(){ 119 public void actionPerformed(ActionEvent e){ 120 t1.setText(null); 121 t2.setText(null); 122 t3.setText(null); 123 t4.setText(null); 124 t5.setText(null); 125 126 jr1.setSelected(false); 127 jr2.setSelected(false); 128 } 129 }); 130 131 Box box1=Box.createVerticalBox(); 132 133 Box box11=Box.createHorizontalBox(); 134 box11.add(Box.createHorizontalStrut(80)); 135 box11.add(l1); 136 box11.add(t1); 137 box11.add(Box.createHorizontalStrut(80)); 138 139 Box box12=Box.createHorizontalBox(); 140 box12.add(Box.createHorizontalStrut(80)); 141 box12.add(l2); 142 box12.add(t2); 143 box12.add(Box.createHorizontalStrut(80)); 144 145 Box box13=Box.createHorizontalBox(); 146 147 box13.add(l3); 148 box13.add(jr1); 149 box13.add(jr2); 150 box13.add(Box.createHorizontalStrut(80)); 151 152 153 Box box14=Box.createHorizontalBox(); 154 box14.add(Box.createHorizontalStrut(80)); 155 box14.add(l4); 156 box14.add(t3); 157 box14.add(Box.createHorizontalStrut(80)); 158 159 Box box15=Box.createHorizontalBox(); 160 box15.add(Box.createHorizontalStrut(80)); 161 box15.add(l5); 162 box15.add(t4); 163 box15.add(Box.createHorizontalStrut(80)); 164 165 Box box16=Box.createHorizontalBox(); 166 box16.add(Box.createHorizontalStrut(80)); 167 box16.add(l6); 168 box16.add(t5); 169 box16.add(Box.createHorizontalStrut(80)); 170 171 Box box17 =Box.createHorizontalBox(); 172 box17.add(Box.createHorizontalStrut(95)); 173 box17.add(b1); 174 box17.add(Box.createHorizontalStrut(40)); 175 box17.add(b2); 176 box17.add(Box.createHorizontalStrut(80)); 177 178 box1.add(Box.createVerticalStrut(10)); 179 box1.add(box11); 180 box1.add(box12); 181 box1.add(box13); 182 box1.add(box14); 183 box1.add(box15); 184 box1.add(box16); 185 box1.add(Box.createVerticalStrut(10)); 186 box1.add(box17); 187 box1.add(Box.createVerticalStrut(10)); 188 d.setTitle("录入学生信息"); 189 d.add(box1,BorderLayout.CENTER); 190 d.setBounds(550, 300, 400, 260); 191 d.setVisible(true); 192 } 193 }); 194 195 196 JMenuItem mi2=new JMenuItem("修改学生基本信息(R)"); 197 mi2.setMnemonic(KeyEvent.VK_R); 198 mi2.addActionListener(new ActionListener(){ 199 public void actionPerformed(ActionEvent e){ 200 JDialog d=new JDialog(); 201 JLabel l1=new JLabel("请输入要修改信息的学号:"); 202 JLabel l2=new JLabel("(新)姓名:"); 203 JLabel l3=new JLabel("(新)性别:"); 204 JLabel l4=new JLabel("(新)专业:"); 205 JLabel l5=new JLabel("(新)年级:"); 206 JLabel l6=new JLabel("(新)出生:"); 207 final JTextField t1=new JTextField(15); 208 final JTextField t2=new JTextField(15); 209 final JTextField t3=new JTextField(15); 210 final JTextField t4=new JTextField(15); 211 final JTextField t5=new JTextField(15); 212 213 final JRadioButton jr1=new JRadioButton("男"); 214 final JRadioButton jr2=new JRadioButton("女"); 215 ButtonGroup group=new ButtonGroup(); 216 group.add(jr1); 217 group.add(jr2); 218 JButton b1=new JButton("录入修改"); 219 b1.addActionListener(new ActionListener() { 220 public void actionPerformed(ActionEvent e) { 221 try { 222 Statement stmt=con.createStatement(); 223 String sql="select * from student2"; 224 PreparedStatement prepareStatement = con.prepareStatement(sql); 225 ResultSet rs=prepareStatement.executeQuery(); 226 227 228 String id; 229 String name; 230 String sex; 231 String profession; 232 String grade; 233 String birthday; 234 235 id=t1.getText(); 236 237 name=t2.getText(); 238 239 if(jr1.isSelected()) 240 sex=jr1.getText(); 241 else 242 sex=jr2.getText(); 243 244 profession=t3.getText(); 245 246 grade=t4.getText(); 247 248 birthday=t5.getText(); 249 250 sql="insert into student2 values(?,?,?,?,?,?)"; 251 PreparedStatement prepareStatement2 = con.prepareStatement(sql); 252 prepareStatement2.setString(1, id); 253 prepareStatement2.setString(2, name); 254 prepareStatement2.setString(3, sex); 255 prepareStatement2.setString(4, profession); 256 prepareStatement2.setString(5, grade); 257 prepareStatement2.setString(6, birthday); 258 int executeUpdate = prepareStatement2.executeUpdate(); 259 System.out.println(executeUpdate); 260 261 262 JDialog d=new JDialog(); 263 JLabel l1=new JLabel(" 操作完成!"); 264 Font font =new Font("宋体",Font.BOLD,20); 265 l1.setFont(font); 266 l1.setForeground(Color.GREEN); 267 d.add(l1,BorderLayout.CENTER); 268 d.setBounds(650, 400, 200, 100); 269 d.setTitle("温馨提示"); 270 d.setVisible(true); 271 272 rs.close(); 273 stmt.close(); 274 275 }catch(SQLException e1) { 276 e1.printStackTrace(); 277 } 278 } 279 }); 280 281 282 283 284 JButton b2=new JButton("重置"); 285 b2.addActionListener(new ActionListener(){ 286 public void actionPerformed(ActionEvent e){ 287 t1.setText(null); 288 t2.setText(null); 289 t3.setText(null); 290 t4.setText(null); 291 t5.setText(null); 292 jr1.setSelected(false); 293 jr2.setSelected(false); 294 } 295 }); 296 JButton b3=new JButton("开始修改"); 297 b3.addActionListener(new ActionListener() { 298 public void actionPerformed(ActionEvent e) { 299 try { 300 301 302 String sql1="delete from student2 where id=?"; 303 PreparedStatement prepareStatement2 = con.prepareStatement(sql1); 304 305 String id; 306 id=t1.getText(); 307 prepareStatement2.setString(1, id); 308 prepareStatement2.executeUpdate(); 309 310 311 312 313 }catch(SQLException e1) { 314 e1.printStackTrace(); 315 } 316 } 317 }); 318 319 Box box1=Box.createVerticalBox(); 320 321 Box box11=Box.createHorizontalBox(); 322 box11.add(Box.createHorizontalStrut(80)); 323 box11.add(l1); 324 box11.add(t1); 325 box11.add(b3); 326 box11.add(Box.createHorizontalStrut(80)); 327 328 329 Box box12=Box.createHorizontalBox(); 330 box12.add(Box.createHorizontalStrut(80)); 331 box12.add(l2); 332 box12.add(t2); 333 box12.add(Box.createHorizontalStrut(80)); 334 335 Box box13=Box.createHorizontalBox(); 336 box13.add(Box.createHorizontalStrut(1)); 337 box13.add(l3); 338 box13.add(jr1); 339 box13.add(jr2); 340 box13.add(Box.createHorizontalStrut(80)); 341 342 343 Box box14=Box.createHorizontalBox(); 344 box14.add(Box.createHorizontalStrut(80)); 345 box14.add(l4); 346 box14.add(t3); 347 box14.add(Box.createHorizontalStrut(80)); 348 349 350 Box box15=Box.createHorizontalBox(); 351 box15.add(Box.createHorizontalStrut(80)); 352 box15.add(l5); 353 box15.add(t4); 354 box15.add(Box.createHorizontalStrut(80)); 355 356 Box box16=Box.createHorizontalBox(); 357 box16.add(Box.createHorizontalStrut(80)); 358 box16.add(l6); 359 box16.add(t5); 360 box16.add(Box.createHorizontalStrut(80)); 361 362 363 Box box17 =Box.createHorizontalBox(); 364 box17.add(Box.createHorizontalStrut(95)); 365 box17.add(b1); 366 box17.add(Box.createHorizontalStrut(40)); 367 box17.add(b2); 368 box17.add(Box.createHorizontalStrut(80)); 369 370 box1.add(Box.createVerticalStrut(10)); 371 box1.add(box11); 372 box1.add(box12); 373 box1.add(box13); 374 box1.add(box14); 375 box1.add(box15); 376 box1.add(box16); 377 box1.add(Box.createVerticalStrut(10)); 378 box1.add(box17); 379 box1.add(Box.createVerticalStrut(10)); 380 d.setTitle("修改学生信息"); 381 d.add(box1,BorderLayout.CENTER); 382 d.setBounds(550, 300, 530, 250); 383 d.setVisible(true); 384 } 385 }); 386 387 388 389 390 JMenuItem mi3=new JMenuItem("查询学生基本信息(S)"); 391 mi3.setMnemonic(KeyEvent.VK_S); 392 mi3.addActionListener(new ActionListener(){ 393 public void actionPerformed(ActionEvent e){ 394 JDialog d=new JDialog(); 395 JLabel l1=new JLabel("请输入要查询的学号:"); 396 JLabel l2=new JLabel("姓名:"); 397 JLabel l3=new JLabel("性别:"); 398 JLabel l4=new JLabel("专业:"); 399 JLabel l5=new JLabel("年级:"); 400 JLabel l6=new JLabel("出生:"); 401 JTextField t1=new JTextField(15); 402 JTextField t2=new JTextField(15); 403 JTextField t3=new JTextField(15); 404 JTextField t4=new JTextField(15); 405 JTextField t5=new JTextField(15); 406 407 JRadioButton jr1=new JRadioButton("男"); 408 JRadioButton jr2=new JRadioButton("女"); 409 ButtonGroup group=new ButtonGroup(); 410 group.add(jr1); 411 group.add(jr2); 412 413 414 JButton b3=new JButton("查询"); 415 b3.addActionListener(new ActionListener() { 416 public void actionPerformed(ActionEvent e) { 417 try { 418 Statement stmt=con.createStatement(); 419 String sql="select * from student2 where id=?"; 420 PreparedStatement prepareStatement = con.prepareStatement(sql); 421 String id; 422 id=t1.getText(); 423 prepareStatement.setString(1, id); 424 425 ResultSet rs=prepareStatement.executeQuery(); 426 while(rs.next()) { 427 String name=rs.getString("name"); 428 String sex=rs.getString("sex"); 429 String profession=rs.getString("profession"); 430 String grade=rs.getString("grade"); 431 String birthday=rs.getString("birthday"); 432 433 t2.setText(name); 434 if(sex.trim().equals("男")) 435 jr1.setSelected(true); 436 else 437 jr2.setSelected(true); 438 t3.setText(profession); 439 t4.setText(grade); 440 t5.setText(birthday); 441 } 442 443 rs.close(); 444 stmt.close(); 445 446 }catch(SQLException e1) { 447 e1.printStackTrace(); 448 } 449 } 450 }); 451 452 453 454 455 Box box1=Box.createVerticalBox(); 456 457 Box box11=Box.createHorizontalBox(); 458 box11.add(Box.createHorizontalStrut(80)); 459 box11.add(l1); 460 box11.add(t1); 461 box11.add(b3); 462 box11.add(Box.createHorizontalStrut(80)); 463 464 465 Box box12=Box.createHorizontalBox(); 466 box12.add(Box.createHorizontalStrut(80)); 467 box12.add(l2); 468 box12.add(t2); 469 box12.add(Box.createHorizontalStrut(80)); 470 471 Box box13=Box.createHorizontalBox(); 472 box13.add(Box.createHorizontalStrut(1)); 473 box13.add(l3); 474 box13.add(jr1); 475 box13.add(jr2); 476 box13.add(Box.createHorizontalStrut(80)); 477 478 479 Box box14=Box.createHorizontalBox(); 480 box14.add(Box.createHorizontalStrut(80)); 481 box14.add(l4); 482 box14.add(t3); 483 box14.add(Box.createHorizontalStrut(80)); 484 485 486 Box box15=Box.createHorizontalBox(); 487 box15.add(Box.createHorizontalStrut(80)); 488 box15.add(l5); 489 box15.add(t4); 490 box15.add(Box.createHorizontalStrut(80)); 491 492 Box box16=Box.createHorizontalBox(); 493 box16.add(Box.createHorizontalStrut(80)); 494 box16.add(l6); 495 box16.add(t5); 496 box16.add(Box.createHorizontalStrut(80)); 497 498 box1.add(Box.createVerticalStrut(10)); 499 box1.add(box11); 500 box1.add(box12); 501 box1.add(box13); 502 box1.add(box14); 503 box1.add(box15); 504 box1.add(box16); 505 box1.add(Box.createVerticalStrut(10)); 506 d.setTitle("查询学生信息"); 507 d.add(box1,BorderLayout.CENTER); 508 d.setBounds(550, 300, 500, 250); 509 d.setVisible(true); 510 } 511 }); 512 513 514 515 JMenuItem mi4=new JMenuItem("删除学生基本信息(D)");mi4.setMnemonic(KeyEvent.VK_D); 516 mi4.addActionListener(new ActionListener(){ 517 public void actionPerformed(ActionEvent e){ 518 JDialog d=new JDialog(); 519 JLabel l1=new JLabel("请输入要删除的学号:"); 520 JLabel l2=new JLabel("姓名:"); 521 JLabel l3=new JLabel("性别:"); 522 JLabel l4=new JLabel("专业:"); 523 JLabel l5=new JLabel("年级:"); 524 JLabel l6=new JLabel("出生:"); 525 JTextField t1=new JTextField(15); 526 JTextField t2=new JTextField(15); 527 JTextField t3=new JTextField(15); 528 JTextField t4=new JTextField(15); 529 JTextField t5=new JTextField(15); 530 531 JRadioButton jr1=new JRadioButton("男"); 532 JRadioButton jr2=new JRadioButton("女"); 533 ButtonGroup group=new ButtonGroup(); 534 group.add(jr1); 535 group.add(jr2); 536 537 JButton b3=new JButton("删除"); 538 b3.addActionListener(new ActionListener() { 539 public void actionPerformed(ActionEvent e) { 540 try { 541 String sql1="delete from student2 where id=?"; 542 PreparedStatement prepareStatement2 = con.prepareStatement(sql1); 543 544 String id; 545 id=t1.getText(); 546 prepareStatement2.setString(1, id); 547 prepareStatement2.executeUpdate(); 548 549 JDialog d=new JDialog(); 550 JLabel l1=new JLabel(" 操作完成!"); 551 Font font =new Font("宋体",Font.BOLD,20); 552 l1.setFont(font); 553 l1.setForeground(Color.GREEN); 554 d.add(l1,BorderLayout.CENTER); 555 d.setBounds(650, 400, 200, 100); 556 d.setTitle("温馨提示"); 557 d.setVisible(true); 558 559 560 }catch(SQLException e1) { 561 e1.printStackTrace(); 562 } 563 } 564 }); 565 566 567 Box box1=Box.createVerticalBox(); 568 569 Box box11=Box.createHorizontalBox(); 570 box11.add(Box.createHorizontalStrut(80)); 571 box11.add(l1); 572 box11.add(t1); 573 box11.add(b3); 574 box11.add(Box.createHorizontalStrut(80)); 575 576 577 Box box12=Box.createHorizontalBox(); 578 box12.add(Box.createHorizontalStrut(80)); 579 box12.add(l2); 580 box12.add(t2); 581 box12.add(Box.createHorizontalStrut(80)); 582 583 Box box13=Box.createHorizontalBox(); 584 box13.add(Box.createHorizontalStrut(1)); 585 box13.add(l3); 586 box13.add(jr1); 587 box13.add(jr2); 588 box13.add(Box.createHorizontalStrut(80)); 589 590 591 Box box14=Box.createHorizontalBox(); 592 box14.add(Box.createHorizontalStrut(80)); 593 box14.add(l4); 594 box14.add(t3); 595 box14.add(Box.createHorizontalStrut(80)); 596 597 598 Box box15=Box.createHorizontalBox(); 599 box15.add(Box.createHorizontalStrut(80)); 600 box15.add(l5); 601 box15.add(t4); 602 box15.add(Box.createHorizontalStrut(80)); 603 604 Box box16=Box.createHorizontalBox(); 605 box16.add(Box.createHorizontalStrut(80)); 606 box16.add(l6); 607 box16.add(t5); 608 box16.add(Box.createHorizontalStrut(80)); 609 610 box1.add(Box.createVerticalStrut(10)); 611 box1.add(box11); 612 box1.add(box12); 613 box1.add(box13); 614 box1.add(box14); 615 box1.add(box15); 616 box1.add(box16); 617 box1.add(Box.createVerticalStrut(10)); 618 d.setTitle("删除学生信息"); 619 d.add(box1,BorderLayout.CENTER); 620 d.setBounds(550, 300, 500, 250); 621 d.setVisible(true); 622 } 623 }); 624 625 626 m.add(mi1); 627 m.addSeparator(); 628 m.add(mi2); 629 m.addSeparator(); 630 m.add(mi3); 631 m.addSeparator(); 632 m.add(mi4); 633 634 635 Font font =new Font("楷体",Font.BOLD,26); 636 l.setFont(font); 637 l.setForeground(Color.MAGENTA); 638 p.add(l,BorderLayout.CENTER); 639 640 f.add(p); 641 f.setTitle("学籍管理系统"); 642 f.setBounds(500, 200, 500, 500); 643 f.setVisible(true); 644 } 645 public static void main(String[] args){ 646 647 StudentTest st1=new StudentTest(); 648 try { 649 Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); 650 System.out.println("成功加载SQL驱动程序!"); 651 }catch(Exception e) { 652 System.out.println("找不到SQL驱动程序!"); 653 } 654 try { 655 Connection con=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=aaa","sa","mingye"); 656 Statement st=con.createStatement(); 657 System.out.println("数据库连接成功!"); 658 }catch(Exception e) { 659 System.out.println("数据库连接失败!"); 660 } 661 662 663 try { 664 Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); 665 }catch (ClassNotFoundException e1) { 666 e1.printStackTrace(); 667 } 668 try { 669 con=DriverManager.getConnection(url,user,password); 670 }catch (SQLException e) { 671 e.printStackTrace(); 672 } 673 674 } 675 }
原文:https://www.cnblogs.com/mingye/p/12070756.html