1. 程式人生 > >希望各位一起學習,共同探討,共同努力........

希望各位一起學習,共同探討,共同努力........

原始碼:

db包:

package db;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


public class SqlHelp {


//定義操作資料庫連線
PreparedStatement ps=null;
Connection ct=null;
ResultSet rs=null;

//查詢資料庫的操作
public ResultSet query(String sql,String []paras)
{
try{
//1載入驅動
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
//2得到連線
ct=DriverManager.getConnection
("jdbc:sqlserver://localhost:1433;databaseName=Atm","sa","liujikun");
//3建立ps
ps=ct.prepareStatement(sql);
//給ps的問號(?)賦值
for(int i=0;i<paras.length;i++)
{
ps.setString(i+1,paras[i] );
}
rs=ps.executeQuery();
}catch(Exception e){
e.printStackTrace();
}finally{

}
return rs;
}


//修改,查詢,刪除
public boolean iduExcute(String sql,String []paras)
{
boolean b=true;

try{
//1載入驅動
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
//2得到連線
ct=DriverManager.getConnection
("jdbc:sqlserver://localhost:1433;databaseName=Atm","sa","liujikun");
//3建立ps
ps=ct.prepareStatement(sql);
//給ps的問號(?)賦值
for(int i=0;i<paras.length;i++)
{
ps.setString(i+1,paras[i] );
}
//4執行操作
ps.executeUpdate();
}catch(Exception e){
e.printStackTrace();
}finally{

this.close();
}

return b;
}
//關閉資料庫資源
public void close()
{
try {
if(ps!=null)
{
ps.close();
}
if(ct!=null)
{
ct.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}

model包:

/**

package model;


import java.sql.*;
import java.util.Vector;


import javax.swing.JTextField;
import javax.swing.table.*;


import view.Deposit;


import db.SqlHelp;


public class BankModel {

//取款
Deposit dp;
public boolean isertStu(String uid,String money)
{
boolean b=true;
SqlHelp shl=new SqlHelp();
dp=new Deposit(uid);
String sql="update usersLogin set usemoney=usemoney+ ? where useId= ?";
String []paras={money,uid};
try{
b=shl.iduExcute(sql, paras);
}catch(Exception e){
e.printStackTrace();
}finally{
shl.close();
}
return b;
}


//(通個id號來修改)100元
public boolean updatStu(String uid)
{
boolean b=true;
SqlHelp shl=new SqlHelp();
String sql="update usersLogin  set usemoney=usemoney-'100'  where useId= ?";
String []paras={uid};
try{
b=shl.iduExcute(sql, paras);
}catch(Exception e){
e.printStackTrace();
}finally{
shl.close();
}
return b;
}
//200元
public boolean updatStu1(String uid)
{
boolean b=true;
SqlHelp shl=new SqlHelp();
String sql="update usersLogin  set usemoney=usemoney-'200'  where useId= ?";
String []paras={uid};
try{
b=shl.iduExcute(sql, paras);
}catch(Exception e){
e.printStackTrace();
}finally{
shl.close();
}
return b;
}
//300
public boolean updatStu2(String uid)
{
boolean b=true;
SqlHelp shl=new SqlHelp();
String sql="update usersLogin  set usemoney=usemoney-'300'  where useId= ?";
String []paras={uid};
try{
b=shl.iduExcute(sql, paras);
}catch(Exception e){
e.printStackTrace();
}finally{
shl.close();
}
return b;
}
//500
public boolean updatStu3(String uid)
{
boolean b=true;
SqlHelp shl=new SqlHelp();
String sql="update usersLogin  set usemoney=usemoney-'500'  where useId= ?";
String []paras={uid};
try{
b=shl.iduExcute(sql, paras);
}catch(Exception e){
e.printStackTrace();
}finally{
shl.close();
}
return b;
}
//1000
public boolean updatStu4(String uid)
{
boolean b=true;
SqlHelp shl=new SqlHelp();
String sql="update usersLogin  set usemoney=usemoney-'1000'  where useId= ?";
String []paras={uid};
try{
b=shl.iduExcute(sql, paras);
}catch(Exception e){
e.printStackTrace();
}finally{
shl.close();
}
return b;
}


//查詢
public String checkUser(String uid)
{
String usemoney=null;
SqlHelp sq1=null;
try{
//組織sql語句和引數列表
String sql="select usemoney from usersLogin where useId=?";
String paras[]={uid};
sq1=new SqlHelp();
ResultSet rs=sq1.query(sql, paras);
if(rs.next())
{
//取出錢
usemoney=rs.getString(1);
System.out.println(usemoney);


}
}catch(Exception e){
e.printStackTrace();
}finally{
sq1.close();

}

return usemoney;
}


}

**
 * 使用者登入時的後臺邏輯部分
 * */
package model;


import java.sql.*;
import db.SqlHelp;




public class UseModel {


//
public String checkUser(String uid,String p)
{
String user=null;
SqlHelp sq1=null;
try{
//組織sql語句和引數列表
String sql="select usersLogin.useName from users,usersLogin where users.useId=usersLogin.useId and users.useId=? and users.passwd=?";
String paras[]={uid,p};

sq1=new SqlHelp();
ResultSet rs=sq1.query(sql, paras);
if(rs.next())
{
//取出名字
user=rs.getString(1);
System.out.println(user);
//return user;
}
}catch(Exception e){
e.printStackTrace();
}finally{
sq1.close();

}

return user;
}


}


view包:

/**
 * 查詢頁面
 * */
package view;


import javax.swing.*;


import model.BankModel;


import java.awt.*;
import java.awt.event.*;




public class ChaXun extends JFrame implements ActionListener{
JButton jb;
JTextField jtf;
JPanel jp;
    String uid;
    BankModel bm;
  public ChaXun(String uid){
this.uid=uid;
bm =new BankModel();
//bm.checkUser(uid);
jtf=new JTextField("您的餘額為:"+bm.checkUser(uid));
jb=new JButton("返回");
jb.addActionListener(this);
//System.out.print();
jp=new JPanel(new BorderLayout());
jp.add(jtf,"West");
jp.add(jb,"East");
this.add(jp);
this.setUndecorated(true);
this.setSize(300,200);
int width=Toolkit.getDefaultToolkit().getScreenSize().width;
int height=Toolkit.getDefaultToolkit().getScreenSize().height;
this.setVisible(true);
this.setLocation(width/2-250,height/2-250);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
public ChaXun()
{

}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getActionCommand().equals("返回"))
{
new Window(uid);
this.dispose();
}
}
  
 
}

/**存錢
 * */
package view;


import javax.swing.*;


import model.BankModel;


import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class Deposit extends JFrame implements ActionListener{


/**
* @param args
*/
//public static void main(String[] args) {
//// TODO Auto-generated method stub
//
//Deposit dp=new Deposit();
//}
JLabel jl;
JTextField jtf;
JButton jb1,jb2;
JPanel jp;
String uid;
BankModel bm;
public Deposit(String uid)
{
this.uid=uid;

jp=new JPanel(new BorderLayout());
jl=new JLabel("請放入鈔票:");
jtf=new JTextField();
jb1=new JButton("確定");
jb1.addActionListener(this);
jb2=new JButton("返回");
jb2.addActionListener(this);
jp.add(jl,"West");
jp.add(jtf,"Center");
jp.add(jb1,"East");
jp.add(jb2,"South");
this.add(jp);
this.setSize(300,100);
this.setUndecorated(true);
int width=Toolkit.getDefaultToolkit().getScreenSize().width;
int height=Toolkit.getDefaultToolkit().getScreenSize().height;
this.setVisible(true);
this.setLocation(width/2-250,height/2-250);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==jb2)
{
new Window(uid);
this.dispose();
}else if(e.getSource()==jb1)
{
System.out.print(uid);

bm=new BankModel();
String money=jtf.getText();
bm.isertStu(uid, money);

//bm.isertStu(uid);
//System.out.println(bm.isertStu(uid));
System.out.println("Deposit="+jtf.getText());
JOptionPane.showMessageDialog(this, "存款成功");


}
}


}

/**
 * 使用者登入
 * */
package view;


import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


import javax.swing.*;


import model.UseModel;






public class UserLogin extends JFrame implements ActionListener{


/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub


UserLogin us=new UserLogin();
}
JButton jb1,jb2,jb3,jb4,jb5,jb6,jb7,jb8,jb9,jb10,jb11;
JLabel jl1,jl2,jl3;
JPanel jp,jp1,jp2;
JTextField jName;
JPasswordField jPasswd;
//String jtf="123";
//String jpd="123456";

public UserLogin()
{

jp=new JPanel();

jl1=new JLabel("使用者名稱:");
jName=new JTextField(10);
jName.setBorder(BorderFactory.createLoweredBevelBorder());//設定下凹感覺

jl2=new JLabel("密    碼:");
jPasswd=new JPasswordField(10);
jPasswd.setBorder(BorderFactory.createLoweredBevelBorder());//設定下凹感覺


jb1=new JButton("登入");
jb1.addActionListener(this);
jb2=new JButton("取消");
jb2.addActionListener(this);
jp.add(jl1);
jp.add(jName);
jp.add(jl2);
jp.add(jPasswd);
jp.add(jb1);
jp.add(jb2);
this.add(jp);
this.setUndecorated(true);
this.setVisible(true);
this.setSize(200,300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
int width=Toolkit.getDefaultToolkit().getScreenSize().width;
int height=Toolkit.getDefaultToolkit().getScreenSize().height;
this.setVisible(true);
this.setLocation(width/2-150,height/2-150);

}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==jb1)
{
//取出id和密碼
String uid=this.jName.getText().trim();
String p=new String(this.jPasswd.getPassword());
System.out.println(uid+p);
UseModel um=new UseModel();
String str=um.checkUser(uid,p);
//System.out.println("login="+str);
System.out.println(str.length()!=0);
if(str.length()!=0)
{
System.out.println("通過");
new Window(uid);
this.dispose();
JOptionPane.showMessageDialog(this, "歡迎光臨");

 
}


}else if(e.getSource()==jb2)
{

System.out.print("取消中");
this.dispose();
}
}


}

