数据库操作,网络编程及多线程的综合练习~
客户端:
UI层
1 import javax.swing.*; 2 import java.awt.*; 3 import java.awt.event.*; 4 5 public class ATMUI{ 6 7 //主方法 8 public static void main(String [] args){ 9 ATMUI ui = new ATMUI(); 10 ui.initBO(); 11 ui.initFrame(); 12 ui.showLogin(); 13 } 14 15 //初始化业务对象 16 //业务对象,每个用户对于一个业务对象并保存该用户数据 17 //因此,启动程序和每次退出都要创建一个新的业务对象给下一个用户使用 18 19 public ATMBO bo = null; 20 public void initBO(){ 21 bo = new ATMBO(); 22 } 23 24 //初始化主窗口 25 26 //界面宽与高 27 public int width = 960; 28 public int height = 660; 29 30 //界面窗口 31 public JFrame jFrame = null; 32 //层叠容器 33 public JLayeredPane layeredPane = null; 34 35 //背景层 36 public JPanel backLayer = null; 37 //前景层 38 public JPanel frontLayer = null; 39 //前景层布局器 40 public CardLayout cardLayout = null; 41 42 public void initFrame(){ 43 44 //-----初始化窗口与层叠容器------ 45 //创建窗口对象,窗口标题为———— 46 jFrame = new JFrame("ATM触摸屏系统"); 47 48 //创建层叠容器对象 49 layeredPane = new JLayeredPane(); 50 //设置层叠容器大小 51 layeredPane.setPreferredSize(new Dimension(width, height)); 52 53 //把层叠容器添加到窗口中 54 jFrame.add(layeredPane); 55 //设置窗口不能放大缩小 56 jFrame.setResizable(false); 57 //设置窗口大小适应内容(层叠容器)大小 58 jFrame.pack(); 59 //设置窗口居中显示 60 jFrame.setLocationRelativeTo(null); 61 //设置窗口可见(默认不可见) 62 jFrame.setVisible(true); 63 //设置窗口关闭时程序关闭 64 jFrame.addWindowListener(new WindowAdapter(){ 65 public void windowClosing(WindowEvent e){ 66 bo.socclose(); 67 System.exit(0); 68 } 69 }); 70 71 //----在层叠容器中加背景层---- 72 //创建背景层对象 73 backLayer = new JPanel(); 74 //设置背景层布局器FlowLayout的水平间距为0(默认是5) 75 ((FlowLayout)backLayer.getLayout()).setHgap(0); 76 //设置背景层布局器FlowLayout的垂直间距为0(默认是5) 77 ((FlowLayout)backLayer.getLayout()).setVgap(0); 78 79 //设置背景层大小(同窗口) 80 backLayer.setSize(width, height); 81 //设置背景层位置(相对于窗口左上角) 82 backLayer.setLocation(0, 0); 83 84 //创建背景图对象 85 JLabel bg = new JLabel (new ImageIcon("img/bg.jpg")); 86 //在背景层中添加背景图 87 backLayer.add(bg); 88 89 //把背景层添加到层叠容器的较底层 90 layeredPane.add(backLayer, new Integer(0)); 91 92 //-----在层叠容器中增加前景层----- 93 //创建前景层对象 94 frontLayer = new JPanel(); 95 //创建CardLayout布局器对象,水平、垂直间距为0 96 cardLayout = new CardLayout(0, 0); 97 //将前景层的布局器设成CardLayout 98 frontLayer.setLayout(cardLayout); 99 //把前景层的背景色设成透明(这样才能看到背景层) 100 frontLayer.setOpaque(false); 101 //设置前景层大小(同窗口) 102 frontLayer.setSize(width, height); 103 //设置前景层位置(相对于窗口左上角) 104 frontLayer.setLocation(0, 0); 105 106 //把前景层添加到层叠容器的较顶层 107 layeredPane.add(frontLayer, new Integer(1)); 108 109 110 } 111 112 //登录界面 113 //本案例中,前景界面层使用了CardLayout,是希望做到调用对应的方法时,把对应的层面调到最顶层即可;另外,每个层面第一次调用时,则初始化,后面的调用就可以直接把已初始化的层面调出并把些必须的组件重置即可 114 115 //登录层容器 116 public JPanel loginPane = null; 117 //登录卡号输入框 118 public JTextField loginCodeInput = null; 119 //登录密码输入框 120 public JPasswordField loginPassInput = null; 121 //登录提示信息 122 public JLabel loginTipsLabel = null; 123 124 public void showLogin(){ 125 if(loginPane == null){ 126 //-----登录层未初始化时---- 127 128 //创建登录层容器对象 129 loginPane = new JPanel(); 130 //把登录层的背景色设成透明 131 loginPane.setOpaque(false); 132 133 //---往登录层容器中添加组件--- 134 //创建一个垂直BOX容器 135 Box loginBox = Box.createVerticalBox(); 136 //在垂直BoX中添加180高度的距离 137 loginBox.add(Box.createVerticalStrut(170)); 138 139 //创建一个欢迎信息容器 140 JPanel welcome_panel = new JPanel(); 141 //把欢迎信息容器的背景色设成透明 142 welcome_panel.setOpaque(false); 143 //创建欢迎信息”欢迎使用海阁银行” 144 JLabel welcome_label = new JLabel("欢迎使用线上银行"); 145 //设置信息字体 146 welcome_label.setForeground(Color.WHITE); 147 welcome_label.setFont(new Font("华文行楷", Font.PLAIN, 35)); 148 //把欢迎信息添加到欢迎信息容器中 149 welcome_panel.add(welcome_label); 150 //把欢迎信息容器添加到垂直Box容器中 151 loginBox.add(welcome_panel); 152 153 //在垂直BOX中添加30高度的距离 154 loginBox.add(Box.createVerticalStrut(30)); 155 //创津一个卡号输入容器 156 JPanel code_panel = new JPanel(); 157 //把卡号输入容器的背景色设成透明 158 code_panel.setOpaque(false); 159 //创津提示输入卡号信息"请输入卡号:" 160 JLabel code_label = new JLabel("请输入卡号:"); 161 //设置信息字体 162 code_label.setForeground(Color.WHITE); 163 code_label.setFont(new Font("华文仿宋", Font.BOLD, 25)); 164 //把提示输入卡号信息添加到卡号输入容器中 165 code_panel.add(code_label); 166 //创建卡号输入框 167 loginCodeInput = new JTextField(10); 168 //设置卡号输入框字体 169 loginCodeInput.setFont(new Font("华文仿宋", Font.PLAIN, 25)); 170 //把卡号输入框添加到卡号输入容器中 171 code_panel.add(loginCodeInput); 172 //把卡号输入容器添加到垂直BOX容器中 173 loginBox.add(code_panel); 174 175 //在变直的X中添加20高度的距离 176 loginBox.add(Box.createVerticalStrut(20)); 177 178 //类似于卡号输入块,创建密码输入块 179 JPanel pass_panel = new JPanel(); 180 pass_panel.setOpaque(false); 181 JLabel pass_label = new JLabel("请输入密码:"); 182 pass_label.setForeground(Color.WHITE); 183 pass_label.setFont(new Font("华文仿宋",Font.BOLD, 25)); 184 pass_panel.add(pass_label); 185 loginPassInput = new JPasswordField(10); 186 loginPassInput.setFont(new Font("华文仿宋" , Font.PLAIN , 25)); 187 pass_panel.add(loginPassInput); 188 loginBox.add(pass_panel); 189 190 //在垂直BoX中添加孔高度的距离 191 loginBox.add(Box.createVerticalStrut(30)); 192 193 //创建按钮容器 194 JPanel btn_panel = new JPanel(); 195 //把按钮容器的背景色设成透明 196 btn_panel.setOpaque(false); 197 //创建登录按钮并设置字体 198 JButton login_btn = new JButton("登 录"); 199 login_btn.setFont(new Font("微软雅黑", Font.PLAIN, 15)); 200 //把登录按钮添加到按钮容器中 201 btn_panel.add(login_btn); 202 203 //创建重置按钮并设置字体 204 JButton reset_btn = new JButton("重 置"); 205 reset_btn.setFont ( new Font ("微软雅黑", Font.PLAIN , 15)); 206 //把重置按钮添加到按钮容器中 207 btn_panel.add(reset_btn); 208 //把按钮容器添加到垂直BOX容器中 209 loginBox.add(btn_panel); 210 211 //在垂直BoX中添加10高度的距离 212 loginBox.add(Box.createVerticalStrut(10)); 213 214 //创建登录提示信息容器 215 JPanel tips_panel = new JPanel(); 216 //把登录提示信息容器的背景色设成透明 217 tips_panel.setOpaque(false); 218 //创建登录提示信息对象并设置字体(默认无提示文字) 219 loginTipsLabel = new JLabel(""); 220 loginTipsLabel.setForeground(new Color(238,32,32)); 221 loginTipsLabel.setFont(new Font("华文仿宋", Font.BOLD, 20)); 222 //把登录提示信息添加到登录提示信息容器中 223 tips_panel.add(loginTipsLabel); 224 //把登录提示信息容器添加到垂直的boX容器中 225 loginBox.add(tips_panel); 226 //把垂直BoX容器添加到登录层容器中 227 loginPane.add(loginBox); 228 229 //-----显示登录层----- 230 //把登录层添加到前景层容器中 231 frontLayer.add("loginPane", loginPane); 232 //使登录层在前景层容器中置于顶层显示 233 cardLayout.show(frontLayer, "loginPane"); 234 //"刷新"前景层使其可视化 235 frontLayer.validate(); 236 //使卡号输入框获取焦点(方便直接输入) 237 loginCodeInput.requestFocus(); 238 239 //-----监听重置按钮事件----- 240 reset_btn.addActionListener(new ActionListener(){ 241 public void actionPerformed(ActionEvent ae){ 242 //清空卡号输入框、密码输入框和提示信息 243 loginCodeInput.setText(""); 244 loginPassInput.setText(""); 245 loginTipsLabel.setText(""); 246 //使卡号输入框获取焦点 247 loginCodeInput.requestFocus(); 248 } 249 }); 250 251 //-----监听登录按钮事件------ 252 login_btn.addActionListener(new ActionListener(){ 253 public void actionPerformed(ActionEvent ae){ 254 //获取用户输入的卡号和密码 255 String code_str = loginCodeInput.getText(); 256 String pass_str = new String(loginPassInput.getPassword()); 257 //判断输入是否为空,为空则提示 258 if("".equals(code_str)){ 259 loginTipsLabel.setText("卡号不能为空!"); 260 loginCodeInput.requestFocus(); 261 }else if("".equals(pass_str)){ 262 loginTipsLabel.setText("密码不能为空!"); 263 loginPassInput.requestFocus(); 264 }else{ 265 //若卡号和密码输入都不为空调用业务对象的登录业务方法并返回结果 266 int login_rtn = bo.dologin(code_str, Integer.valueOf(pass_str)); 267 if(login_rtn == -2){ 268 //返回-2,表示网络出现异常 269 loginTipsLabel.setText("出现异常!"); 270 }else if(login_rtn == -1){ 271 //返回-1,表示登录成功,显示主菜单界面 272 showMenu(); 273 }else if(login_rtn == 3){ 274 //返回3表示错误已超过3次,显示吞卡提示界面 275 showTunka(); 276 }else{ 277 //返回非―1且非表示错误但未超过3次机会,提示输入错误信息 278 loginCodeInput.setText(""); 279 loginPassInput.setText(""); 280 loginTipsLabel.setText("卡号或密码错误,请重新输入,您还有"+(3-login_rtn)+"次机会!"); 281 loginCodeInput.requestFocus(); 282 } 283 } 284 } 285 }); 286 }else{ 287 //-----登录层已初始化---- 288 //使登录层在前景层容器中置于顶层显示 289 cardLayout.show(frontLayer, "loginPane"); 290 //重置 291 loginCodeInput.setText(""); 292 loginPassInput.setText(""); 293 loginTipsLabel.setText(""); 294 loginCodeInput.requestFocus(); 295 } 296 } 297 298 //吞卡提示界面 299 public JPanel tunkaPane = null; 300 301 public void showTunka(){ 302 if(tunkaPane == null){ 303 //----吞卡界面未初始化前----- 304 tunkaPane = new JPanel(); 305 tunkaPane.setOpaque(false); 306 307 //-----添加组件----- 308 Box tunkaBox = Box.createVerticalBox(); 309 310 tunkaBox.add(Box.createVerticalStrut(180)); 311 312 JPanel tunka_panel = new JPanel(); 313 tunka_panel.setOpaque(false); 314 JLabel tunka_label = new JLabel("您已超过3次输入机会,系统吞卡,请联系客服!"); 315 tunka_label.setForeground(Color.WHITE); 316 tunka_label.setFont(new Font("微软雅黑",Font.PLAIN,25)); 317 tunka_panel.add(tunka_label); 318 tunkaBox.add(tunka_panel); 319 320 tunkaBox.add(Box.createVerticalStrut(30)); 321 322 JPanel btn_panel = new JPanel(); 323 btn_panel.setOpaque(false); 324 JButton tunka_btn = new JButton("确 定"); 325 tunka_btn.setFont(new Font("微软雅黑", Font.PLAIN, 20)); 326 btn_panel.add(tunka_btn); 327 JButton zuobi_btn = new JButton("忘记密码(^-^)"); 328 zuobi_btn.setFont(new Font("微软雅黑", Font.PLAIN, 20)); 329 btn_panel.add(zuobi_btn); 330 tunkaBox.add(btn_panel); 331 332 tunkaPane.add(tunkaBox); 333 334 //吞卡提示界面 335 frontLayer.add("tunkaPane", tunkaPane); 336 cardLayout.show(frontLayer, "tunkaPane"); 337 frontLayer.validate(); 338 339 //监听按钮事件 340 zuobi_btn.addActionListener(new ActionListener(){ 341 public void actionPerformed(ActionEvent ae){ 342 //shownopassword(); 343 } 344 }); 345 346 tunka_btn.addActionListener(new ActionListener(){ 347 public void actionPerformed(ActionEvent ae){ 348 349 quit(); 350 } 351 }); 352 353 }else{ 354 //----吞卡界面已初始化----- 355 cardLayout.show(frontLayer, "tunkaPane"); 356 } 357 } 358 359 //免密码界面 360 /* 361 public JPanel nopasswordPane = null; 362 public JTextField nopinput = null; 363 public JLabel nopl = null; 364 365 public void shownopassword(){ 366 if(nopasswordPane == null){ 367 nopasswordPane = new JPanel(); 368 nopasswordPane.setOpaque(false); 369 370 Box nopasswordbox = Box.createVerticalBox(); 371 nopasswordPane.add(nopasswordbox); 372 373 nopasswordbox.add(Box.createVerticalStrut(200)); 374 375 JPanel nopshu = new JPanel(); 376 nopshu.setOpaque(false); 377 JLabel nopshutips = new JLabel("账号:"); 378 nopshutips.setForeground(Color.WHITE); 379 nopshutips.setFont(new Font("微软雅黑", Font.PLAIN, 20)); 380 nopshu.add(nopshutips); 381 nopinput = new JTextField(10); 382 nopinput.setFont(new Font("华文仿宋", Font.PLAIN, 25)); 383 nopshu.add(nopinput); 384 nopasswordbox.add(nopshu); 385 386 nopasswordbox.add(Box.createVerticalStrut(30)); 387 388 JPanel nopbtn = new JPanel(); 389 nopbtn.setOpaque(false); 390 JButton nopqb = new JButton("确 定"); 391 nopqb.setFont(new Font("华文仿宋", Font.BOLD, 20)); 392 nopbtn.add(nopqb); 393 JButton nopfb = new JButton("返 回"); 394 nopfb.setFont(new Font("华文仿宋", Font.BOLD, 20)); 395 nopbtn.add(nopfb); 396 nopasswordbox.add(nopbtn); 397 398 nopasswordbox.add(Box.createVerticalStrut(30)); 399 400 JPanel noptips = new JPanel(); 401 noptips.setOpaque(false); 402 nopl = new JLabel(); 403 nopl.setForeground(new Color(238,32,32)); 404 nopl.setFont(new Font("华文仿宋", Font.BOLD, 20)); 405 noptips.add(nopl); 406 nopasswordbox.add(noptips); 407 408 //监听 409 nopqb.addActionListener(new ActionListener(){ 410 public void actionPerformed(ActionEvent ae){ 411 String nopget = nopinput.getText(); 412 bo.current_index = -1; 413 int p = -1; 414 bo.dologin(nopget, p); 415 if(bo.current_index == -1){ 416 nopinput.setText(""); 417 nopinput.requestFocus(); 418 nopl.setText("账号输入有误!!"); 419 }else{ 420 showLogin(); 421 loginCodeInput.setText(serverBO.code[bo.current_index]); 422 Integer i = serverBO.password[bo.current_index]; 423 loginPassInput.setText(i.toString()); 424 bo.cs = 0; 425 nopl.setText(""); 426 nopinput.setText(""); 427 nopinput.requestFocus(); 428 } 429 } 430 }); 431 432 nopfb.addActionListener(new ActionListener(){ 433 public void actionPerformed(ActionEvent ae){ 434 nopinput.setText(""); 435 nopl.setText(""); 436 showLogin(); 437 bo.cs = 0; 438 } 439 }); 440 441 //设置界面 442 frontLayer.add("nopasswordPane", nopasswordPane); 443 cardLayout.show(frontLayer, "nopasswordPane"); 444 frontLayer.validate(); 445 nopinput.requestFocus(); 446 }else{ 447 cardLayout.show(frontLayer, "nopasswordPane"); 448 nopinput.requestFocus(); 449 } 450 }*/ 451 452 //主菜单界面 453 public JPanel menuPane = null; 454 455 public void showMenu(){ 456 if(menuPane == null){ 457 //----主菜单界面未初始化前---- 458 menuPane = new JPanel(); 459 menuPane.setOpaque(false); 460 menuPane.setLayout(new BorderLayout()); 461 462 //----添加组件---- 463 //头部信息显示快,位于北区 464 Box tipsBox = Box.createVerticalBox(); 465 menuPane.add(tipsBox, BorderLayout.NORTH); 466 467 tipsBox.add(Box.createVerticalStrut(150)); 468 469 JLabel tips_label = new JLabel("请选择您要的服务"); 470 tips_label.setForeground(Color.WHITE); 471 tips_label.setFont(new Font("华文行楷", Font.PLAIN, 30)); 472 tips_label.setAlignmentX(Component.CENTER_ALIGNMENT); 473 tipsBox.add(tips_label); 474 475 //左栏按钮区,位于西区 476 Box menuLeft = Box.createVerticalBox(); 477 menuPane.add(menuLeft, BorderLayout.WEST); 478 479 menuLeft.add(Box.createVerticalStrut(50)); 480 481 JButton chaxun_btn = new JButton("查询余额"); 482 chaxun_btn.setFont(new Font("华文仿宋", Font.BOLD, 25)); 483 menuLeft.add(chaxun_btn); 484 485 menuLeft.add(Box.createVerticalStrut(100)); 486 487 JButton cunkuan_btn = new JButton("存 款"); 488 cunkuan_btn.setFont(new Font("华文仿宋", Font.BOLD, 25)); 489 menuLeft.add(cunkuan_btn); 490 491 menuLeft.add(Box.createVerticalStrut(100)); 492 493 JButton qukuan_btn = new JButton("取 款"); 494 qukuan_btn.setFont(new Font("华文仿宋", Font.BOLD, 25)); 495 menuLeft.add(qukuan_btn); 496 497 //右栏按钮块,位于东区 498 Box menuRight = Box.createVerticalBox(); 499 menuPane.add(menuRight, BorderLayout.EAST); 500 501 menuRight.add(Box.createVerticalStrut(50)); 502 503 JButton xiugai_btn = new JButton("修改密码"); 504 xiugai_btn.setFont(new Font("华文仿宋", Font.BOLD, 25)); 505 xiugai_btn.setAlignmentX(Component.RIGHT_ALIGNMENT); 506 menuRight.add(xiugai_btn); 507 508 menuRight.add(Box.createVerticalStrut(100)); 509 510 JButton zhuanzhang_btn = new JButton("转 账"); 511 zhuanzhang_btn.setFont(new Font("华文仿宋", Font.BOLD, 25)); 512 zhuanzhang_btn.setAlignmentX(Component.RIGHT_ALIGNMENT); 513 menuRight.add(zhuanzhang_btn); 514 515 menuRight.add(Box.createVerticalStrut(100)); 516 517 JButton quit_btn = new JButton("退 卡"); 518 quit_btn.setFont(new Font("华文仿宋", Font.BOLD, 25)); 519 quit_btn.setAlignmentX(Component.RIGHT_ALIGNMENT); 520 menuRight.add(quit_btn); 521 522 //----显示主菜单界面层---- 523 frontLayer.add("menuPane", menuPane); 524 cardLayout.show(frontLayer, "menuPane"); 525 frontLayer.validate(); 526 527 //监听 528 chaxun_btn.addActionListener(new ActionListener(){ 529 public void actionPerformed(ActionEvent ae){ 530 531 showChaxun(); 532 } 533 }); 534 535 cunkuan_btn.addActionListener(new ActionListener(){ 536 public void actionPerformed(ActionEvent ae){ 537 538 showCunkuan(); 539 } 540 }); 541 542 543 qukuan_btn.addActionListener(new ActionListener(){ 544 public void actionPerformed(ActionEvent ae){ 545 546 showQukuan(); 547 } 548 }); 549 550 xiugai_btn.addActionListener(new ActionListener(){ 551 public void actionPerformed(ActionEvent ae){ 552 553 showXiugai(); 554 } 555 }); 556 557 zhuanzhang_btn.addActionListener(new ActionListener(){ 558 public void actionPerformed(ActionEvent ae){ 559 560 showZhuanzhang(); 561 } 562 }); 563 564 quit_btn.addActionListener(new ActionListener(){ 565 public void actionPerformed(ActionEvent ae){ 566 567 quit(); 568 } 569 }); 570 571 }else{ 572 //----界面初始化后---- 573 cardLayout.show(frontLayer, "menuPane"); 574 } 575 } 576 577 //公共提示界面 578 public JPanel tishiPane = null; 579 public JLabel tishil = null; 580 public void showtishi(){ 581 if(tishiPane == null){ 582 //----未初始化---- 583 tishiPane = new JPanel(); 584 tishiPane.setOpaque(false); 585 586 //-----添加组件---- 587 Box tishibox = Box.createVerticalBox(); 588 tishiPane.add(tishibox); 589 590 tishibox.add(Box.createVerticalStrut(200)); 591 592 //提示语 593 JPanel tishi_p = new JPanel(); 594 tishi_p.setOpaque(false); 595 tishil = new JLabel(); 596 tishil.setForeground(Color.WHITE); 597 tishil.setFont(new Font("微软雅黑", Font.PLAIN, 25)); 598 tishi_p.add(tishil); 599 tishibox.add(tishi_p); 600 601 tishibox.add(Box.createVerticalStrut(30)); 602 603 //按钮 604 JPanel fanghui = new JPanel(); 605 fanghui.setOpaque(false); 606 JButton fhbtn = new JButton("返 回"); 607 fhbtn.setFont(new Font("华文仿宋", Font.BOLD, 20)); 608 fanghui.add(fhbtn); 609 tishibox.add(fanghui); 610 611 //监听 612 fhbtn.addActionListener(new ActionListener(){ 613 public void actionPerformed(ActionEvent ae){ 614 showMenu(); 615 } 616 }); 617 618 //设置界面 619 frontLayer.add("tishiPane", tishiPane); 620 cardLayout.show(frontLayer, "tishiPane"); 621 frontLayer.validate(); 622 623 }else{ 624 cardLayout.show(frontLayer, "tishiPane"); 625 } 626 } 627 628 //查询界面 629 public void showChaxun(){ 630 showtishi(); 631 tishil.setText("您的账户余额是"+bo.dochaxun()+"元"); 632 } 633 634 //存款界面 635 public JPanel cunkuanPane = null; 636 public JTextField cuninput = null; 637 public JLabel cunl = null; 638 639 public void showCunkuan(){ 640 if(cunkuanPane == null){ 641 cunkuanPane = new JPanel(); 642 cunkuanPane.setOpaque(false); 643 644 Box cunkuanbox = Box.createVerticalBox(); 645 cunkuanPane.add(cunkuanbox); 646 647 cunkuanbox.add(Box.createVerticalStrut(200)); 648 649 JPanel cunshu = new JPanel(); 650 cunshu.setOpaque(false); 651 JLabel cunshutips = new JLabel("请输入存款金额:"); 652 cunshutips.setForeground(Color.WHITE); 653 cunshutips.setFont(new Font("微软雅黑", Font.PLAIN, 25)); 654 cunshu.add(cunshutips); 655 cuninput = new JTextField(5); 656 cuninput.setFont(new Font("华文仿宋", Font.PLAIN, 30)); 657 cunshu.add(cuninput); 658 cunkuanbox.add(cunshu); 659 660 cunkuanbox.add(Box.createVerticalStrut(30)); 661 662 JPanel cunbtn = new JPanel(); 663 cunbtn.setOpaque(false); 664 JButton cunqb = new JButton("确 定"); 665 cunqb.setFont(new Font("华文仿宋", Font.BOLD, 20)); 666 cunbtn.add(cunqb); 667 JButton cunfb = new JButton("返 回"); 668 cunfb.setFont(new Font("华文仿宋", Font.BOLD, 20)); 669 cunbtn.add(cunfb); 670 cunkuanbox.add(cunbtn); 671 672 cunkuanbox.add(Box.createVerticalStrut(30)); 673 674 JPanel cuntips = new JPanel(); 675 cuntips.setOpaque(false); 676 cunl = new JLabel(); 677 cunl.setForeground(new Color(238,32,32)); 678 cunl.setFont(new Font("华文仿宋", Font.BOLD, 25)); 679 cuntips.add(cunl); 680 cunkuanbox.add(cuntips); 681 682 //监听 683 cunqb.addActionListener(new ActionListener(){ 684 public void actionPerformed(ActionEvent ae){ 685 String cunget = cuninput.getText(); 686 if("".equals(cunget)){ 687 cunl.setText("输入不能为空!!"); 688 }else{ 689 double cunr = bo.docunkuan(Double.valueOf(cunget)); 690 if(cunr == 1){ 691 cuninput.setText(""); 692 cuninput.requestFocus(); 693 cunl.setText(""); 694 showCunkuanSuccess(); 695 }else{ 696 cunl.setText("您输入的金额不是整百数值,请重新输入!!"); 697 cuninput.setText(""); 698 cuninput.requestFocus(); 699 } 700 } 701 } 702 }); 703 704 cunfb.addActionListener(new ActionListener(){ 705 public void actionPerformed(ActionEvent ae){ 706 cuninput.setText(""); 707 cunl.setText(""); 708 showMenu(); 709 } 710 }); 711 712 //设置界面 713 frontLayer.add("cunkuanPane", cunkuanPane); 714 cardLayout.show(frontLayer, "cunkuanPane"); 715 frontLayer.validate(); 716 cuninput.requestFocus(); 717 }else{ 718 cardLayout.show(frontLayer, "cunkuanPane"); 719 cuninput.requestFocus(); 720 } 721 } 722 723 //存款成功提示界面 724 public void showCunkuanSuccess(){ 725 showtishi(); 726 tishil.setText("存款成功!您的账户余额为"+bo.dochaxun()+"元"); 727 } 728 729 //取款界面 730 public JPanel qukuanPane = null; 731 public JTextField quinput = null; 732 public JLabel qul = null; 733 734 public void showQukuan(){ 735 if(qukuanPane == null){ 736 qukuanPane = new JPanel(); 737 qukuanPane.setOpaque(false); 738 739 Box qukuanbox = Box.createVerticalBox(); 740 qukuanPane.add(qukuanbox); 741 742 qukuanbox.add(Box.createVerticalStrut(200)); 743 744 JPanel qushu = new JPanel(); 745 qushu.setOpaque(false); 746 JLabel qushutips = new JLabel("请输入取款金额:"); 747 qushutips.setForeground(Color.WHITE); 748 qushutips.setFont(new Font("微软雅黑", Font.PLAIN, 25)); 749 qushu.add(qushutips); 750 quinput = new JTextField(5); 751 quinput.setFont(new Font("华文仿宋", Font.PLAIN, 30)); 752 qushu.add(quinput); 753 qukuanbox.add(qushu); 754 755 qukuanbox.add(Box.createVerticalStrut(30)); 756 757 JPanel qubtn = new JPanel(); 758 qubtn.setOpaque(false); 759 JButton quqb = new JButton("确 定"); 760 quqb.setFont(new Font("华文仿宋", Font.BOLD, 20)); 761 qubtn.add(quqb); 762 JButton qufb = new JButton("返 回"); 763 qufb.setFont(new Font("华文仿宋", Font.BOLD, 20)); 764 qubtn.add(qufb); 765 qukuanbox.add(qubtn); 766 767 qukuanbox.add(Box.createVerticalStrut(30)); 768 769 JPanel qutips = new JPanel(); 770 qutips.setOpaque(false); 771 qul = new JLabel(); 772 qul.setForeground(new Color(238,32,32)); 773 qul.setFont(new Font("华文仿宋", Font.BOLD, 25)); 774 qutips.add(qul); 775 qukuanbox.add(qutips); 776 777 //监听 778 quqb.addActionListener(new ActionListener(){ 779 public void actionPerformed(ActionEvent ae){ 780 String quget = quinput.getText(); 781 if("".equals(quget)){ 782 qul.setText("输入不能为空!!"); 783 }else{ 784 int qur = bo.doqukuan(Double.valueOf(quget)); 785 if(qur == 1){ 786 quinput.setText(""); 787 quinput.requestFocus(); 788 qul.setText(""); 789 showQukuanSuccess(); 790 }else{ 791 qul.setText("您的账户余额不足!!"); 792 quinput.setText(""); 793 quinput.requestFocus(); 794 } 795 } 796 } 797 }); 798 799 qufb.addActionListener(new ActionListener(){ 800 public void actionPerformed(ActionEvent ae){ 801 quinput.setText(""); 802 qul.setText(""); 803 showMenu(); 804 } 805 }); 806 807 //设置界面 808 frontLayer.add("qukuanPane", qukuanPane); 809 cardLayout.show(frontLayer, "qukuanPane"); 810 frontLayer.validate(); 811 quinput.requestFocus(); 812 }else{ 813 cardLayout.show(frontLayer, "qukuanPane"); 814 quinput.requestFocus(); 815 } 816 } 817 818 //取款成功提示界面 819 public void showQukuanSuccess(){ 820 showtishi(); 821 tishil.setText("取款成功!您的账户余额为"+bo.dochaxun()+"元"); 822 } 823 824 //修改密码界面 825 public JPanel xiugaiPane = null; 826 public JPasswordField jiuinput = null; 827 public JPasswordField xininput = null; 828 public JPasswordField queinput = null; 829 public JLabel xiul = null; 830 831 public void showXiugai(){ 832 if(xiugaiPane == null){ 833 xiugaiPane = new JPanel(); 834 xiugaiPane.setOpaque(false); 835 836 Box xiugaibox = Box.createVerticalBox(); 837 xiugaiPane.add(xiugaibox); 838 839 xiugaibox.add(Box.createVerticalStrut(180)); 840 841 //旧密码输入 842 JPanel jiushu = new JPanel(); 843 jiushu.setOpaque(false); 844 JLabel jiushutips = new JLabel("请输入旧密码:"); 845 jiushutips.setForeground(Color.WHITE); 846 jiushutips.setFont(new Font("微软雅黑", Font.PLAIN, 20)); 847 jiushu.add(jiushutips); 848 jiuinput = new JPasswordField(10); 849 jiuinput.setFont(new Font("华文仿宋", Font.PLAIN, 25)); 850 jiushu.add(jiuinput); 851 xiugaibox.add(jiushu); 852 853 xiugaibox.add(Box.createVerticalStrut(15)); 854 855 //新密码输入 856 JPanel xinshu = new JPanel(); 857 xinshu.setOpaque(false); 858 JLabel xinshutips = new JLabel("请输入新密码:"); 859 xinshutips.setForeground(Color.WHITE); 860 xinshutips.setFont(new Font("微软雅黑", Font.PLAIN, 20)); 861 xinshu.add(xinshutips); 862 xininput = new JPasswordField(10); 863 xininput.setFont(new Font("华文仿宋", Font.PLAIN, 25)); 864 xinshu.add(xininput); 865 xiugaibox.add(xinshu); 866 867 xiugaibox.add(Box.createVerticalStrut(15)); 868 869 //新密码确定 870 JPanel queshu = new JPanel(); 871 queshu.setOpaque(false); 872 JLabel queshutips = new JLabel("请确认新密码:"); 873 queshutips.setForeground(Color.WHITE); 874 queshutips.setFont(new Font("微软雅黑", Font.PLAIN, 20)); 875 queshu.add(queshutips); 876 queinput = new JPasswordField(10); 877 queinput.setFont(new Font("华文仿宋", Font.PLAIN, 25)); 878 queshu.add(queinput); 879 xiugaibox.add(queshu); 880 881 xiugaibox.add(Box.createVerticalStrut(30)); 882 883 //按钮 884 JPanel xiubtn = new JPanel(); 885 xiubtn.setOpaque(false); 886 JButton xiuqb = new JButton("确 定"); 887 xiuqb.setFont(new Font("华文仿宋", Font.BOLD, 20)); 888 xiubtn.add(xiuqb); 889 JButton xiufb = new JButton("返 回"); 890 xiufb.setFont(new Font("华文仿宋", Font.BOLD, 20)); 891 xiubtn.add(xiufb); 892 xiugaibox.add(xiubtn); 893 894 xiugaibox.add(Box.createVerticalStrut(30)); 895 896 //提示语 897 JPanel xiutips = new JPanel(); 898 xiutips.setOpaque(false); 899 xiul = new JLabel(); 900 xiul.setForeground(new Color(238,32,32)); 901 xiul.setFont(new Font("华文仿宋", Font.BOLD, 25)); 902 xiutips.add(xiul); 903 xiugaibox.add(xiutips); 904 905 //监听 906 xiuqb.addActionListener(new ActionListener(){ 907 public void actionPerformed(ActionEvent ae){ 908 String jiustr = new String(jiuinput.getPassword()); 909 String xinstr = new String(xininput.getPassword()); 910 String questr = new String(queinput.getPassword()); 911 912 if("".equals(jiustr)){ 913 xiul.setText("旧密码不能为空!!"); 914 jiuinput.requestFocus(); 915 }else if("".equals(xinstr)){ 916 xiul.setText("新密码不能为空!!"); 917 xininput.requestFocus(); 918 }else if("".equals(questr)){ 919 xiul.setText("请再次输入新密码!!"); 920 queinput.requestFocus(); 921 }else{ 922 int n = bo.doxiugai(Integer.valueOf(jiustr), Integer.valueOf(xinstr), Integer.valueOf(questr)); 923 924 if(n == -1){ 925 xiul.setText("出现异常!"); 926 }else if(n == 0){ 927 jiuinput.setText(""); 928 xininput.setText(""); 929 queinput.setText(""); 930 xiul.setText("旧密码输入有误!!"); 931 }else if(n == 1){ 932 jiuinput.setText(""); 933 xininput.setText(""); 934 queinput.setText(""); 935 showXiugaiSuccess(); 936 }else{ 937 xininput.setText(""); 938 queinput.setText(""); 939 xiul.setText("两次密码输入不一致!!"); 940 } 941 } 942 } 943 }); 944 945 xiufb.addActionListener(new ActionListener(){ 946 public void actionPerformed(ActionEvent ae){ 947 jiuinput.setText(""); 948 xininput.setText(""); 949 queinput.setText(""); 950 xiul.setText(""); 951 showMenu(); 952 } 953 }); 954 955 //界面设置 956 frontLayer.add("xiugaiPane", xiugaiPane); 957 cardLayout.show(frontLayer, "xiugaiPane"); 958 frontLayer.validate(); 959 jiuinput.requestFocus(); 960 }else{ 961 cardLayout.show(frontLayer, "xiugaiPane"); 962 jiuinput.setText(""); 963 xininput.setText(""); 964 queinput.setText(""); 965 xiul.setText(""); 966 jiuinput.requestFocus(); 967 } 968 } 969 970 //密码修改成功提示界面 971 public JPanel xiusuc = null; 972 973 public void showXiugaiSuccess(){ 974 if(xiusuc == null){ 975 xiusuc = new JPanel(); 976 xiusuc.setOpaque(false); 977 978 //组件 979 Box xiusbox = Box.createVerticalBox(); 980 xiusuc.add(xiusbox); 981 982 xiusbox.add(Box.createVerticalStrut(200)); 983 984 JPanel xiusp = new JPanel(); 985 xiusp.setOpaque(false); 986 JLabel xiustips = new JLabel("修改密码成功!请按确认重新登陆"); 987 xiustips.setForeground(Color.WHITE); 988 xiustips.setFont(new Font("微软雅黑", Font.PLAIN, 25)); 989 xiusp.add(xiustips); 990 xiusbox.add(xiusp); 991 992 xiusbox.add(Box.createVerticalStrut(30)); 993 994 JPanel xiubq = new JPanel(); 995 xiubq.setOpaque(false); 996 JButton xiusbtn = new JButton("确 认"); 997 xiusbtn.setFont(new Font("华文仿宋", Font.BOLD, 20)); 998 xiubq.add(xiusbtn); 999 xiusbox.add(xiubq); 1000 1001 //监听 1002 xiusbtn.addActionListener(new ActionListener(){ 1003 public void actionPerformed(ActionEvent ae){ 1004 bo.cs = 0; 1005 showLogin(); 1006 } 1007 }); 1008 1009 //设置界面 1010 frontLayer.add("xiusuc", xiusuc); 1011 cardLayout.show(frontLayer, "xiusuc"); 1012 frontLayer.validate(); 1013 1014 }else{ 1015 cardLayout.show(frontLayer, "xiusuc"); 1016 } 1017 } 1018 1019 //转账界面 1020 public JPanel zhuanpane = null; 1021 public JTextField kahaoinput = null; 1022 public JTextField jineinput = null; 1023 public JLabel zhuantips = null; 1024 public void showZhuanzhang(){ 1025 if(zhuanpane == null){ 1026 //组件 1027 zhuanpane = new JPanel(); 1028 zhuanpane.setOpaque(false); 1029 1030 Box zhuanbox = Box.createVerticalBox(); 1031 zhuanpane.add(zhuanbox); 1032 1033 zhuanbox.add(Box.createVerticalStrut(200)); 1034 1035 JPanel kahaop = new JPanel(); 1036 kahaop.setOpaque(false); 1037 JLabel kahaot = new JLabel("请输入转账账号:"); 1038 kahaot.setForeground(Color.WHITE); 1039 kahaot.setFont(new Font("微软雅黑", Font.PLAIN, 25)); 1040 kahaoinput = new JTextField(10); 1041 kahaoinput.setFont(new Font("华文仿宋", Font.PLAIN, 30)); 1042 kahaop.add(kahaot); 1043 kahaop.add(kahaoinput); 1044 zhuanbox.add(kahaop); 1045 1046 zhuanbox.add(Box.createVerticalStrut(15)); 1047 1048 JPanel jinep = new JPanel(); 1049 jinep.setOpaque(false); 1050 JLabel jinet = new JLabel("请输入转账金额:"); 1051 jinet.setForeground(Color.WHITE); 1052 jinet.setFont(new Font("微软雅黑", Font.PLAIN, 25)); 1053 jineinput = new JTextField(10); 1054 jineinput.setFont(new Font("华文仿宋", Font.PLAIN, 30)); 1055 jinep.add(jinet); 1056 jinep.add(jineinput); 1057 zhuanbox.add(jinep); 1058 1059 zhuanbox.add(Box.createVerticalStrut(30)); 1060 1061 JPanel zhuanbp = new JPanel(); 1062 zhuanbp.setOpaque(false); 1063 JButton zhuanqbtn = new JButton("确 认"); 1064 zhuanqbtn.setFont(new Font("华文仿宋", Font.BOLD, 20)); 1065 JButton zhuanfbtn = new JButton("返 回"); 1066 zhuanfbtn.setFont(new Font("华文仿宋", Font.BOLD, 20)); 1067 zhuanbp.add(zhuanqbtn); 1068 zhuanbp.add(zhuanfbtn); 1069 zhuanbox.add(zhuanbp); 1070 1071 zhuanbox.add(Box.createVerticalStrut(30)); 1072 1073 JPanel zhuantp = new JPanel(); 1074 zhuantp.setOpaque(false); 1075 zhuantips = new JLabel(); 1076 zhuantips.setForeground(new Color(238,32,32)); 1077 zhuantips.setFont(new Font("华文仿宋", Font.BOLD, 25)); 1078 zhuantp.add(zhuantips); 1079 zhuanbox.add(zhuantp); 1080 1081 //监听 1082 zhuanqbtn.addActionListener(new ActionListener(){ 1083 public void actionPerformed(ActionEvent ae){ 1084 String zh = kahaoinput.getText(); 1085 String je = jineinput.getText(); 1086 if("".equals(zh)){ 1087 zhuantips.setText("转账账户不能为空!"); 1088 kahaoinput.requestFocus(); 1089 }else if("".equals(je)){ 1090 zhuantips.setText("转账金额不能为空!"); 1091 jineinput.requestFocus(); 1092 }else{ 1093 int f = bo.dozhuanzhang(zh, Double.valueOf(je)); 1094 if(f == -1){ 1095 zhuantips.setText("出现异常!"); 1096 }else if(f == 0){ 1097 zhuantips.setText("转账账户不存在!"); 1098 }else if(f == 1){ 1099 zhuantips.setText("您不能为自己转账!"); 1100 }else if(f == 2){ 1101 zhuantips.setText("转账金额超过账户余额!"); 1102 }else{ 1103 showZhuanzhangsuccess(); 1104 } 1105 } 1106 } 1107 }); 1108 1109 zhuanfbtn.addActionListener(new ActionListener(){ 1110 public void actionPerformed(ActionEvent ae){ 1111 showMenu(); 1112 } 1113 }); 1114 1115 //界面 1116 frontLayer.add("zhuan", zhuanpane); 1117 cardLayout.show(frontLayer, "zhuan"); 1118 frontLayer.validate(); 1119 kahaoinput.requestFocus(); 1120 }else{ 1121 cardLayout.show(frontLayer, "zhuan"); 1122 zhuantips.setText(""); 1123 kahaoinput.setText(""); 1124 jineinput.setText(""); 1125 kahaoinput.requestFocus(); 1126 } 1127 } 1128 1129 //转账成功提示界面 1130 public void showZhuanzhangsuccess(){ 1131 showtishi(); 1132 tishil.setText("转款成功!您的账户余额为"+bo.dochaxun()+"元"); 1133 } 1134 1135 //退卡 1136 public void quit(){ 1137 //重新初始化业务对象 1138 initBO(); 1139 //重新显示登录界面 1140 showLogin(); 1141 } 1142 1143 }
业务层
1 import java.io.*; 2 import java.net.*; 3 4 public class ATMBO{ 5 //服务端ip 6 public String ip = "127.0.0.1"; 7 //服务器端口 8 public int port = 5000; 9 //存储当前账号游标 10 public String current_code = null; 11 //储存错误次数 12 public int cs = 0; 13 14 //创建Socket对象 15 Socket socket = null; 16 DataOutputStream output = null; 17 DataInputStream input = null; 18 19 //登陆业务 20 //1.用户输入的卡号和密码作为登录业务方法的两个参数,有前面ATMUI类调用的时候传入 21 //2.登陆业务处理后要“告知”ATMUI类结果,因此设一个int返回值,如果卡号和密码都正确,登陆成功,则返回-1,若果卡号和密码都不正确,则返回错误次数(超过3次吞卡),因此,当前账户登录错误次数也要作为全局变量储存起来。 22 23 24 public int dologin(String code_input, int password_input){ 25 int rtn = -1; 26 try{ 27 //通过服务器ip和端口,创建Socket对象,连接服务器 28 socket = new Socket(ip, port); 29 30 //获取Socket输出流,并包装为DataOutputStream 31 output = new DataOutputStream(socket.getOutputStream()); 32 //拼接要传输的参数 33 String send = "dologin&" +code_input+"&"+password_input; 34 //加密输出 35 encryptWrite(send, output); 36 37 //获取Socket输入流,并包装为DataInputStream 38 input = new DataInputStream(socket.getInputStream()); 39 //获取并解密,接收返回结果 40 String receive = readDecrypt(input); 41 if("-1".equals(receive)){ 42 cs++; 43 rtn = cs; 44 }else{ 45 current_code = receive; 46 rtn = -1; 47 } 48 return rtn; 49 }catch(Exception ex){ 50 ex.printStackTrace(); 51 return -2; 52 } 53 54 } 55 56 //查询业务 57 public double dochaxun(){ 58 double rtn = 0; 59 try{ 60 String send = "dochaxun&"+current_code; 61 encryptWrite(send, output); 62 63 String receive = readDecrypt(input); 64 rtn = Double.valueOf(receive); 65 }catch(Exception ex){ 66 ex.printStackTrace(); 67 } 68 return rtn; 69 } 70 71 //存款业务 72 public double docunkuan(double cunkuan){ 73 double rtn = 0; 74 try{ 75 76 String send = "docunkuan&"+current_code+"&"+cunkuan; 77 encryptWrite(send, output); 78 79 String receive = readDecrypt(input); 80 rtn = Double.valueOf(receive); 81 }catch(Exception ex){ 82 ex.printStackTrace(); 83 } 84 return rtn; 85 } 86 87 //取款业务 88 public int doqukuan(double qukuan){ 89 int rtn = 0; 90 try{ 91 String send = "doqukuan&"+current_code+"&"+qukuan; 92 encryptWrite(send, output); 93 94 String receive = readDecrypt(input); 95 rtn = Integer.valueOf(receive); 96 97 }catch(Exception ex){ 98 ex.printStackTrace(); 99 } 100 return rtn; 101 } 102 103 //密码修改业务 104 public int doxiugai(int jiu, int xin, int que){ 105 int rtn = -1; 106 try{ 107 String send = "doxiugai&"+current_code+"&"+ jiu +"&"+ xin +"&"+ que; 108 encryptWrite(send, output); 109 110 String receive = readDecrypt(input); 111 rtn = Integer.valueOf(receive); 112 113 }catch(Exception ex){ 114 ex.printStackTrace(); 115 } 116 return rtn; 117 } 118 119 //转账业务 120 public int dozhuanzhang(String z, double m){ 121 int rtn = -1; 122 try{ 123 String send = "dozhuanzhang&"+current_code+"&"+ z +"&"+ m; 124 encryptWrite(send, output); 125 126 String receive = readDecrypt(input); 127 rtn = Integer.valueOf(receive); 128 129 }catch(Exception ex){ 130 ex.printStackTrace(); 131 } 132 return rtn; 133 } 134 135 136 //加密输出 137 public void encryptWrite(String src, DataOutputStream output) throws IOException{ 138 //将一个字符串转换成字符数组 139 char[] char_arr = src.toCharArray(); 140 //对每个字符·进行加密操作 141 for(int i=0; i<char_arr.length; i++){ 142 output.writeChar(char_arr[i] + 12); 143 } 144 //在输出内容后加一个特殊符号作为结束符 145 output.writeChar(33333); 146 //刷写内容 147 output.flush(); 148 } 149 150 //读取解密 151 public String readDecrypt(DataInputStream input) throws IOException{ 152 String rtn = ""; 153 154 while(true) { 155 //从输出流中读取一个字符,直到结束符 156 int char_src = input.readChar(); 157 if(char_src != 33333){ 158 rtn = rtn + (char)(char_src - 12); 159 }else{ 160 break; 161 } 162 } 163 return rtn; 164 } 165 166 public void socclose() { 167 try { 168 if(output != null){output.close();} 169 if(input != null){input.close();} 170 if(socket != null){socket.close();} 171 } catch (IOException e) { 172 e.printStackTrace(); 173 } 174 } 175 176 }
服务端:
1 import java.io.*; 2 import java.net.*; 3 import java.sql.*; 4 5 public class serverBO{ 6 7 public static void main(String [] args){ 8 //定义serverSocket,用于监听某个端口,等待客户端连接 9 ServerSocket server_socket = null; 10 11 try{ 12 //创建ServerSocket对象,监听端口5000 13 server_socket = new ServerSocket(5000); 14 System.out.println("ready"); 15 while(true){ 16 //调用ServerSocket的accept方法,等待客户端连接,成功连接之后,返回Socket对象 17 Socket socket = server_socket.accept(); 18 19 new thread(socket).start(); 20 } 21 }catch(Exception ex){ 22 ex.printStackTrace(); 23 }finally{ 24 try{ 25 //关闭ServerSocket 26 if(server_socket != null){ 27 server_socket.close(); 28 } 29 }catch(Exception ex){ 30 ex.printStackTrace(); 31 } 32 } 33 } 34 35 //登陆业务 36 //1.用户输入的卡号和密码作为登录业务方法的两个参数,有前面ATMUI类调用的时候传入 37 //2.登陆业务处理后要“告知”ATMUI类结果,因此设一个int返回值,如果卡号和密码都正确,登陆成功,则返回-1,若果卡号和密码都不正确,则返回错误次数(超过3次吞卡),因此,当前账户登录错误次数也要作为全局变量储存起来。 38 39 public String dologin(String code_input, int password_input){ 40 //定义数据库连接对象 41 Connection conn = null; 42 //定义传输对象statement 43 Statement stmt = null; 44 //定义结果焦对象ResultSet 45 ResultSet rs = null; 46 try{ 47 //加载Driver驱动 48 Class.forName("com.mysql.jdbc.Driver"); 49 //创建数据库连接对象Connection 50 conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bank", "root", "root"); 51 //创建一个向数据库发送命令并返回结果的传送对象statement 52 stmt =conn.createStatement(); 53 54 //定义sql命令,查询符合输入的卡号和密码的数据 55 String sql = "select * from account where code = '"+code_input + "' and password = " + password_input; 56 //将sql命令通过sql传送对象Statement传送到数据库执行,并返回卡号 57 rs = stmt.executeQuery(sql); 58 59 //处理结果集 60 if(rs.next()){ 61 //如果存在符合输入的卡号和密码条件的数据,表示登录成功,返回卡号 62 return code_input; 63 }else{ 64 //如果不存在该数据,表示登录失败,返回“-1” 65 return "-1"; 66 } 67 }catch(Exception e){ 68 e.printStackTrace(); 69 //有异常返回-1 70 return "-1"; 71 }finally{ 72 try{ 73 if(rs != null){ rs.close();} 74 if(stmt != null){stmt.close();} 75 if(conn != null){conn.close();} 76 }catch(Exception e){ 77 e.printStackTrace(); 78 } 79 } 80 81 } 82 83 //查询业务 84 public double dochaxun(String current_code){ 85 double rtn = 0.0; 86 87 //定义数据库连接对象 88 Connection conn = null; 89 //定义传输对象statement 90 Statement stmt = null; 91 //定义结果焦对象ResultSet 92 ResultSet rs = null; 93 94 try{ 95 //加载Driver驱动 96 Class.forName("com.mysql.jdbc.Driver"); 97 //创建数据库连接对象Connection 98 conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bank", "root", "root"); 99 //创建一个向数据库发送命令并返回结果的传送对象statement 100 stmt =conn.createStatement(); 101 102 //定义sql命令,查询对应卡号的余额 103 String sql = "select money from account where code = '"+current_code + "'"; 104 //将sql命令通过sql传送对象Statement传送到数据库执行,并返回结果集 105 rs = stmt.executeQuery(sql); 106 107 //处理结果集 108 if(rs.next()){ 109 //获取余额信息 110 rtn = rs.getDouble("money"); 111 } 112 }catch(Exception e){ 113 e.printStackTrace(); 114 }finally{ 115 try{ 116 if(rs != null){ rs.close();} 117 if(stmt != null){stmt.close();} 118 if(conn != null){conn.close();} 119 }catch(Exception e){ 120 e.printStackTrace(); 121 } 122 } 123 return rtn; 124 } 125 126 //存款业务 127 public int docunkuan(String current_code, double cunkuan){ 128 double ck = cunkuan; 129 double p = ck/100; 130 int c = (int) p; 131 if(c == p) { 132 //定义数据库连接对象 133 Connection conn = null; 134 //定义传输对象statement 135 Statement stmt = null; 136 137 try{ 138 //加载Driver驱动 139 Class.forName("com.mysql.jdbc.Driver"); 140 //创建数据库连接对象Connection 141 conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bank", "root", "root"); 142 //创建一个向数据库发送命令并返回结果的传送对象statement 143 stmt =conn.createStatement(); 144 145 //定义sql命令,修改对应卡号的余额 146 String sql = "update account set money = money + "+cunkuan+" where code = '" + current_code + "'"; 147 //将sql命令通过sql传送对象Statement传送到数据库执行 148 stmt.executeUpdate(sql); 149 }catch(Exception e){ 150 e.printStackTrace(); 151 }finally{ 152 try{ 153 if(stmt != null){stmt.close();} 154 if(conn != null){conn.close();} 155 }catch(Exception e){ 156 e.printStackTrace(); 157 } 158 } 159 return 1; 160 }else{ 161 return 0; 162 } 163 } 164 165 //取款业务 166 public int doqukuan(String current_code, double qukuan){ 167 double money = dochaxun(current_code); 168 if(qukuan > money){ 169 return 0; 170 }else{ 171 Connection conn = null; 172 Statement stmt = null; 173 174 try{ 175 Class.forName("com.mysql.jdbc.Driver"); 176 conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bank", "root", "root"); 177 stmt =conn.createStatement(); 178 179 String sql = "update account set money = money - "+ qukuan + " where code = '" + current_code + "'"; 180 stmt.executeUpdate(sql); 181 }catch(Exception e){ 182 e.printStackTrace(); 183 }finally{ 184 try{ 185 if(stmt != null){stmt.close();} 186 if(conn != null){conn.close();} 187 }catch(Exception e){ 188 e.printStackTrace(); 189 } 190 } 191 return 1; 192 } 193 } 194 195 //密码修改业务 196 public int doxiugai(String current_code, int jiu, int xin, int que){ 197 Connection conn = null; 198 Statement stmt = null; 199 ResultSet rs = null; 200 try{ 201 Class.forName("com.mysql.jdbc.Driver"); 202 conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bank", "root", "root"); 203 stmt =conn.createStatement(); 204 205 String sql = "select * from account where code = '"+current_code + "'"; 206 rs = stmt.executeQuery(sql); 207 208 //处理结果集 209 rs.next(); 210 int password = rs.getInt("password"); 211 212 //密码修改 213 if(jiu == password){ 214 if(xin == que){ 215 String sql1 = "update account set password = " + xin + " where code = '"+current_code + "'"; 216 stmt.executeUpdate(sql1); 217 return 1; 218 }else{ 219 return 2; 220 } 221 }else{ 222 return 0; 223 } 224 225 }catch(Exception e){ 226 e.printStackTrace(); 227 return 0; 228 }finally{ 229 try{ 230 if(rs != null){ rs.close();} 231 if(stmt != null){stmt.close();} 232 if(conn != null){conn.close();} 233 }catch(Exception e){ 234 e.printStackTrace(); 235 } 236 } 237 } 238 239 //转账业务 240 public int dozhuanzhang(String current_code, String z, double m){ 241 Connection conn = null; 242 Statement stmt = null; 243 ResultSet rs = null; 244 245 try{ 246 Class.forName("com.mysql.jdbc.Driver"); 247 conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bank", "root", "root"); 248 stmt = conn.createStatement(); 249 250 String sql = "select * from account where code = '"+z + "'"; 251 rs = stmt.executeQuery(sql); 252 if(!(rs.next())){ 253 return 0; 254 }else{ 255 String zcode = rs.getString("code"); 256 if(zcode.equals(current_code)){ 257 return 1; 258 } 259 } 260 double money = dochaxun(current_code); 261 if(m > money){ 262 return 2; 263 } 264 265 try{ 266 //设置事务的提交方式为非自动提交 267 conn.setAutoCommit(false); 268 String sql1 = "update account set money = money - "+ m + " where code = '" + current_code + "'"; 269 String sql2 = "update account set money = money + "+ m + " where code = '" + z + "'"; 270 stmt.executeUpdate(sql1); 271 stmt.executeUpdate(sql2); 272 //提交事务: 273 conn.commit(); 274 return 3; 275 }catch(SQLException e){ 276 conn.rollback(); 277 e.printStackTrace(); 278 return -1; 279 }finally{ 280 conn.setAutoCommit(true); 281 } 282 }catch(Exception e){ 283 e.printStackTrace(); 284 return -1; 285 }finally{ 286 try{ 287 if(rs != null){ rs.close();} 288 if(stmt != null){stmt.close();} 289 if(conn != null){conn.close();} 290 }catch(Exception e){ 291 e.printStackTrace(); 292 } 293 } 294 } 295 296 //加密输出 297 public void encryptWrite(String src, DataOutputStream output) throws IOException{ 298 //将一个字符串转换成字符数组 299 char[] char_arr = src.toCharArray(); 300 //对每个字符·进行加密操作 301 for(int i=0; i<char_arr.length; i++){ 302 output.writeChar(char_arr[i] + 12); 303 } 304 //在输出内容后加一个特殊符号作为结束符 305 output.writeChar(33333); 306 //刷写内容 307 output.flush(); 308 } 309 310 //读取解密 311 public String readDecrypt(DataInputStream input) throws IOException{ 312 String rtn = ""; 313 314 while(true) { 315 //从输出流中读取一个字符,直到结束符 316 int char_src = input.readChar(); 317 if(char_src != 33333){ 318 rtn = rtn + (char)(char_src - 12); 319 }else{ 320 break; 321 } 322 } 323 return rtn; 324 } 325 326 } 327 328 //线程 329 class thread extends Thread{ 330 serverBO bo = new serverBO(); 331 332 Socket socket = null; 333 DataInputStream input; 334 DataOutputStream output; 335 public thread(Socket s){ 336 socket = s; 337 try { 338 input = new DataInputStream(socket.getInputStream()); 339 output = new DataOutputStream(socket.getOutputStream()); 340 } catch (IOException e) { 341 e.printStackTrace(); 342 } 343 } 344 345 public void run(){ 346 try { 347 while (true) { 348 //读取并解密 349 String receive = bo.readDecrypt(input); 350 //现在从客户端传输过来的数据格式为“参数1&参数2&参数3...”,因此要进行解析 351 //其中第一个参数是指要调用的业务方法 352 String[] param = receive.split("&"); 353 if ("dologin".equals(param[0])) { 354 //登陆业务:第二个参数是用户输入的卡号;第三个是密码 355 String send = bo.dologin(param[1], Integer.valueOf(param[2])); 356 //把业务处理结果加密并输出返回 357 bo.encryptWrite(String.valueOf(send), output); 358 } else if ("dochaxun".equals(param[0])) { 359 //查询业务:第2个参数是当前账号的游标 360 double send = bo.dochaxun(param[1]); 361 //把业务处理结果加密并输出返回 362 bo.encryptWrite(String.valueOf(send), output); 363 } else if ("docunkuan".equals(param[0])) { 364 //存款业务:第二个参数是当前账号的游标,第三个是用户输入的金额 365 int send = bo.docunkuan(param[1], 366 Double.valueOf(param[2])); 367 bo.encryptWrite(String.valueOf(send), output); 368 } else if ("doqukuan".equals(param[0])) { 369 //取款业务 370 int send = bo.doqukuan(param[1], 371 Double.valueOf(param[2])); 372 bo.encryptWrite(String.valueOf(send), output); 373 } else if ("dozhuanzhang".equals(param[0])) { 374 //转帐业务 375 int send = bo.dozhuanzhang(param[1], 376 param[2], Double.valueOf(param[3])); 377 bo.encryptWrite(String.valueOf(send), output); 378 } else if ("doxiugai".equals(param[0])) { 379 //密码修改业务 380 int send = bo.doxiugai(param[1], 381 Integer.valueOf(param[2]), 382 Integer.valueOf(param[3]), 383 Integer.valueOf(param[4])); 384 bo.encryptWrite(String.valueOf(send), output); 385 } 386 } 387 } catch (EOFException e) { 388 System.out.println("线程关闭"); 389 } catch (SocketException e) { 390 System.out.println("socket关闭,线程结束"); 391 } catch (IOException e) { 392 e.printStackTrace(); 393 } finally{ 394 try { 395 if(input != null){ input.close();} 396 if(output != null){ output.close();} 397 if(socket != null){ socket.close();} 398 } catch (IOException e) { 399 e.printStackTrace(); 400 } 401 } 402 } 403 }