日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java伪界面操作数据库的小实例

發布時間:2025/5/22 java 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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伪界面操作数据库的小实例的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。