A simple banking system with Java and Oracle __oracle

Source: Internet
Author: User
1, the design of the banking system of the two Tables
create TABLE T_user (
	accountnum varchar2 () primary key,
	username varchar2 (),
	Password VARCHAR2 (6),
	balance number (8,2),
	datetime date default sysdate
);
CREATE TABLE t_history (
	hid number ()  primary key,
	accountnumber varchar2, Money number
	(8,2),
	content Varchar2 (50),--note time
	date default sysdate
);
Create sequence Seq_account
start with 1001
increment by 1;
Create sequence Seq_hid
start with 1
increment 1;
2. Add foreign KEY constraint to t_history accountnumber
ALTER TABLE t_history add constraints Fk_number foreign key (AccountNumber) References  t_user (AccountNum);
3, the completion of the interface design
package edu.com;

Import java.sql.SQLException;
Import Java.util.Scanner;

public class Testbank {public

	static void Main (string[] args) throws ClassNotFoundException, SQLException {
		
		Syst EM.OUT.PRINTLN ("Agricultural University Software Institute of the Bank System 1.0");
		System.out.println ("0, exit");
		System.out.println ("1, open Account");
		System.out.println ("2, Save Money");
		System.out.println ("3, Take Money");
		System.out.println ("4, transfer");
		while (true)
		{
			System.out.println ("Please enter system options [0-4]:");
			Scanner scan=new Scanner (system.in);
			int Chooess=scan.nextint ();
			Switch (chooess) {case
			0:
				System.out.println ("Bye-bye");
				System.exit (0);
				break;
			Case 1:
				Bank.kaihu ();
				break;
			Case 2:
				Bank.cunqian ();
				break;	
			Case 3:
				Bank.quqian ();
				break;	
			Case 4:
				Bank.zhuanzhang ();
				break;
				Default: Break;}}}

4, to the corresponding function to achieve code package edu.com;
Import Java.util.Scanner;
Import java.sql.*; public class Bank {public static Connection Connection () throws SQLException, classnotfoundexception{class.forname ("O
	      Racle.jdbc.driver.OracleDriver ");
	      Connection conn=drivermanager.getconnection ("Jdbc:oracle:thin: @localhost: 1521:orcl", "Scott", "Tiger");
	Return conn;
	   public static void Kaihu () throws ClassNotFoundException, sqlexception{Connection conn=connection ();
		Scanner scan=new Scanner (system.in);
		System.out.println ("Please enter user name:");
		String Name=scan.next ();
		System.out.println ("Please enter password:");
		String Pwd=scan.next ();
	      String sql= "INSERT into T_user (accountnum,username,password,balance) VALUES (seq_accountnum.nextval,?,?, 100)";
	      PreparedStatement psmt=conn.preparestatement (SQL);
	      Psmt.setstring (1, name);
	      Psmt.setstring (2, PWD);
	      ResultSet Rs=psmt.executequery (); if (Rs.next ()) {System.out.println ("open account is successful.")
			"); else {SYSTEM.OUT.PRINTLN ("Account failure.")
			");
	   } public static void Cunqian () throws ClassNotFoundException, sqlexception{Connection conn=connection ();
	   Scanner scan=new Scanner (system.in);
	   System.out.print ("Please enter account number:");
	   String Num=scan.next ();
	   System.out.print ("Please enter password:");
	   String Pwd=scan.next ();
	   System.out.print ("Please enter Deposit Amount:");
	   float money=scan.nextfloat (); String sql1= "Update t_user set balance=balance+?"
	      where accountnum=? ";
	      
	      PreparedStatement psmt=conn.preparestatement (SQL1);
	      Psmt.setfloat (1, money);
	      Psmt.setstring (2, num);
	     int rs1=psmt.executeupdate (); String sql2= "INSERT into T_history (hid,accountnumber,money,content) VALUES (Seq_hid.nextval," +num+ "," +money+ ", ' deposit ')
	      ";	
	      Psmt=conn.preparestatement (SQL2);
	      ResultSet Rs2=psmt.executequery (); if (rs1>0) {System.out.println ("deposit succeeded.")
			"); else {System.out.println ("deposit failed.")
			"); The public static void Quqian () throws ClassNotFoundException,sqlexception{Connection conn=connection ();
	   Scanner scan=new Scanner (system.in);
	   System.out.print ("Please enter account number:");
	   String Num=scan.next ();
	   System.out.print ("Please enter password:");
	   String Pwd=scan.next ();
	   System.out.print ("Please enter the amount of withdrawal:");
	   float money=scan.nextfloat (); String sql1= "Update t_user set balance=balance-?"
	      where accountnum=? ";
	      
	      PreparedStatement psmt=conn.preparestatement (SQL1);
	      Psmt.setfloat (1, money);
	      Psmt.setstring (2, num);
	     int rs1=psmt.executeupdate (); String sql2= "INSERT into T_history (hid,accountnumber,money,content) VALUES (Seq_hid.nextval," +num+ "," +money+ ", ' withdrawals ')
	      ";	
	      Psmt=conn.preparestatement (SQL2);
	      ResultSet Rs2=psmt.executequery (); if (rs1>0) {System.out.println ("withdraw successfully.")
			"); else {System.out.println ("withdrawal failed.")
			"); The public static void Zhuanzhang () throws ClassNotFoundException, SQLException {Connection conn=connection (
	   ); Scanner scan=new Scanner (system.in);
	   System.out.print ("Please enter account number:");
	   String Num=scan.next ();
	   System.out.print ("Please enter password:");
	   String Pwd=scan.next ();
	   System.out.print ("Please enter the account number:");
	   String No=scan.next ();
	   System.out.print ("Please enter the transfer amount:");
	   float money=scan.nextfloat ();
	   Conn.setautocommit (FALSE);
	   String sql1= "Update t_user set balance=balance-" +money+ "where accountnum=" +num+ "";
	   String sql2= "Update t_user set balance=balance+" +money+ "where accountnum=" +no+ ""; String sql3= "INSERT into T_history (hid,accountnumber,money,content) VALUES (Seq_hid.nextval," +num+ "," +money+ ", ' transfer ')
	   ";
	   PreparedStatement pstmt=conn.preparestatement (SQL1);
	   int result1=pstmt.executeupdate ();
	   Pstmt=conn.preparestatement (SQL2);
	   int result2=pstmt.executeupdate ();	
	      Pstmt=conn.preparestatement (SQL3);
	   ResultSet Rs2=pstmt.executequery (); if (result1>0&&result2>0) {System.out.println ("Transfer succeeded.")
			");
		Conn.commit (); else {System.out.println ("Transfer failed.")
			"); Conn.rollback (); }
   }
}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.