首页 新闻 搜索 专区 学院

心累,都添加到数据库了居然显示添加失败

0
[已解决问题] 解决于 2016-04-20 12:25
package per.sww.page;

import per.sww.tools.ScannerChoice;

public class MainPage extends ScannerChoice{
    public static void main(String[] args) {
        MainPage.mianPage();
    }
    
    /**
     * No.1
     * 实现登录界面
     * 且实现退出功能
     */
    public static void mianPage(){
        System.out.println("---------------------------\n");
        System.out.println("\t 1.商品维护\n");
        System.out.println("\t 2.前台收银\n");
        System.out.println("\t 3.商品管理\n");
        System.out.println("\t 0.退出程序\n");
        System.out.println("---------------------------\n");
        System.out.println("\n请输入选项:");
        do{
            //MainPage继承了ScannerChoice,在此处调用Scanner的ScannerInfoString方法
            String choice = ScannerInfoString();
            //创建一个正则验证输入
            String regex = "[0-3]";
            if(choice.matches(regex)){
                int info = Integer.parseInt(choice);
                switch (info) {
                case 0:
                    System.out.println("---------------------------\n");
                    System.out.println("您已退出!");
                    System.exit(1);
                    break;
                case 1:
                    MaintenancePage();
                    break;
                case 2:
                    checkstandLogPage();
                    break;
                case 3:
                    commodityManagementPage();
                    break;
                default:
                    break;
                }
            }
            System.out.println("输入有误请重新输入:");
        }while(true);
    }
    /**
     * No.4
     * 主页面中[商品管理]选项类
     */
    private static void commodityManagementPage() {
        System.out.println("---------------------------\n");
        System.out.println("\t 1.售货员管理\n");
        System.out.println("\t 2.列出当日卖出列表\n");
        System.out.println("---------------------------\n");
        System.out.println("\n请输入选项,或者按 0 返回上一级菜单.");
    }
    /**
     * No.3
     * 主页面中[前台收银]选项类
     */
    private static void checkstandLogPage() {
        System.out.println("---------------------------\n");
        System.out.println("■■■■■■■■■■■■■■■■■欢迎购物■■■■■■■■■■■■■■■■■");
        System.out.println("\t 1.登录系统\n");
        System.out.println("\t 2.退出\n");
        System.out.println("---------------------------\n");
    }

    /**
     * No.2
     * 主页面中[商品维护]选项类
     */
    public static void MaintenancePage(){
        System.out.println("---------------------------\n");
        System.out.println("\t 0.退出程序\n");
        System.out.println("\t 1.添加商品\n");
        System.out.println("\t 2.更改商品\n");
        System.out.println("\t 3.删除商品\n");
        System.out.println("\t 4.查询商品\n");
        System.out.println("\t 5.显示所有商品\n");
        System.out.println("---------------------------\n");
        System.out.println("\n请输入选项,或者按 0 返回上一级菜单.");
        do{
            String choice = ScannerInfoString();
            String regex = "[0-3]";
            if(choice.matches(regex)){
                int info = Integer.parseInt(choice);
                switch (info) {
                case 0:
                    mianPage();
                    System.exit(1);
                    break;
                case 1:
                    GoodsPage.addGoodsPage();
                    break;
                case 2:
                    checkstandLogPage();
                    break;
                case 3:
                    commodityManagementPage();
                    break;
                case 4:
                    commodityManagementPage();
                    break;
                case 5:
                    commodityManagementPage();
                    break;
                default:
                    break;
                }
            }
            System.out.println("输入有误请重新输入:");
        }while(true);
    }
}

 

package per.sww.tools;

import java.util.Scanner;

import per.sww.entity.Goods;
import per.sww.page.GoodsPage;
import sun.management.OperatingSystemImpl;

public class ScannerChoice {
    /**
     * 获取键盘录入
     */
    public static String ScannerInfoString(){
        Scanner scanner=new Scanner(System.in);
        System.out.print("请输入:");
        return scanner.next();
    }
    
    
    /**
     * 键盘录入价格
     * @return
     */
    public static double ScannerInfo(){
        double num=0.00;
        do{
            Scanner sc=new Scanner(System.in);
            System.out.println("小数点后两位");
            String info=sc.next();
            //验证小数的正则
            String regex="(([1-9][0-9]*)\\.([0-9]{2}))|[0]\\.([0-9]{2})";
            if(info.matches(regex)){
                num=Double.parseDouble(info);
            }else{
                System.out.println("输入有误!");
                continue;
            }
            break;
        }while(true);
        return num;
    }
    
    public static int ScannerNum(){
        int num=0;
        String regex="([1-9])|([1-9][0-9]+)";
        do{
            Scanner sc=new Scanner(System.in);
            System.out.println("请输入:");
            String nums=sc.next();
            if(nums.matches(regex)){
                num=Integer.parseInt(nums);
            }else{
                System.out.println("输入有误请重新输入:");
                continue;
            }
            break;
        }while(true);
        return num;
    }
    
//    public static void changedInfoNext(String conn){
//        do{
//            System.out.println("是否继续进行-当前操作:(Y/N)");
//            String chioe=ScannerChoice.ScannerInfoString();
//            if("y".equals(chioe) || "Y".equals(chioe)){
//                    
//                }
//        }while(true);
//    }
}

 

package per.sww.entity;

public final class SalesMan {
    private int sId;
    private String sName;
    private String sPassword;
    
    public SalesMan(int sId,String sName){
        this.sId=sId;
        this.sName=sName;
    }
    