/**主介面
 * */
package view;
import javax.swing.*;


import model.BankModel;






import java.awt.*;
import java.awt.event.*;


public class Window extends JFrame implements ActionListener{


/**
* @param args
*/
//public static void main(String[] args) {
//// TODO Auto-generated method stub
//
//Window wd=new Window(id);
//}

JButton jb1,jb2,jb3,jb4,jb5;
JLabel jl1,jl2;
JPanel jp,jp1,jp2,jp3;
BankModel bm;
String uid;
public Window(String uid)
{
this.uid=uid;
//Container ct=this.getContentPane();
jp=new JPanel(new GridLayout(3,3));

jb1=new JButton("查詢");
jb1.addActionListener(this);
jb2=new JButton("存款");
jb2.addActionListener(this);
jp1=new JPanel(new BorderLayout());
jp1.add(jb1,"West");
jp1.add(jb2,"East");
jb3=new JButton("取款");
jb3.addActionListener(this);
jb4=new JButton("轉賬");
jb4.addActionListener(this);

jp2=new JPanel(new BorderLayout());
jp2.add(jb3,"West");
jp2.add(jb4,"East");

jp3=new JPanel(new BorderLayout());
jb5=new JButton("取卡");
jb5.addActionListener(this);
jp3.add(jb5,"East");
this.setUndecorated(true);
jp.add(jp1);
jp.add(jp2);
jp.add(jp3);
this.add(jp);
this.setSize(500,400);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
int width=Toolkit.getDefaultToolkit().getScreenSize().width;
int height=Toolkit.getDefaultToolkit().getScreenSize().height;
this.setVisible(true);
this.setLocation(width/2-250,height/2-250);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub

if(e.getActionCommand().equals("查詢"))
{
System.out.println("使用者想查詢");
//因為把對錶的資料封裝到StuModel中,所以我們可以比較簡單的完成查詢任務
//trim()過濾空字串
//String Id=this.jtf.getText().trim();
//寫一個sql語句
System.out.println("查詢編號"+uid);
new ChaXun(uid);
//bm =new BankModel();
//bm.checkUser(uid);
//System.out.println(bm);
//String str=bm.checkUser(uid, p);

}
else if(e.getActionCommand().equals("取卡"))
{
this.dispose();

JOptionPane.showMessageDialog(this, "祝您使用愉快");
new UserLogin();
}else if(e.getActionCommand().equals("取款"))
{
new WithdrawMoney(uid);
System.out.print(uid);
}else if(e.getActionCommand().equals("存款"))
{

new Deposit(uid);
}else if(e.getActionCommand().equals("轉賬"))
{
JOptionPane.showMessageDialog(this, "對不起,該業務已暫停服務");
}
}


}

