1 package com.jdbc; 2 3 Import java.sql.Connection; 4 Import Java.sql.DriverManager; 5 Import Java.sql.ResultSet; 6 Import java.sql.Statement; 7 Import Java.util.Scanner; 8 9 public class atm{static Statement sc=null, one static Scanner sca=new Scanner (system.in), Static S Tring username; public static void Main (string[] args) throws Exception {14//The method is to get the operating platform of the database. Getstatement ();
System.out.println ("Welcome to J18 Bank"); SYSTEM.OUT.PRINTLN ("Please login:"); System.out.println ("User name:"); Username=sca.next (); System.out.println ("Password:"); String Password=sca.next (); 22//Call query account method, require incoming user name and password return value of type intnum=queryj18 Int (Username,password); 24//num==1 indicates that there is a corresponding user name and password in the database25
if (num==1) {System.out.println ("login Successful"); 27//Use for Dead loop operation(;;) {System.out.println ("Please select a transaction type:"), System.out.println ("1, save 2, withdraw 3, check the balance"); 31 int Cz=sca.nextint ();//input operation type + if (cz==1) {33cun ();//Call saving method}else if (cz==2) {qu ();//Call to withdraw Money method}else if (cz==3) {PNS query ();//Call query Balance method}else{System.out.println ("Thank you for using!" "); a break; 41} 42} 43} 44
else{System.out.println ("Login failed! "); 46} 47} - /** 49 * Method of taking Money * @throws Exception * *52
public static void Qu () throws exception{System.out.println ("Please enter your withdrawal amount:"); Si double Money=sca.nextdou BLE (); The String sql= "update J18 set money=money-" +money; SYSTEM.OUT.PRINTLN (SQL); =sc.execute boolean A (SQL); 58
if (!a) {System.out.println ("withdrawal succeeded! "); 60} 61} + /** 63 * Save Method * *65
public static void Cun () throws exception{-System.out.println ("Please enter your deposit amount:"); double Mone Y=sca.nextdouble ();//Enter deposit amount69//splicing Modify SQLsql= String "Update J18 set money=money+" +money; 71//Execute SQL statements in the operating platform=sc.execute boolean A (SQL); 73//Determine if the successif (!a) {System.out.println ("Deposit Successful! "); QUERYJ18} + + public static int (String username,string password) throws exception{79//splicing query SQL Note: When stitching, the string needs to be preceded by a ' (single quote)String sql= "SELECT * from J18 where sname= '" +username+ "' and pwd= '" +password+ "'"; 81//Execute query SQL in the platform and place the contents of the query in ResultSet RSResultSet rs=sc.executequery (SQL); 83//Declare a variable initial value of type int 0num=0 int; 85//If there is something worthwhile in the results of the query, go inside the loop(Rs.next ()) {//rs.next () is to determine if there is data in the current position, there is no right to skipnum++; The return num; 90} the / * 92 * Query method * *94
public static void query () throws exception{95//splicing query SQL Note: When stitching, the string needs to be preceded by a ' (single quote)Sql= String "Select money from J18 where sname= '" +username+ "'"; 97//Execute query SQL in the platform and place the contents of the query in ResultSet RS98 ResultSet rs=sc.executequery (SQL); 99//Declare a variable of type double to assign a value of 0Double money=0;101//rs.next () is to determine if there is data in the current position, there is no right to skip102 while (Rs.next ()) {103//Assign the queried data to the money variable104 money=rs.getdouble (1);}106 System.out.println ("Your account Balance:" +money); 107}108109/**110 * Get Database Operation Platform Method 111 * @throws Exception112 */113 public static void Getstatement () throws exception{114//1\ Load DriverClass.forName ("Com.mysql.jdbc.Driver");/**117 * Database Connection URL118 * jdbc:mysql://ip:port/database name 119 * JDBC:MYSQL://LOCALHOST:3306/SCORE120 */121 String url= "JDBC:MYSQL://LOCALHOST:3306/ATM"; 122//Database user name123 String Username= "root"; 124//Database Passwordpassword= String "root"; 126//Use driver to get database connection, need incoming URL username password127 Connection c=drivermanager.getconnection (URL, username, password); 128//Get database operating platform, platform129 sc=c.createstatement (); 130}131}
Java CEO Java Connection MySQL instance (ATM)