zoukankan      html  css  js  c++  java
  • JDBC 24homework

    编写程序:

    1. 创建商品信息表Goods(包含编号Code、名称Name、数量Number、单价Price)

    2. 设计程序流程,由用户选择:插入、删除、修改、查询

    程序效果如下:

    1)根据提示输入:1.插入 2.删除 3.修改 4.查询

    2)如果用户选择1. 则提示用户输入商品的编号、名称、数量、单价信息,并将用户输入的数据insert到表中。

    3)如果用户选择2,则表示用户要删除一个商品,提示用户输入商品编号,并根据用户输入的编号删除表中的记录。

    4)自行设计修改的实现方法

    5)自行设计查询的方法

     

      1 package com.xt.java.base24;
      2 
      3 import java.sql.Connection;
      4 import java.sql.DriverManager;
      5 import java.sql.ResultSet;
      6 import java.sql.SQLException;
      7 import java.sql.Statement;
      8 import java.util.Scanner;
      9 
     10 public class UserTest {
     11 
     12     public static void main(String[] args) {
     13         while(true){
     14         Scanner scanner=new Scanner(System.in);
     15         System.out.println("----------------欢迎进入商品管理系统-------------------");
     16         System.out.println("插入----------1");
     17         System.out.println("删除----------2");
     18         System.out.println("修改----------3");
     19         System.out.println("查询----------4");
     20         System.out.println("退出----------0
    
    ");
     21         System.out.println("请选择您要进行的操作:");
     22         int selectNum=scanner.nextInt();
     23         Connection conn=null;
     24         Statement stat=null;
     25         ResultSet rs=null;
     26         String url="jdbc:mysql://localhost:3306/lyxdatabases";
     27         
     28         //加载驱动,通过驱动管理器将Java与数据库建立联系。同时排除异常。
     29         try {
     30             Class.forName("com.mysql.jdbc.Driver");
     31             conn=DriverManager.getConnection(url,"root","1234");
     32             stat=conn.createStatement();
     33         } catch (ClassNotFoundException e) {
     34             e.printStackTrace();
     35         } catch (SQLException e) {
     36             e.printStackTrace();
     37         }
     38         
     39         
     40         switch(selectNum){
     41         case 0:{
     42             System.out.println("退出系统!!!");
     43             System .exit(0);
     44             break;
     45         }
     46         case 1:{
     47             System.out.println("请输入您要插入的商品的编号:");
     48             int gCode=scanner.nextInt();
     49             System.out.println("请输入您要插入的商品的名称:");
     50             String gName=scanner.next();
     51             System.out.println("请输入您要插入的商品的数量:");
     52             int gNumber=scanner.nextInt();
     53             System.out.println("请输入您要插入的商品的价钱:");
     54             double gPrice=scanner.nextDouble();
     55             String sql="insert into goodsTwo values("+gCode+",'"+gName+"',"+gNumber+","+gPrice+")";
     56             //查看sql语句是否输入正确!
     57             System.out.println(sql);
     58             try {
     59                 int affectedRows=stat.executeUpdate(sql);
     60                 if(affectedRows>0){
     61                     System.out.println("商品插入成功!");
     62                 }else{
     63                     System.out.println("商品插入失败,请重新操作。。。。。。");
     64                 }
     65             } catch (SQLException e) {
     66                 // TODO Auto-generated catch block
     67                 e.printStackTrace();
     68             }
     69             try {
     70                 if(rs!=null){
     71                 rs.close();
     72                 }
     73                 if(stat!=null){
     74                     stat.close();
     75                 }
     76                 if(conn!=null){
     77                     conn.close();
     78                 }
     79             }
     80              catch (SQLException e) {
     81                 // TODO Auto-generated catch block
     82                 e.printStackTrace();
     83              
     84             }
     85             break;
     86         }
     87         case 2:{
     88             System.out.println("请输入您要删除的商品的编号:");
     89             int gCode1=scanner.nextInt();
     90             String sql1="delete from goodsTwo where code="+gCode1;
     91             try {
     92                 int affectedRows1=stat.executeUpdate(sql1);
     93                 if(affectedRows1>0){
     94                     System.out.println("商品删除成功!!");
     95                 }else{
     96                     System.out.println("商品删除失败,请重新操作。。。。。。");
     97                 }
     98             } catch (SQLException e) {
     99                 // TODO Auto-generated catch block
    100                 e.printStackTrace();
    101             }
    102             try {
    103                 if(rs!=null){
    104                 rs.close();
    105                 }
    106                 if(stat!=null){
    107                     stat.close();
    108                 }
    109                 if(conn!=null){
    110                     conn.close();
    111                 }
    112             }
    113              catch (SQLException e) {
    114                 // TODO Auto-generated catch block
    115                 e.printStackTrace();
    116              
    117             }
    118             break;
    119         }
    120         case 3:{
    121             while (true){
    122             System.out.println("请输入您要改正的商品的编号:");
    123             int gCode2=scanner.nextInt();
    124             System.out.println("编号----------a");
    125             System.out.println("名称----------b");
    126             System.out.println("数量----------c");
    127             System.out.println("价钱----------d");
    128             System.out.println("操作完成,退出系统----------e");
    129             
    130             System.out.println("----------请选择您要改正的对象:");
    131             String mark=scanner.next();
    132             /**
    133              * 改正编号。。。
    134              */
    135             if(mark.equals("a")){
    136                 System.out.println("请输入你要改正的编号:");
    137                 int code2=scanner.nextInt();
    138                 String sql2="update goodsTwo set code="+code2+" where code="+gCode2;
    139                 //查看sql2语句是否输入正确!
    140                 System.out.println(sql2);
    141                 
    142                 try {
    143                     int affectedRows1=stat.executeUpdate(sql2);
    144                     if(affectedRows1>0){
    145                         System.out.println("商品改正成功!!");
    146                     }else{
    147                         System.out.println("商品改正失败,请重新操作。。。。。。");
    148                     }
    149                 } catch (SQLException e) {
    150                     // TODO Auto-generated catch block
    151                     e.printStackTrace();
    152                 }
    153             
    154             }
    155             
    156             /**
    157              * 改正名称。。。。
    158              */
    159             else if(mark.equals("b")){
    160                 System.out.println("请输入你要改正的名称:");
    161                 String gName=scanner.next();
    162                 String sql3="update goodsTwo set name='"+gName+"' where code="+gCode2;
    163                 //查看sql3语句是否输入正确!
    164                 System.out.println(sql3);
    165                 
    166                 try {
    167                     int affectedRows1=stat.executeUpdate(sql3);
    168                     if(affectedRows1>0){
    169                         System.out.println("商品改正成功!!");
    170                     }else{
    171                         System.out.println("商品改正失败,请重新操作。。。。。。");
    172                     }
    173                 } catch (SQLException e) {
    174                     // TODO Auto-generated catch block
    175                     e.printStackTrace();
    176                 }
    177             
    178             }
    179             /**
    180              * 改正数量。。。。
    181              */
    182             else if(mark.equals("c")){
    183                 System.out.println("请输入你要改正的数量:");
    184                 int gNumber=scanner.nextInt();
    185                 String sql4="update goodsTwo set number="+gNumber+" where code="+gCode2;
    186                 //查看sql4语句是否输入正确!
    187                 System.out.println(sql4);
    188                 
    189                 try {
    190                     int affectedRows1=stat.executeUpdate(sql4);
    191                     if(affectedRows1>0){
    192                         System.out.println("商品改正成功!!");
    193                     }else{
    194                         System.out.println("商品改正失败,请重新操作。。。。。。");
    195                     }
    196                 } catch (SQLException e) {
    197                     // TODO Auto-generated catch block
    198                     e.printStackTrace();
    199                 }
    200                 try {
    201                     if(rs!=null){
    202                     rs.close();
    203                     }
    204                     if(stat!=null){
    205                         stat.close();
    206                     }
    207                     if(conn!=null){
    208                         conn.close();
    209                     }
    210                 }
    211                  catch (SQLException e) {
    212                     // TODO Auto-generated catch block
    213                     e.printStackTrace();
    214                  
    215                 }
    216             
    217             }
    218             /**
    219              * 改正价钱。。。
    220              */
    221             else if(mark.equals("d")){
    222                 System.out.println("请输入你要改正的价钱:");
    223                 double gPrice2=scanner.nextDouble();
    224                 String sql4="update goodsTwo set price="+gPrice2+" where code="+gCode2;
    225                 //查看sql4语句是否输入正确!
    226                 System.out.println(sql4);
    227                 
    228                 try {
    229                     int affectedRows1=stat.executeUpdate(sql4);
    230                     if(affectedRows1>0){
    231                         System.out.println("商品改正成功!!");
    232                     }else{
    233                         System.out.println("商品改正失败,请重新操作。。。。。。");
    234                     }
    235                 } catch (SQLException e) {
    236                     // TODO Auto-generated catch block
    237                     e.printStackTrace();
    238                 }
    239             
    240             }
    241             else if(mark.equals("e")){
    242                 System.out.println("操作完成,退出系统!!");
    243                 return;
    244             }
    245             else{
    246                 System.out.println("您输入的数据有错误,请重新输入!!!");
    247                 break;
    248             }
    249             
    250             
    251         }
    252             
    253         }
    254         case 4:{
    255             System.out.println("请输入您要查询的商品的编号:");
    256             int gCode5=scanner.nextInt();
    257             String sql5="select* from goodsTwo where code="+gCode5;
    258             try {
    259                 rs=stat.executeQuery(sql5);
    260                 while(rs.next()){
    261                 System.out.print("编号:"+rs.getInt("code")+"    ");
    262                 System.out.print("名称:"+rs.getString("name")+"   ");
    263                 System.out.print("数量:"+rs.getInt("number")+"   ");
    264                 System.out.println("价钱:"+rs.getDouble("price"));}
    265             } catch (SQLException e) {
    266                 // TODO Auto-generated catch block
    267                 e.printStackTrace();
    268             }
    269         
    270         
    271         }
    272         
    273         try {
    274             if(rs!=null){
    275             rs.close();
    276             }
    277             if(stat!=null){
    278                 stat.close();
    279             }
    280             if(conn!=null){
    281                 conn.close();
    282             }
    283         }
    284          catch (SQLException e) {
    285             // TODO Auto-generated catch block
    286             e.printStackTrace();
    287          
    288         }
    289         break;
    290         }
    291         
    292         
    293         }
    294         
    295     }
    296 
    297 }
  • 相关阅读:
    python chr函数
    WebStorm新建JS文件、CSS文件时自动生成文件注释
    Electron – 基础学习(2): 项目打包成exe桌面应用 之electron-packager
    Electron – 基础学习(1): 环境安装、创建项目及入门
    Electron – 项目报错整理【打包1】: WARNING: Make sure that .NET Framework 4.5 or later and Powershell 3 or later are installed, otherwise extracting the Electron zip file will hang.
    Vue – 基础学习(5):动态加载和注册组件
    Vue – 基础学习(4):事件修饰符
    Vue – 基础学习(3):$forceUpdate()和$nextTick()的区别
    Vue – 基础学习(2):组件间 通信及参数传递
    PHP_MySQL之间的连接步骤
  • 原文地址:https://www.cnblogs.com/lyxcode/p/7388998.html
Copyright © 2011-2022 走看看