/**
 * 取錢
 * */
package view;


import javax.swing.*;


import model.BankModel;


import java.awt.*;
import java.awt.event.*;


public class WithdrawMoney extends JFrame implements ActionListener{


/**
* @param args
*/
//public static void main(String[] args) {
//// TODO Auto-generated method stub
//WithdrawMoney wm=new WithdrawMoney(uid);
//}
JButton jb1,jb2,jb3,jb4,jb5,jb6;
JPanel jp,jp1,jp2,jp3;
String uid;
BankModel bm;
public WithdrawMoney(String uid)
{
this.uid=uid;
jp=new JPanel(new GridLayout(3,3));

jb1=new JButton("100");
jb1.addActionListener(this);
jb2=new JButton("200");
jb2.addActionListener(this);
jp1=new JPanel(new BorderLayout());
jp1.add(jb1,"West");
jp1.add(jb2,"East");
jb3=new JButton("300");
jb3.addActionListener(this);
jb4=new JButton("500");
jb4.addActionListener(this);

jp2=new JPanel(new BorderLayout());
jp2.add(jb3,"West");
jp2.add(jb4,"East");

jp3=new JPanel(new BorderLayout());
jb5=new JButton("1000");
jb5.addActionListener(this);
//jb5.addActionListener(this);
jb6=new JButton("返回");
jb6.addActionListener(this);
jp3.add(jb5,"West");
jp3.add(jb6,"East");
this.setUndecorated(true);
jp.add(jp1);
jp.add(jp2);
jp.add(jp3);
this.add(jp);
this.setSize(500,400);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
int width=Toolkit.getDefaultToolkit().getScreenSize().width;
int height=Toolkit.getDefaultToolkit().getScreenSize().height;
this.setVisible(true);
this.setLocation(width/2-250,height/2-250);


}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getActionCommand().equals("返回"))
{
new Window(uid);
this.dispose();
}else if(e.getActionCommand().equals("100"))
{
bm =new BankModel();
//bm.updatStu(uid);
System.out.println(uid);
System.out.print(bm.updatStu(uid));
JOptionPane.showMessageDialog(this, "取款成功");
}else if(e.getActionCommand().equals("200"))
{
bm =new BankModel();
//bm.updatStu(uid);
System.out.println(uid);
System.out.print(bm.updatStu1(uid));
JOptionPane.showMessageDialog(this, "取款成功");
}else if(e.getActionCommand().equals("300"))
{
bm =new BankModel();
//bm.updatStu(uid);
System.out.println(uid);
System.out.print(bm.updatStu2(uid));
JOptionPane.showMessageDialog(this, "取款成功");
}else if(e.getActionCommand().equals("500"))
{
bm =new BankModel();
//bm.updatStu(uid);
System.out.println(uid);
System.out.print(bm.updatStu3(uid));
JOptionPane.showMessageDialog(this, "取款成功");
}else if(e.getActionCommand().equals("1000"))
{
bm =new BankModel();
//bm.updatStu(uid);
System.out.println(uid);
System.out.print(bm.updatStu4(uid));
JOptionPane.showMessageDialog(this, "取款成功");
}
}


}

      本人在寫改程式碼時用了mvc模式,把資料庫的連結單獨封了一個包,把銀行中存錢,查詢,取錢封到了後臺邏輯中model包中,然後其次就是圖形使用者介面有查詢、取款、存款的介面封到了view包中,因為是新手,所以程式碼瑕疵很多,請各位大神多多包含給一定的指導評論,謝謝