A small instance of Java pseudo-interface operation database

Source: Internet
Author: User

First there are two tables fruit and login in MySQL:

 PackageCom.zuoye;ImportJava.sql.*;ImportJava.util.*; Public classTest { Public StaticString uid;  Public Static voidMain (string[] args)throwsexception{Scanner SC=NewScanner (system.in); System.out.println ("User name:"); UID=Sc.nextline (); System.out.println ("Password:"); String pwd=Sc.nextline (); Class.forName ("Com.mysql.jdbc.Driver"); Connection Conn= Drivermanager.getconnection ("Jdbc:mysql://127.0.0.1:3306/mydb?charactereconding=gbk", "Root", "" "); String SQL= "SELECT * from login where username=?" and password=? "; PreparedStatement P=conn.preparestatement (SQL); P.setstring (1, UID); P.setstring (2, PWD); ResultSet RS=P.executequery (); BooleanOK =Rs.next (); if(OK) {System.out.println ("Welcome:" +rs.getstring (2));                    Xuanxiang (); }        Else{System.out.println ("User name or password is incorrect");                } main (args);            Conn.close (); }     Public Static voidXuanxiang ()throwsexception{Scanner SC=NewScanner (system.in); System.out.println ("******* option *******"); System.out.println ("1. Show All Items"); System.out.println ("2. Add Item"); System.out.println ("3. Purchase Goods"); System.out.println ("4. Exit"); String a=Sc.nextline (); if(A.equals ("1") ) {Liebiao ();                                Xuanxiang (); }        Else if(A.equals ("2") ) {Tianjia ();        Xuanxiang (); }        Else if(A.equals ("3") ) {Goumai ();                    Xuanxiang (); }        Else{            return; }                            }         Public Static voidLiebiao ()throwsexception{Class.forName ("Com.mysql.jdbc.Driver"); Connection conn1= Drivermanager.getconnection ("Jdbc:mysql://127.0.0.1:3306/mydb?characterencoding=gbk", "Root", "" "); String SQL1= "SELECT * FROM Fruit"; Statement state1=conn1.createstatement (); ResultSet rs1=state1.executequery (SQL1);  while(Rs1.next ()) {System.out.print (rs1.getstring (1) + "\ T"); System.out.print (Rs1.getstring (2) + "\ T"); System.out.print (Rs1.getstring (3) + "\ T"); System.out.print (Rs1.getstring (4) + "\ T"); System.out.print (Rs1.getstring (5) + "\ T"); System.out.print (Rs1.getstring (6) + "\ n");    } conn1.close (); }     Public Static voidTianjia ()throwsexception{Scanner SC=NewScanner (system.in); System.out.println ("Please enter the number:"); String BH=Sc.nextline (); System.out.println ("Please enter the symbol:"); String PZ=Sc.nextline (); System.out.println ("Please enter the price:"); DoubleJG =double.parsedouble (Sc.nextline ()); System.out.println ("Please enter the origin:"); String CD=Sc.nextline (); System.out.println ("Please Enter quantity:"); intSl=Integer.parseint (Sc.nextline ()); System.out.println ("Please enter the image address:"); String DZ=Sc.nextline (); Class.forName ("Com.mysql.jdbc.Driver"); Connection Conn= Drivermanager.getconnection ("Jdbc:mysql://127.0.0.1:3306/mydb?charactereconding=gbk", "Root", "" "); String SQL= "INSERT into fruit values (?,?,?,?,?,?)"; PreparedStatement P=conn.preparestatement (SQL); P.setstring (1, BH); P.setstring (2, PZ); P.setdouble (3, JG); P.setstring (4, CD); P.setint (5, SL); P.setstring (6, DZ);                P.executeupdate ();    Conn.close (); }     Public Static voidGoumai ()throwsexception{Class.forName ("Com.mysql.jdbc.Driver"); Connection Conn= Drivermanager.getconnection ("Jdbc:mysql://127.0.0.1:3306/mydb?characterencoding=gbk", "Root", "" "); Scanner SC=NewScanner (system.in); System.out.println ("What Fruit to buy"); String PZ=Sc.nextline (); System.out.println ("How much to buy"); intGS =Integer.parseint (Sc.nextline ()); String SQL1= "SELECT * from fruit where name=?"; PreparedStatement P1=conn.preparestatement (SQL1); P1.setstring (1, PZ); ResultSet rs2=P1.executequery (); Doubles = 0;  while(Rs2.next ()) {s= (rs2.getdouble (3) *GS);        System.out.println (s); } String sql2= "Update fruit set Numbers = numbers-?" where Name =? "; PreparedStatement P2=conn.preparestatement (SQL2); P2.setint (1, GS); P2.setstring (2, PZ);                        P2.executeupdate (); String Sql3= "Update login Set account = account-?" where UserName =? "; PreparedStatement P3=conn.preparestatement (SQL3); P3.setdouble (1, s); P3.setstring (2, UID);                P3.executeupdate ();    Conn.close (); }}

User name: Lisi Password:666666welcome: John DoeOption *******1. Show All Products2. Add Item3. Purchase goods4. Exit1k001 Apples2.40 Yantai image/0. gifk002 Pineapple1.40 Guangdong IMAGE/1. gifk003 oranges2.40 Fuzhou IMAGE/2. gifk004 Grapes2.40 New Reins IMAGE/3. gifk005 Cherry2.40 Qingdao IMAGE/4. gifk006 Peach2.40 Huaguoshan IMAGE/5. gifk007 Banana2.40 Jinan IMAGE/6. gifOption *******1. Show All Products2. Add Item3. Purchase goods4. Exit

A small instance of Java pseudo-interface operation database

Related Article

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.