首页 技术 正文
技术 2022年11月23日
0 收藏 720 点赞 2,303 浏览 3938 个字

建立两个表,一个水果表一个用户表。

1.要求输入账号和密码,登陆成功显示欢迎界面,失败提示错误

2.显示选择界面,输入不同的数字,显示不同的内容,,并实现不同的功能,并返回界面

import java.awt.image.Raster;
import java.sql.*;
import java.util.*;public class ZuoYe {
public static String zh; public static void main(String [] args) throws Exception {
Scanner sc=new Scanner(System.in);
System.out.println("请输入账号");
zh=sc.nextLine();
System.out.println("请输入密码");
String mm=sc.nextLine(); Class.forName("com.mysql.jdbc.Driver"); String jdbc="jdbc:mysql://127.0.0.1:3306/mydb";
Connection conn=DriverManager.getConnection(jdbc, "root", "");
String sql="select * from login where UserName=? and Password=? ";
PreparedStatement state= conn.prepareStatement(sql);
state.setString(1, zh);
state.setString(2, mm);
ResultSet re=state.executeQuery(); if(re.next()){
System.out.println("登陆成功!"+re.getString(2)+",欢迎你!");
jm();
}
else{
System.out.println("账号或密码输入错误");
} conn.close();
} public static void jm() 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 xz=sc.nextLine();
if(xz.equals("1")){
xianshi();
jm();
}
else if(xz.equals("2")){
tianjia();
xianshi();
jm();
}
else if(xz.equals("3")){
goumai();
xianshi();
}
else if(xz.equals("4")){
return;
}
else{
System.out.println("输入有误");
}
} public static void xianshi() throws Exception{
Class.forName("com.mysql.jdbc.Driver");
String jdbc="jdbc:mysql://127.0.0.1:3306/mydb";
Connection conn=DriverManager.getConnection(jdbc, "root", "");
String sql="select * from fruit";
PreparedStatement state=conn.prepareStatement(sql);
ResultSet re=state.executeQuery();
System.out.println("*********************************************");
System.out.println("商品编号"+"\t"+"商品名称"+"\t"+"单价"+"\t"+"原产地"+"\t"+"剩余商品数量");
while(re.next()){
System.out.println(re.getString(1)+"\t"+re.getString(2)+"\t"+re.getDouble(3)+"\t"+re.getString(4)+"\t"+re.getInt(5));
}
System.out.println("*********************************************");
conn.close();
} public static void tianjia() throws Exception{
Scanner sc=new Scanner(System.in);
System.out.println("请输入商品属性:商品编号-商品名称-单价-原产地-添加数量,属性之间用-隔开");
String sp=sc.nextLine();
String [] str=sp.split("-");
double qz=Double.parseDouble(str[2]) ;
int sl=Integer.parseInt(str[4]);
Class.forName("com.mysql.jdbc.Driver");
String jdbc="jdbc:mysql://127.0.0.1:3306/mydb?characterEncoding=GBK";
Connection conn=DriverManager.getConnection(jdbc, "root", "");
String sql="insert into fruit values(?,?,?,?,?,NULL)";
PreparedStatement state=conn.prepareStatement(sql);
state.setString(1, str[0]);
state.setString(2, str[1]);
state.setDouble(3, qz);
state.setString(4, str[3]);
state.setInt(5, sl);
state.executeUpdate();
System.out.println("添加成功!!!"); conn.close();
} public static void goumai() throws Exception{
Scanner sc=new Scanner(System.in);
System.out.println("请输入购买的水果种类或代号");
String mz=sc.nextLine();
System.out.println("请输入购买数量");
int sl=Integer.parseInt(sc.nextLine()); Class.forName("com.mysql.jdbc.Driver");
String jdbc="jdbc:mysql://127.0.0.1:3306/mydb?characterEncoding=GBK";
Connection conn=DriverManager.getConnection(jdbc,"root","");
conn.setAutoCommit(false); //查询苹果单价
String sql2="select * from fruit where Name=? or Ids=?";
PreparedStatement state=conn.prepareStatement(sql2);
state.setString(1, mz);
state.setString(2, mz);
ResultSet re=state.executeQuery();
re.next();
double zj=re.getDouble(3)*sl;//总价格 //修改商品数量
String sl_sql="update fruit set Numbers=? where Name=? or Ids=?";
PreparedStatement state_sl=conn.prepareStatement(sl_sql);
state_sl.setInt(1, (re.getInt(5)-sl));
state_sl.setString(2, mz);
state_sl.setString(3, mz);
state_sl.executeUpdate(); //修改用户剩余钱数
String sy="update login set Account=Account-? where UserName=?";
PreparedStatement dstate=conn.prepareStatement(sy);
dstate.setDouble(1, zj);
dstate.setString(2, zh);
dstate.executeUpdate(); //最后查询剩余钱数
String zhye="select * from login where UserName=?";
PreparedStatement state_ye=conn.prepareStatement(zhye);
state_ye.setString(1, zh);
ResultSet re1 =state_ye.executeQuery();
re1.next();
System.out.println("购买成功!!!您当前账户余额为"+re1.getDouble(4)+"元");
conn.commit();
conn.close();
}}

JDBC 练习JDBC 练习JDBC 练习

微信扫一扫

支付宝扫一扫

本文网址:https://www.zhankr.net/140484.html

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:877 阅读:6,234
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:806 阅读:4,053
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:566 阅读:4,858
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:732 阅读:4,790
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:5,881
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:3,572
助力内容变现

将您的收入提升到一个新的水平

点击联系客服

在线时间:8:00-16:00

客服电话

400-888-8888

客服邮箱

ceotheme@ceo.com

扫描二维码

关注微信公众号

扫描二维码

手机访问本站