数据库实验三代码

import java.util.;
import java.io.
;
import java.sql.*;
import java.util.Scanner;
public class openGaussDemo {

static final String JDBC_DRIVER = "org.postgresql.Driver";
static final String DB_URL = "jdbc:postgresql://124.70.25.192:26000/shiyan1?ApplicationName=app1";
 public static void main(String[] args) {
     Scanner scan1 = new Scanner(System.in);
     Scanner scan2 = new Scanner(System.in);
     Scanner scan3 = new Scanner(System.in);
     System.out.println("用户您好,请输入你的身份代号(1:管理员/2:顾客/3:厨师/4:店主)");
     int shenfen = scan1.nextInt();
     System.out.println("用户您好,请输入您的用户姓名");
     String USER = scan2.nextLine();
     System.out.println("用户您好,请输入你的密码");
     String PASS = scan3.nextLine();
    Connection conn = null;
    Statement stmt = null;
     int n;
     Scanner sc=new Scanner (System.in);
     String a;
     if(shenfen == 1)
     {
         try{
             // 注册 JDBC 驱动
             Class.forName(JDBC_DRIVER);

             // 打开链接
             System.out.println("管理员您好!请稍等,正在连接数据库...");
             conn = DriverManager.getConnection(DB_URL,USER,PASS);

             // 执行查询
             System.out.println("实例化Statement对象...");
             stmt = conn.createStatement();
             String sql;
             String x;
             int flag=0;
             int i;
             int flag1=0;
             //int num=0;
             int choice;
             int choice1;
             int choice2;
             int choice3;
             String info;
             String sql1;
             System.out.println("管理员你好,请选择接下来的操作代码:1.饭店信息管理 2.店主信息管理 3.顾客信息管理 4.退出");
             choice=sc.nextInt();
             if(choice ==1) {
                 System.out.println("********饭店信息管理*********");
                 System.out.println("1.创建饭店信息 2.删除饭店信息 3.修改饭店信息 4. 查询饭店信息");
                 choice1 = sc.nextInt();
                 if (choice1 == 1) {
                     System.out.println("********创建饭店信息********");
                     sql = "INSERT  INTO  restaurant  VALUES ('";
                     System.out.println("请输入饭店ID:");
                     sql = sql + sc.next() + "','";
                     System.out.println("请输入饭店店名:");
                     sql = sql + sc.next() + "','";
                     System.out.println("请输入饭店评价:");
                     sql = sql + sc.next() + "')";
                     stmt.executeUpdate(sql);         //将sql语句上传至数据库执行
                     System.out.println("增添内容成功!!");

                 }
                 else if(choice1 ==2)
                 {
                     System.out.println("********删除饭店信息********");
                     sql="delete from restaurant where rname = '";
                     System.out.println("请输入删除饭店的店名:");
                     info = sc.next();
                     sql =sql + info +"'";
                     sql1= "select * from restaurant where rname = '" + info + "'";
                     ResultSet rs = stmt.executeQuery(sql1);
                     if(rs.next()){
                         stmt.executeUpdate(sql);         //将sql语句上传至数据库执行
                         System.out.println("饭店信息删除成功!!");
                     }
                     else{
                         System.out.println("饭店信息不存在,无法删除");
                         stmt.close();
                         rs.close();
                         conn.close();
                         System.exit(0);
                     }
                     rs.close();
                 }
                 else if(choice1 ==3)
                 {
                     System.out.println("********修改饭店信息********");
                     sql ="update restaurant set ";
                     System.out.println("请输入修改饭店信息代码的: 1.饭店ID 2.饭店店名 3.饭店评价 ");
                     choice2=sc.nextInt();
                     if(choice2==1)
                         sql = sql + "rid = '";
                     else if(choice2==2)
                         sql = sql + "rname = '";
                     else if(choice2==3)
                         sql = sql + "rintroduce = '";
                     System.out.println("请输入修改后的选择信息:");
                     sql =sql + sc.next()+"'";
                     System.out.println("请输入想要修改的饭店店名:");
                     info = sc.next();
                     sql =sql + " where rname='" + info +"'";
                     sql1= "select * from restaurant where rname = '" + info + "'";
                     ResultSet rs = stmt.executeQuery(sql1);
                     if(rs.next()){
                         stmt.executeUpdate(sql);         //将sql语句上传至数据库执行
                         System.out.println("饭店信息修改内容成功!!");
                     }
                     else{
                         System.out.println("饭店信息不存在,无法修改");
                         stmt.close();
                         rs.close();
                         conn.close();
                         System.exit(0);
                     }
                     rs.close();
                 }
                 else if(choice1 ==4)
                 {
                     System.out.println("********查询饭店信息********");

                     System.out.println("是否查询所有饭店信息?0.是1.否");
                     flag=sc.nextInt();
                     if(flag==0)
                         sql="select * from restaurant";
                     else{
                         sql="select * from restaurant where rname = '";
                         System.out.println("请输入想要查询的饭店店名:");
                         info = sc.next();
                         sql =sql + info +"'";
                     }
                     System.out.println("查询结果如下:");
                     ResultSet re = stmt.executeQuery(sql);
                     if(re.next()){
                         ResultSet rs = stmt.executeQuery(sql);
                         ResultSetMetaData rsmd = rs.getMetaData();
                         int columnCount = rsmd.getColumnCount();
                         String columnName;
                         for( i = 0 ; i < columnCount; i++){   //把表头打印出来
                             //3. 得到列名
                             columnName = rsmd.getColumnName(i + 1);
                             System.out.print(columnName + "   ");//打印出表头
                         }
                         System.out.print("\n");
                         while(rs.next()){
                             // 通过字段检索

                             // 输出数据
                             for(i=1;i<=columnCount;i++)
                             {
                                 System.out.print(rs.getString(i)+" "); //直接把每一列打出来。
                             }
                             System.out.print("\n");

                         }
                         rs.close();

                     }
                     else{
                         System.out.println("饭店不存在无法查询");
                         stmt.close();
                         re.close();
                         conn.close();
                         System.exit(0);
                     }
                     //***************************************
                     re.close();


                 }

             }
             if(choice ==2) {
                 System.out.println("********店主信息管理*********");
                 System.out.println("1.创建店主信息 2.删除店主信息 3.修改店主信息 4. 查询店主信息");
                 choice1 = sc.nextInt();
                 if (choice1 == 1) {
                     System.out.println("********创建店主信息********");
                     sql = "INSERT  INTO  resowner  VALUES ('";
                     System.out.println("请输入店主ID:");
                     sql = sql + sc.next() + "','";
                     System.out.println("请输入店主姓名:");
                     sql = sql + sc.next() + "','";
                     System.out.println("请输入店主性别:");
                     sql = sql + sc.next() + "','";
                     System.out.println("请输入店主拥有饭店的ID:");
                     sql = sql + sc.next() + "','";
                     System.out.println("请输入店主密码:");
                     sql = sql + sc.next() + "')";
                     stmt.executeUpdate(sql);         //将sql语句上传至数据库执行
                     System.out.println("增添内容成功!!");

                 }
                 else if(choice1 ==2)
                 {
                     System.out.println("********删除店主信息********");
                     sql="delete from resowner where roid = '";
                     System.out.println("请输入删除店主的ID:");
                     info = sc.next();
                     sql =sql + info +"'";
                     sql1= "select * from resowner where roid = '" + info + "'";
                     ResultSet rs = stmt.executeQuery(sql1);
                     if(rs.next()){
                         stmt.executeUpdate(sql);         //将sql语句上传至数据库执行
                         System.out.println("店主信息删除成功!!");
                     }
                     else{
                         System.out.println("店主信息不存在,无法删除");
                         stmt.close();
                         rs.close();
                         conn.close();
                         System.exit(0);
                     }
                     rs.close();
                 }
                 else if(choice1 ==3)
                 {
                     System.out.println("********修改店主信息********");
                     sql ="update resowner set ";
                     System.out.println("请选择输入修改店主信息的代码: 1.店主ID 2.店主姓名 3.店主性别 4.店主拥有饭店ID 5.店主密码 ");
                     choice2=sc.nextInt();
                     if(choice2==1)
                         sql = sql + "roid = '";
                     else if(choice2==2)
                         sql = sql + "roname = '";
                     else if(choice2==3)
                         sql = sql + "rosex = '";
                     else if(choice2==4)
                         sql = sql + "rid = '";
                     else if(choice2==5)
                         sql = sql + "ropwd = '";
                     System.out.println("请输入修改后的选择信息:");
                     sql =sql + sc.next()+"'";
                     System.out.println("请输入想要修改的店主姓名:");
                     info = sc.next();
                     sql =sql + " where roname='" + info +"'";
                     sql1= "select * from resowner where roname = '" + info + "'";
                     ResultSet rs = stmt.executeQuery(sql1);
                     if(rs.next()){
                         stmt.executeUpdate(sql);         //将sql语句上传至数据库执行
                         System.out.println("店主信息修改内容成功!!");
                     }
                     else{
                         System.out.println("店主信息不存在,无法修改");
                         stmt.close();
                         rs.close();
                         conn.close();
                         System.exit(0);
                     }
                     rs.close();
                 }
                 else if(choice1 ==4)
                 {
                     System.out.println("********查询店主信息********");

                     System.out.println("是否查询所有店主信息?0.是1.否");
                     flag=sc.nextInt();
                     if(flag==0)
                         sql="select * from resowner";
                     else{
                         sql="select * from resowner where roname = '";
                         System.out.println("请输入想要查询的店主姓名:");
                         info = sc.next();
                         sql =sql + info +"'";
                     }
                     System.out.println("查询结果如下:");
                     ResultSet re = stmt.executeQuery(sql);
                     if(re.next()){
                         ResultSet rs = stmt.executeQuery(sql);
                         ResultSetMetaData rsmd = rs.getMetaData();
                         int columnCount = rsmd.getColumnCount();
                         String columnName;
                         for( i = 0 ; i < columnCount; i++){   //把表头打印出来
                             //3. 得到列名
                             columnName = rsmd.getColumnName(i + 1);
                             System.out.print(columnName + "   ");//打印出表头
                         }
                         System.out.print("\n");
                         while(rs.next()){
                             // 通过字段检索

                             // 输出数据
                             for(i=1;i<=columnCount;i++)
                             {
                                 System.out.print(rs.getString(i)+" "); //直接把每一列打出来。
                             }
                             System.out.print("\n");

                         }
                         rs.close();

                     }
                     else{
                         System.out.println("店主不存在无法查询");
                         stmt.close();
                         re.close();
                         conn.close();
                         System.exit(0);
                     }
                     //***************************************
                     re.close();


                 }

             }
             if(choice ==3) {
                 System.out.println("********顾客信息管理*********");
                 System.out.println("1.创建顾客信息 2.删除顾客信息 3.修改顾客信息 4. 查询顾客信息");
                 choice1 = sc.nextInt();
                 if (choice1 == 1) {
                     System.out.println("********创建顾客信息********");
                     sql = "INSERT  INTO  customer  VALUES ('";
                     System.out.println("请输入顾客ID:");
                     sql = sql + sc.next() + "','";
                     System.out.println("请输入顾客姓名:");
                     sql = sql + sc.next() + "','";
                     System.out.println("请输入顾客性别:");
                     sql = sql + sc.next() + "','";
                     System.out.println("请输入顾客密码:");
                     sql = sql + sc.next() + "')";
                     stmt.executeUpdate(sql);         //将sql语句上传至数据库执行
                     System.out.println("增添内容成功!!");

                 }
                 else if(choice1 ==2)
                 {
                     System.out.println("********删除顾客信息********");
                     sql="delete from customer where cid = '";
                     System.out.println("请输入删除顾客的ID:");
                     info = sc.next();
                     sql =sql + info +"'";
                     sql1= "select * from customer where cid = '" + info + "'";
                     ResultSet rs = stmt.executeQuery(sql1);
                     if(rs.next()){
                         stmt.executeUpdate(sql);         //将sql语句上传至数据库执行
                         System.out.println("顾客信息删除成功!!");
                     }
                     else{
                         System.out.println("顾客信息不存在,无法删除");
                         stmt.close();
                         rs.close();
                         conn.close();
                         System.exit(0);
                     }
                     rs.close();
                 }
                 else if(choice1 ==3)
                 {
                     System.out.println("********修改顾客信息********");
                     sql ="update customer set ";
                     System.out.println("请选择输入修改顾客信息的代码: 1.顾客ID 2.顾客姓名 3.顾客性别 4.顾客密码 ");
                     choice2=sc.nextInt();
                     if(choice2==1)
                         sql = sql + "cid = '";
                     else if(choice2==2)
                         sql = sql + "cname = '";
                     else if(choice2==3)
                         sql = sql + "csex = '";
                     else if(choice2==4)
                         sql = sql + "pwd = '";
                     System.out.println("请输入修改后的选择信息:");
                     sql =sql + sc.next()+"'";
                     System.out.println("请输入想要修改的顾客姓名:");
                     info = sc.next();
                     sql =sql + " where cname='" + info +"'";
                     sql1= "select * from customer where cname = '" + info + "'";
                     ResultSet rs = stmt.executeQuery(sql1);
                     if(rs.next()){
                         stmt.executeUpdate(sql);         //将sql语句上传至数据库执行
                         System.out.println("顾客信息修改内容成功!!");
                     }
                     else{
                         System.out.println("顾客信息不存在,无法修改");
                         stmt.close();
                         rs.close();
                         conn.close();
                         System.exit(0);
                     }
                     rs.close();
                 }
                 else if(choice1 ==4)
                 {
                     System.out.println("********查询顾客信息********");

                     System.out.println("是否查询所有顾客信息?0.是1.否");
                     flag=sc.nextInt();
                     if(flag==0)
                         sql="select * from customer";
                     else{
                         sql="select * from customer where cname = '";
                         System.out.println("请输入想要查询的顾客姓名:");
                         info = sc.next();
                         sql =sql + info +"'";
                     }
                     System.out.println("查询结果如下:");
                     ResultSet re = stmt.executeQuery(sql);
                     if(re.next()){
                         ResultSet rs = stmt.executeQuery(sql);
                         ResultSetMetaData rsmd = rs.getMetaData();
                         int columnCount = rsmd.getColumnCount();
                         String columnName;
                         for( i = 0 ; i < columnCount; i++){   //把表头打印出来
                             //3. 得到列名
                             columnName = rsmd.getColumnName(i + 1);
                             System.out.print(columnName + "   ");//打印出表头
                         }
                         System.out.print("\n");
                         while(rs.next()){
                             // 通过字段检索

                             // 输出数据
                             for(i=1;i<=columnCount;i++)
                             {
                                 System.out.print(rs.getString(i)+" "); //直接把每一列打出来。
                             }
                             System.out.print("\n");

                         }
                         rs.close();

                     }
                     else{
                         System.out.println("顾客不存在无法查询");
                         stmt.close();
                         re.close();
                         conn.close();
                         System.exit(0);
                     }
                     //***************************************
                     re.close();


                 }

             }
             else
             {
                 stmt.close();
                 conn.close();
                 System.exit(0);
             }

             // 完成后关闭
             stmt.close();
             conn.close();
         }catch(SQLException se){
             // 处理 JDBC 错误
             se.printStackTrace();
         }catch(Exception e){
             // 处理 Class.forName 错误
             e.printStackTrace();
         }finally{
             // 关闭资源
             try{
                 if(stmt!=null) stmt.close();
             }catch(SQLException se2){
             }// 什么都不做
             try{
                 if(conn!=null) conn.close();
             }catch(SQLException se){
                 se.printStackTrace();
             }
         }
         System.out.println("管理员,欢迎您再次登录!");
     }
     if(shenfen == 2)
     {
         try{
             // 注册 JDBC 驱动
             Class.forName(JDBC_DRIVER);

             // 打开链接
             System.out.println("顾客您好!请稍等,正在连接数据库...");
             conn = DriverManager.getConnection(DB_URL,USER,PASS);

             // 执行查询
             System.out.println("实例化Statement对象...");
             stmt = conn.createStatement();
             String sql;
             String x;
             int flag=0;
             int i;
             int flag1=0;
             //int num=0;
             int choice;
             int choice1;
             int choice2;
             int choice3;
             String info;
             String sql1;
             System.out.println("顾客你好,请选择接下来的操作代码:1.饭店信息管理 2.餐品信息管理 3.退出");
             choice=sc.nextInt();
             if(choice ==1) {
                 System.out.println("********饭店信息管理*********");
                 System.out.println("1.修改饭店评价 2. 查询饭店信息");
                 choice1 = sc.nextInt();
                 if(choice1 ==1)
                 {
                     System.out.println("********修改饭店评价********");
                     sql ="update restaurant set ";
                     System.out.println("请确认是修改评价: 0.确认 1.退出  ");
                     choice2=sc.nextInt();
                     if(choice2==0)
                         sql = sql + "rintroduce = '";
                     System.out.println("请输入想要修改的评价:");
                     sql =sql + sc.next()+"'";
                     System.out.println("请输入想要修改评价的饭店的店名:");
                     info = sc.next();
                     sql =sql + " where rname='" + info +"'";
                     sql1= "select * from restaurant where rname = '" + info + "'";
                     ResultSet rs = stmt.executeQuery(sql1);
                     if(rs.next()){
                         stmt.executeUpdate(sql);         //将sql语句上传至数据库执行
                         System.out.println("饭店评价修改成功!!");
                     }
                     else{
                         System.out.println("饭店不存在,无法修改");
                         stmt.close();
                         rs.close();
                         conn.close();
                         System.exit(0);
                     }
                     rs.close();
                 }
                 else if(choice1 ==2)
                 {
                     System.out.println("********查询饭店信息********");

                     System.out.println("是否查询所有饭店信息?0.是1.否");
                     flag=sc.nextInt();
                     if(flag==0)
                         sql="select * from restaurant";
                     else{
                         sql="select * from restaurant where rname = '";
                         System.out.println("请输入想要查询的饭店店名:");
                         info = sc.next();
                         sql =sql + info +"'";
                     }
                     System.out.println("查询结果如下:");
                     ResultSet re = stmt.executeQuery(sql);
                     if(re.next()){
                         ResultSet rs = stmt.executeQuery(sql);
                         ResultSetMetaData rsmd = rs.getMetaData();
                         int columnCount = rsmd.getColumnCount();
                         String columnName;
                         for( i = 0 ; i < columnCount; i++){   //把表头打印出来
                             //3. 得到列名
                             columnName = rsmd.getColumnName(i + 1);
                             System.out.print(columnName + "   ");//打印出表头
                         }
                         System.out.print("\n");
                         while(rs.next()){
                             // 通过字段检索

                             // 输出数据
                             for(i=1;i<=columnCount;i++)
                             {
                                 System.out.print(rs.getString(i)+" "); //直接把每一列打出来。
                             }
                             System.out.print("\n");

                         }
                         rs.close();

                     }
                     else{
                         System.out.println("饭店不存在无法查询");
                         stmt.close();
                         re.close();
                         conn.close();
                         System.exit(0);
                     }
                     //***************************************
                     re.close();


                 }

             }
             if(choice ==2) {
                 System.out.println("********餐品信息管理*********");
                 System.out.println("请确认是否查询餐品信息:0.是 1.退出");
                 choice1 = sc.nextInt();
                 if(choice1 ==0)
                 {

                     System.out.println("是否查询所有餐品信息?0.是1.否");
                     flag=sc.nextInt();
                     if(flag==0)
                         sql="select * from food";
                     else{
                         sql="select * from food where fid = '";
                         System.out.println("请输入想要查询的餐品ID:");
                         info = sc.next();
                         sql =sql + info +"'";
                     }
                     System.out.println("查询结果如下:");
                     ResultSet re = stmt.executeQuery(sql);
                     if(re.next()){
                         ResultSet rs = stmt.executeQuery(sql);
                         ResultSetMetaData rsmd = rs.getMetaData();
                         int columnCount = rsmd.getColumnCount();
                         String columnName;
                         for( i = 0 ; i < columnCount; i++){   //把表头打印出来
                             //3. 得到列名
                             columnName = rsmd.getColumnName(i + 1);
                             System.out.print(columnName + "   ");//打印出表头
                         }
                         System.out.print("\n");
                         while(rs.next()){
                             // 通过字段检索

                             // 输出数据
                             for(i=1;i<=columnCount;i++)
                             {
                                 System.out.print(rs.getString(i)+" "); //直接把每一列打出来。
                             }
                             System.out.print("\n");

                         }
                         rs.close();

                     }
                     else{
                         System.out.println("餐品不存在无法查询");
                         stmt.close();
                         re.close();
                         conn.close();
                         System.exit(0);
                     }
                     //***************************************
                     re.close();


                 }

             }

             else
             {
                 stmt.close();
                 conn.close();
                 System.exit(0);
             }

             // 完成后关闭
             stmt.close();
             conn.close();
         }catch(SQLException se){
             // 处理 JDBC 错误
             se.printStackTrace();
         }catch(Exception e){
             // 处理 Class.forName 错误
             e.printStackTrace();
         }finally{
             // 关闭资源
             try{
                 if(stmt!=null) stmt.close();
             }catch(SQLException se2){
             }// 什么都不做
             try{
                 if(conn!=null) conn.close();
             }catch(SQLException se){
                 se.printStackTrace();
             }
         }
         System.out.println("顾客,欢迎您再次登录!");
     }
     if(shenfen == 3)
     {
         try{
             // 注册 JDBC 驱动
             Class.forName(JDBC_DRIVER);

             // 打开链接
             System.out.println("厨师您好!请稍等,正在连接数据库...");
             conn = DriverManager.getConnection(DB_URL,USER,PASS);

             // 执行查询
             System.out.println("实例化Statement对象...");
             stmt = conn.createStatement();
             String sql;
             String x;
             int flag=0;
             int i;
             int flag1=0;
             //int num=0;
             int choice;
             int choice1;
             int choice2;
             int choice3;
             String info;
             String sql1;
             System.out.println("厨师你好,请选择接下来的操作代码:1.饭店信息管理 2.餐品信息管理 3.退出");
             choice=sc.nextInt();
             if(choice ==1) {
                 System.out.println("********饭店信息管理*********");
                     System.out.println("是否查询所有饭店信息?0.是1.否");
                     flag=sc.nextInt();
                     if(flag==0)
                         sql="select * from restaurant";
                     else{
                         sql="select * from restaurant where rname = '";
                         System.out.println("请输入想要查询的饭店店名:");
                         info = sc.next();
                         sql =sql + info +"'";
                     }
                     System.out.println("查询结果如下:");
                     ResultSet re = stmt.executeQuery(sql);
                     if(re.next()){
                         ResultSet rs = stmt.executeQuery(sql);
                         ResultSetMetaData rsmd = rs.getMetaData();
                         int columnCount = rsmd.getColumnCount();
                         String columnName;
                         for( i = 0 ; i < columnCount; i++){   //把表头打印出来
                             //3. 得到列名
                             columnName = rsmd.getColumnName(i + 1);
                             System.out.print(columnName + "   ");//打印出表头
                         }
                         System.out.print("\n");
                         while(rs.next()){
                             // 通过字段检索

                             // 输出数据
                             for(i=1;i<=columnCount;i++)
                             {
                                 System.out.print(rs.getString(i)+" "); //直接把每一列打出来。
                             }
                             System.out.print("\n");

                         }
                         rs.close();

                     }
                     else{
                         System.out.println("饭店不存在无法查询");
                         stmt.close();
                         re.close();
                         conn.close();
                         System.exit(0);
                     }
                     //***************************************
                     re.close();


                 }


             if(choice ==2) {
                 System.out.println("********餐品信息管理*********");
                 System.out.println("请确认是否查询餐品信息:0.是 1.退出");
                 choice1 = sc.nextInt();
                 if(choice1 ==0)
                 {

                     System.out.println("是否查询所有餐品信息?0.是1.否");
                     flag=sc.nextInt();
                     if(flag==0)
                         sql="select * from food";
                     else{
                         sql="select * from food where fid = '";
                         System.out.println("请输入想要查询的餐品ID:");
                         info = sc.next();
                         sql =sql + info +"'";
                     }
                     System.out.println("查询结果如下:");
                     ResultSet re = stmt.executeQuery(sql);
                     if(re.next()){
                         ResultSet rs = stmt.executeQuery(sql);
                         ResultSetMetaData rsmd = rs.getMetaData();
                         int columnCount = rsmd.getColumnCount();
                         String columnName;
                         for( i = 0 ; i < columnCount; i++){   //把表头打印出来
                             //3. 得到列名
                             columnName = rsmd.getColumnName(i + 1);
                             System.out.print(columnName + "   ");//打印出表头
                         }
                         System.out.print("\n");
                         while(rs.next()){
                             // 通过字段检索

                             // 输出数据
                             for(i=1;i<=columnCount;i++)
                             {
                                 System.out.print(rs.getString(i)+" "); //直接把每一列打出来。
                             }
                             System.out.print("\n");

                         }
                         rs.close();

                     }
                     else{
                         System.out.println("餐品不存在无法查询");
                         stmt.close();
                         re.close();
                         conn.close();
                         System.exit(0);
                     }
                     //***************************************
                     re.close();


                 }

             }

             else
             {
                 stmt.close();
                 conn.close();
                 System.exit(0);
             }

             // 完成后关闭
             stmt.close();
             conn.close();
         }catch(SQLException se){
             // 处理 JDBC 错误
             se.printStackTrace();
         }catch(Exception e){
             // 处理 Class.forName 错误
             e.printStackTrace();
         }finally{
             // 关闭资源
             try{
                 if(stmt!=null) stmt.close();
             }catch(SQLException se2){
             }// 什么都不做
             try{
                 if(conn!=null) conn.close();
             }catch(SQLException se){
                 se.printStackTrace();
             }
         }
         System.out.println("顾客,欢迎您再次登录!");
     }
     if(shenfen == 4)
     {
         try{
             // 注册 JDBC 驱动
             Class.forName(JDBC_DRIVER);

             // 打开链接
             System.out.println("店主您好!请稍等,正在连接数据库...");
             conn = DriverManager.getConnection(DB_URL,USER,PASS);

             // 执行查询
             System.out.println("实例化Statement对象...");
             stmt = conn.createStatement();
             String sql;
             String x;
             int flag=0;
             int i;
             int flag1=0;
             //int num=0;
             int choice;
             int choice1;
             int choice2;
             int choice3;
             String info;
             String sql1;
             System.out.println("店主你好,请选择接下来的操作代码:1.饭店信息管理 2.餐品信息管理 3.厨师信息管理 4.退出");
             choice=sc.nextInt();
             if(choice ==1) {
                 System.out.println("********饭店信息管理*********");

                     System.out.println("********查询饭店信息********");

                     System.out.println("是否查询所有饭店信息?0.是1.否");
                     flag=sc.nextInt();
                     if(flag==0)
                         sql="select * from restaurant";
                     else{
                         sql="select * from restaurant where rname = '";
                         System.out.println("请输入想要查询的饭店店名:");
                         info = sc.next();
                         sql =sql + info +"'";
                     }
                     System.out.println("查询结果如下:");
                     ResultSet re = stmt.executeQuery(sql);
                     if(re.next()){
                         ResultSet rs = stmt.executeQuery(sql);
                         ResultSetMetaData rsmd = rs.getMetaData();
                         int columnCount = rsmd.getColumnCount();
                         String columnName;
                         for( i = 0 ; i < columnCount; i++){   //把表头打印出来
                             //3. 得到列名
                             columnName = rsmd.getColumnName(i + 1);
                             System.out.print(columnName + "   ");//打印出表头
                         }
                         System.out.print("\n");
                         while(rs.next()){
                             // 通过字段检索

                             // 输出数据
                             for(i=1;i<=columnCount;i++)
                             {
                                 System.out.print(rs.getString(i)+" "); //直接把每一列打出来。
                             }
                             System.out.print("\n");

                         }
                         rs.close();

                     }
                     else{
                         System.out.println("饭店不存在无法查询");
                         stmt.close();
                         re.close();
                         conn.close();
                         System.exit(0);
                     }
                     //***************************************
                     re.close();




             }
             if(choice ==2) {
                 System.out.println("********餐品信息管理*********");
                 System.out.println("1.创建餐品信息 2.删除餐品信息 3.修改餐品信息 4. 查询餐品信息");
                 choice1 = sc.nextInt();
                 if (choice1 == 1) {
                     System.out.println("********创建餐品信息********");
                     sql = "INSERT  INTO  food  VALUES ('";
                     System.out.println("请输入餐品ID:");
                     sql = sql + sc.next() + "','";
                     System.out.println("请输入餐品名字:");
                     sql = sql + sc.next() + "','";
                     System.out.println("请输入餐品价格:");
                     sql = sql + sc.next() + "','";
                     System.out.println("请输入餐品销量:");
                     sql = sql + sc.next() + "','";
                     System.out.println("请输入餐品所属的饭店ID:");
                     sql = sql + sc.next() + "')";
                     stmt.executeUpdate(sql);         //将sql语句上传至数据库执行
                     System.out.println("增添内容成功!!");

                 }
                 else if(choice1 ==2)
                 {
                     System.out.println("********删除餐品信息********");
                     sql="delete from food where fid = '";
                     System.out.println("请输入删除餐品的ID:");
                     info = sc.next();
                     sql =sql + info +"'";
                     sql1= "select * from food where fid = '" + info + "'";
                     ResultSet rs = stmt.executeQuery(sql1);
                     if(rs.next()){
                         stmt.executeUpdate(sql);         //将sql语句上传至数据库执行
                         System.out.println("餐品信息删除成功!!");
                     }
                     else{
                         System.out.println("餐品信息不存在,无法删除");
                         stmt.close();
                         rs.close();
                         conn.close();
                         System.exit(0);
                     }
                     rs.close();
                 }
                 else if(choice1 ==3)
                 {
                     System.out.println("********修改餐品信息********");
                     sql ="update food set ";
                     System.out.println("请选择输入修改餐品信息的代码: 1.餐品ID 2.餐品名字 3.餐品价格 4.餐品销量 5.餐品所属餐馆ID ");
                     choice2=sc.nextInt();
                     if(choice2==1)
                         sql = sql + "fid = '";
                     else if(choice2==2)
                         sql = sql + "fname = '";
                     else if(choice2==3)
                         sql = sql + "fprice = '";
                     else if(choice2==4)
                         sql = sql + "fsales = '";
                     else if(choice2==5)
                         sql = sql + "rid = '";
                     System.out.println("请输入修改后的选择信息:");
                     sql =sql + sc.next()+"'";
                     System.out.println("请输入想要修改的餐品ID:");
                     info = sc.next();
                     sql =sql + " where fid='" + info +"'";
                     sql1= "select * from food where fid = '" + info + "'";
                     ResultSet rs = stmt.executeQuery(sql1);
                     if(rs.next()){
                         stmt.executeUpdate(sql);         //将sql语句上传至数据库执行
                         System.out.println("餐品信息修改内容成功!!");
                     }
                     else{
                         System.out.println("餐品信息不存在,无法修改");
                         stmt.close();
                         rs.close();
                         conn.close();
                         System.exit(0);
                     }
                     rs.close();
                 }
                 else if(choice1 ==4)
                 {
                     System.out.println("********查询餐品信息********");

                     System.out.println("是否查询所有餐品信息?0.是1.否");
                     flag=sc.nextInt();
                     if(flag==0)
                         sql="select * from food";
                     else{
                         sql="select * from food where fid = '";
                         System.out.println("请输入想要查询的餐品ID:");
                         info = sc.next();
                         sql =sql + info +"'";
                     }
                     System.out.println("查询结果如下:");
                     ResultSet re = stmt.executeQuery(sql);
                     if(re.next()){
                         ResultSet rs = stmt.executeQuery(sql);
                         ResultSetMetaData rsmd = rs.getMetaData();
                         int columnCount = rsmd.getColumnCount();
                         String columnName;
                         for( i = 0 ; i < columnCount; i++){   //把表头打印出来
                             //3. 得到列名
                             columnName = rsmd.getColumnName(i + 1);
                             System.out.print(columnName + "   ");//打印出表头
                         }
                         System.out.print("\n");
                         while(rs.next()){
                             // 通过字段检索

                             // 输出数据
                             for(i=1;i<=columnCount;i++)
                             {
                                 System.out.print(rs.getString(i)+" "); //直接把每一列打出来。
                             }
                             System.out.print("\n");

                         }
                         rs.close();

                     }
                     else{
                         System.out.println("餐品不存在无法查询");
                         stmt.close();
                         re.close();
                         conn.close();
                         System.exit(0);
                     }
                     //***************************************
                     re.close();


                 }

             }


             if(choice ==3) {
                 System.out.println("********厨师信息管理*********");
                 System.out.println("请确认是否查询厨师信息:0.是 1.否");
                 choice1 = sc.nextInt();
                 if(choice1 ==0)
                 {
                     System.out.println("********查询厨师信息********");

                     System.out.println("是否查询所有厨师信息?0.是1.否");
                     flag=sc.nextInt();
                     if(flag==0)
                         sql="select cookerid,rid,cookername,cookersex from cooker";
                     else{
                         sql="select cookerid,rid,cookername,cookersex from cooker where cookername = '";
                         System.out.println("请输入想要查询的厨师姓名:");
                         info = sc.next();
                         sql =sql + info +"'";
                     }
                     System.out.println("查询结果如下:");
                     ResultSet re = stmt.executeQuery(sql);
                     if(re.next()){
                         ResultSet rs = stmt.executeQuery(sql);
                         ResultSetMetaData rsmd = rs.getMetaData();
                         int columnCount = rsmd.getColumnCount();
                         String columnName;
                         for( i = 0 ; i < columnCount; i++){   //把表头打印出来
                             //3. 得到列名
                             columnName = rsmd.getColumnName(i + 1);
                             System.out.print(columnName + "   ");//打印出表头
                         }
                         System.out.print("\n");
                         while(rs.next()){
                             // 通过字段检索

                             // 输出数据
                             for(i=1;i<=columnCount;i++)
                             {
                                 System.out.print(rs.getString(i)+" "); //直接把每一列打出来。
                             }
                             System.out.print("\n");

                         }
                         rs.close();

                     }
                     else{
                         System.out.println("厨师不存在无法查询");
                         stmt.close();
                         re.close();
                         conn.close();
                         System.exit(0);
                     }
                     //***************************************
                     re.close();


                 }

             }
             else
             {
                 stmt.close();
                 conn.close();
                 System.exit(0);
             }

             // 完成后关闭
             stmt.close();
             conn.close();
         }catch(SQLException se){
             // 处理 JDBC 错误
             se.printStackTrace();
         }catch(Exception e){
             // 处理 Class.forName 错误
             e.printStackTrace();
         }finally{
             // 关闭资源
             try{
                 if(stmt!=null) stmt.close();
             }catch(SQLException se2){
             }// 什么都不做
             try{
                 if(conn!=null) conn.close();
             }catch(SQLException se){
                 se.printStackTrace();
             }
         }
         System.out.println("厨师,欢迎您再次登录!");
     }


 }

}

posted @ 2021-06-11 17:27  20181309  阅读(69)  评论(0编辑  收藏  举报