Java伪界面操作数据库的小实例
生活随笔
收集整理的這篇文章主要介紹了
Java伪界面操作数据库的小实例
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
首先在Mysql中有兩個表fruit和login:
?
?
package com.zuoye;import java.sql.*;import java.util.*;public class Test {public static String uid;public static void main(String[] args)throws Exception{Scanner sc = new Scanner(System.in);System.out.println("用戶名:");uid =sc.nextLine();System.out.println("密碼:");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();boolean ok = rs.next();if(ok){System.out.println("歡迎您:"+rs.getString(2));xuanxiang();}else{System.out.println("用戶名或密碼有誤");}main(args);conn.close();}public static void xuanxiang() throws Exception{Scanner sc = new Scanner(System.in);System.out.println("*******選項*******");System.out.println("1.顯示所有商品");System.out.println("2.添加商品");System.out.println("3.購買商品");System.out.println("4.退出");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 void liebiao() throws Exception{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 void tianjia() throws Exception{Scanner sc =new Scanner(System.in);System.out.println("請輸入編號:");String bh =sc.nextLine();System.out.println("請輸入品種:");String pz =sc.nextLine();System.out.println("請輸入價格:");double jg =Double.parseDouble(sc.nextLine());System.out.println("請輸入產地:");String cd =sc.nextLine();System.out.println("請輸入數量:");int sl=Integer.parseInt(sc.nextLine());System.out.println("請輸入圖片地址:");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 void goumai() throws Exception{Class.forName("com.mysql.jdbc.Driver");Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/mydb?characterEncoding=GBK","root","");Scanner sc = new Scanner(System.in);System.out.println("購買什么水果");String pz = sc.nextLine();System.out.println("買多少");int gs = Integer.parseInt(sc.nextLine());String sql1 ="select * from fruit where Name=?";PreparedStatement p1 = conn.prepareStatement(sql1);p1.setString(1, pz);ResultSet rs2 = p1.executeQuery();double s = 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();} }?
?
用戶名: lisi 密碼: 666666 歡迎您:李四 *******選項******* 1.顯示所有商品 2.添加商品 3.購買商品 4.退出 1 k001 蘋果 2.40 煙臺 87 image/0.gif k002 菠蘿 1.40 廣東 90 image/1.gif k003 桔子 2.40 福州 90 image/2.gif k004 葡萄 2.40 新韁 90 image/3.gif k005 櫻桃 2.40 青島 90 image/4.gif k006 桃子 2.40 花果山 90 image/5.gif k007 香蕉 2.40 濟南 90 image/6.gif *******選項******* 1.顯示所有商品 2.添加商品 3.購買商品 4.退出?
轉載于:https://www.cnblogs.com/claricre/p/6258666.html
總結
以上是生活随笔為你收集整理的Java伪界面操作数据库的小实例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 优盘驱动怎么重新安装 重新安装优盘驱动的
- 下一篇: 电脑启动lol卡怎么办 电脑开LOL卡顿