資源簡介
這是用Java編寫的一個簡單的銀行轉賬系統,包括取款,存款,轉賬等功能,其中用到了數據庫的連接,采用Eclipse編寫,包含數據庫的設計文件。非常適合有一定基礎的Java初學者使用。
package com.gujunjia.bank;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.sql.*;
/**
*
* @author gujunjia
*/
public class DataBase
{
static Connection conn;
static PreparedStatement st;
static ResultSet rs;
/**
* 加載驅動
*/
public static void loadDriver()
{
try
{
Class.forName("com.mysql.jdbc.Driver");
}
catch (ClassNotFoundException e)
{
System.out.println("加載驅動失敗");
}
}
/**
* 創建數據庫的連接
*
* @param database
* 需要訪問的數據庫的名字
*/
public static void connectionDatabase(String database)
{
try
{
String url = "jdbc:mysql://localhost:3306/" + database;
String username = "root";
String password = "gujunjia";
conn = DriverManager.getConnection(url, username, password);
}
catch (SQLException e)
{
System.out.println(e.getMessage());
}
}
/**
* 關閉數據庫連接
*/
public static void closeConnection()
{
if (rs != null)
{ // 關閉記錄集
try
{
rs.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
if (st != null)
{ // 關閉聲明
try
{
st.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
if (conn != null)
{ // 關閉連接對象
try
{
conn.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
}
package com.gujunjia.bank;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
* 本類主要實現整個系統的界面
*
* @author gujunjia
*/
public class MainFrame extends JFrame implements ActionListener, FocusListener
{
/**
*
*/
private static final long serialVersionUID = 1L;
public static String userId;
JTextField userIdText;
JPasswordField passwordText;
JButton registerButton;
JButton logInButton;
public MainFrame()
{
super("個人銀行系統

代碼片段和文件信息
package?com.gujunjia.bank;
/*
?*?To?change?this?template?choose?Tools?|?Templates
?*?and?open?the?template?in?the?editor.
?*/
import?java.sql.SQLException;
import?java.util.ArrayList;
import?java.util.List;
import?javax.swing.JOptionPane;
/**
?*?
?*?@author?gujunjia
?*/
public?class?Bank
{
/**
?*?構造方法,用于加載驅動和連接數據庫bank
?*/
public?Bank()
{
Database.loadDriver();
Database.connectionDatabase(“bank“);
}
/**
?*?注冊用戶
?*?
?*?@param?id
?*????????????用戶身份證號
?*?@param?name
?*????????????用戶名
?*?@param?password
?*????????????密碼
?*?@param?amount
?*????????????初始存款金額
?*/
public?void?register(String?id?String?name?String?password?double?amount)
{
try
{
Database.st?=?Database.conn
.prepareStatement(“insert?into?User?values(????)“);
Database.st.setString(1?id);
Database.st.setString(2?name);
Database.st.setString(3?password);
Database.st.setDouble(4?amount);
Database.st.executeUpdate();
}
catch?(SQLException?e)
{
JOptionPane.showMessageDialog(null?e.getMessage());
}
}
/**
?*?
?*?@param?id
?*????????????用戶賬號
?*?@return?返回用戶姓名
?*/
public?String?getUserName(String?id)
{
String?userName?=?““;
try
{
Database.st?=?Database.conn
.prepareStatement(“select?userName?from?User?where?identityCardID=?“);
Database.st.setString(1?id);
Database.rs?=?Database.st.executeQuery();
while?(Database.rs.next())
{
userName?=?Database.rs.getString(1);
}
}
catch?(SQLException?e)
{
JOptionPane.showMessageDialog(null?e.getMessage());
}
return?userName;
}
/**
?*?存款操作
?*?
?*?@param?id
?*????????????需要存款的賬號
?*?@param?money
?*????????????存款金額
?*/
public?void?deposit(String?id?double?money)
{
double?remainingSum?=?getRemainingSum(id);
remainingSum?+=?money;
try
{
Database.st?=?Database.conn
.prepareStatement(“update?User?set?amountOfDeposited=??where?identityCardID=?“);
Database.st.setDouble(1?remainingSum);
Database.st.setString(2?id);
Database.st.executeUpdate();
}
catch?(SQLException?e)
{
JOptionPane.showMessageDialog(null?e.getMessage());
}
}
/**
?*?轉賬操作
?*?
?*?@param?myId
?*????????????我的賬號
?*?@param?money
?*????????????轉賬的金額
?*?@param?desId
?*????????????對方的賬號
?*/
public?void?transfer(String?myId?double?money?String?desId)
{
double?myRemainingSum?=?getRemainingSum(myId);
try
{
myRemainingSum?-=?money;
Database.st?=?Database.conn
.prepareStatement(“update?User?set?amountOfDeposited=??where?identityCardID=??“);
Database.st.setDouble(1?myRemainingSum);
Database.st.setString(2?myId);
Database.st.executeUpdate();
double?desRemainingSum?=?getRemainingSum(desId);
desRemainingSum?+=?money;
Database.st?=?Database.conn
.prepareStatement(“update?User?set?amountOfDeposited=??where?identityCardID=??“);
Database.st.setDoub
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件????????411??2011-03-05?11:16??TheSystemOfBank\.classpath
?????文件????????391??2011-03-05?11:06??TheSystemOfBank\.project
?????文件????????629??2011-03-05?11:06??TheSystemOfBank\.settings\org.eclipse.jdt.core.prefs
?????文件??????20148??2011-03-07?10:22??TheSystemOfBank\bank.jar
?????文件????????975??2011-02-28?10:03??TheSystemOfBank\bank.sql
?????文件???????4544??2011-03-05?17:05??TheSystemOfBank\bin\com\gujunjia\bank\Bank.class
?????文件???????1993??2011-03-05?11:17??TheSystemOfBank\bin\com\gujunjia\bank\Databa
?????文件????????439??2011-03-05?11:17??TheSystemOfBank\bin\com\gujunjia\bank\Main.class
?????文件???????4725??2011-03-05?16:37??TheSystemOfBank\bin\com\gujunjia\bank\Mainfr
?????文件???????5940??2011-03-05?16:52??TheSystemOfBank\bin\com\gujunjia\bank\Register.class
?????文件???????1125??2011-03-05?13:27??TheSystemOfBank\bin\com\gujunjia\bank\User.class
?????文件???????2247??2011-03-05?17:07??TheSystemOfBank\bin\com\gujunjia\bank\UserGUI$1.class
?????文件????????814??2011-03-05?17:07??TheSystemOfBank\bin\com\gujunjia\bank\UserGUI$2.class
?????文件???????1897??2011-03-05?17:07??TheSystemOfBank\bin\com\gujunjia\bank\UserGUI$3.class
?????文件???????2731??2011-03-05?17:07??TheSystemOfBank\bin\com\gujunjia\bank\UserGUI$4.class
?????文件????????815??2011-03-05?17:07??TheSystemOfBank\bin\com\gujunjia\bank\UserGUI$5.class
?????文件???????6500??2011-03-05?17:07??TheSystemOfBank\bin\com\gujunjia\bank\UserGUI.class
?????文件????????838??2011-03-05?16:04??TheSystemOfBank\bin\com\test\Test$1.class
?????文件???????1305??2011-03-05?16:04??TheSystemOfBank\bin\com\test\Test.class
?????文件???????1488??2011-03-06?21:47??TheSystemOfBank\doc\allclasses-fr
?????文件???????1328??2011-03-06?21:47??TheSystemOfBank\doc\allclasses-nofr
?????文件??????15121??2011-03-06?21:47??TheSystemOfBank\doc\com\gujunjia\bank\Bank.html
?????文件???????5773??2011-03-06?21:47??TheSystemOfBank\doc\com\gujunjia\bank\class-use\Bank.html
?????文件???????5813??2011-03-06?21:47??TheSystemOfBank\doc\com\gujunjia\bank\class-use\Databa
?????文件???????5773??2011-03-06?21:47??TheSystemOfBank\doc\com\gujunjia\bank\class-use\Main.html
?????文件???????5823??2011-03-06?21:47??TheSystemOfBank\doc\com\gujunjia\bank\class-use\Mainfr
?????文件???????5813??2011-03-06?21:47??TheSystemOfBank\doc\com\gujunjia\bank\class-use\Register.html
?????文件???????5773??2011-03-06?21:47??TheSystemOfBank\doc\com\gujunjia\bank\class-use\User.html
?????文件???????5803??2011-03-06?21:47??TheSystemOfBank\doc\com\gujunjia\bank\class-use\UserGUI.html
?????文件??????10965??2011-03-06?21:47??TheSystemOfBank\doc\com\gujunjia\bank\Databa
............此處省略71個文件信息
評論
共有 條評論