    public SalesMan(int sId,String sName,String sPassword){
        this.sId=sId;
        this.sName=sName;
        this.sPassword=sName;
    }
    
    public SalesMan(String sName,String sPassword){
        this.sName=sName;
        this.sPassword=sPassword;
    }
    
    
    //封装
    public int getsId() {
        return sId;
    }
    public void setsId(int sId) {
        this.sId = sId;
    }
    public String getsName() {
        return sName;
    }
    public void setsName(String sName) {
        this.sName = sName;
    }
    public String getsPassword() {
        return sPassword;
    }
    public void setsPassword(String sPassword) {
        this.sPassword = sPassword;
    }
}

 

package per.sww.entity;

public final class Goods {
    private int gid;
    private String gname;
    private double gprice;
    private int gnum;
    /**
     * 添加商品信息
     * 
     * @param gname
     *            ,gprice,gum
     */
    public Goods(String gname,double gprice,int gnum){
        this.gname=gname;
        this.gprice=gprice;
        this.gnum=gnum;
    }
    /**
     * 展示所有商品
     * 
     * @param gid
     *            ,gname,gprice,gum
     */
    public Goods(int gid,String gname,double gprice,int gnum){
        this.gid=gid;
        this.gname=gname;
        this.gprice=gprice;
        this.gnum=gnum;
    }
    
    
    /**
     * 根据编号更改商品信息
     * 
     * @param gid
     *            ,gum
     */
    public Goods(int gid,String    gname){
        this.gid=gid;
        this.gname=gname;
    }
    
    
    /**
     * 根据编号更改商品信息
     * 
     * @param gid
     *            ,gprice
     */
    public Goods(int gid,int gnum){
        this.gid=gid;
        this.gnum=gnum;
    }
    
    
    public int getGid() {
        return gid;
    }
    public void setGid(int gid) {
        this.gid = gid;
    }
    public String getGname() {
        return gname;
    }
    public void setGname(String gname) {
        this.gname = gname;
    }
    public double getGprice() {
        return gprice;
    }
    public void setGprice(double gprice) {
        this.gprice = gprice;
    }
    public int getGnum() {
        return gnum;
    }
    public void setGnum(int gnum) {
        this.gnum = gnum;
    }
}

 

 

package per.sww.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import per.sww.db.DbClose;
import per.sww.db.DbConn;
import per.sww.entity.Goods;

public final class GoodsDao {
    Connection conn=null;
    PreparedStatement ps=null;
    ResultSet rs=null;
    
    
    /**
     * 1.添加商品到数据库goods表
     * 
     * @param goods
     *            商品对象
     * @return boolean
     */
    public boolean addGoods(Goods goods){
        boolean bool=false;
        conn=DbConn.getconn();
        String sql="INSERT INTO GOODS(GNAME,GPRICE,GNUM) VALUES(?,?,?)";
        
        try{
            ps=conn.prepareStatement(sql);
            ps.setString(1, goods.getGname());
            ps.setDouble(2, goods.getGprice());
            ps.setInt(3, goods.getGnum());
            int rs=ps.executeUpdate();
            
            if(rs>0){
                bool=false;
            }
        }catch(SQLException e){
            e.printStackTrace();
        }finally{
            DbClose.addClose(ps, conn);
        }
        return bool;
        
    }
}

 

 

package per.sww.page;

import per.sww.dao.GoodsDao;
import per.sww.entity.Goods;
import per.sww.tools.ScannerChoice;

public final class GoodsPage extends ScannerChoice{
    
    public static void addGoodsPage(){
        System.out.println("\t正在执行添加商品操作\n");

        System.out.println("\n請輸入添加商品-名称");
        String goodName=ScannerInfoString();
        System.out.println("\n請輸入添加商品-价格");
        double goodPrice=ScannerInfo();
        System.out.println("\n請輸入添加商品-数量");
        int goodNumber=ScannerNum();
        
        Goods goods=new Goods(goodName, goodPrice, goodNumber);
        boolean bool=new GoodsDao().addGoods(goods);
        if(bool){
            System.out.println("\n\t!您已成功添加商品到数据库!");
        } else {
            System.out.println("添加商品失败");
        }
        //changedInfoNext("addGoodsPage");
    }
}
---------------------------

     1.商品维护

     2.前台收银

     3.商品管理

     0.退出程序

---------------------------


请输入选项:
请输入:1
---------------------------

     0.退出程序

     1.添加商品

     2.更改商品

     3.删除商品

     4.查询商品

     5.显示所有商品

---------------------------


请输入选项,或者按 0 返回上一级菜单.
请输入:1
    正在执行添加商品操作


請輸入添加商品-名称
请输入:草莓

請輸入添加商品-价格
小数点后两位
1.11

請輸入添加商品-数量
请输入:
999
添加商品失败
输入有误请重新输入:
请输入:    

数据库中都查出来了,可是它居然跟我讲添加失败,气人!

cocoyoona的主页 cocoyoona | 初学一级 | 园豆:20
提问于:2016-04-19 19:07
< >
分享
最佳答案
0

正常呀,你写的代码也把我气到了,addGoods整个方法我并没有见到有返回true的地方

奖励园豆:5
Rich.T | 老鸟四级 |园豆:3440 | 2016-04-20 09:40

....我笨啊

cocoyoona | 园豆:20 (初学一级) | 2016-04-20 10:24
其他回答(1)
0

...

AnonymouL | 园豆:1769 (小虾三级) | 2016-04-20 11:16
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册