package day02;
import java.sql.*;
import java.io.*;
public class DBTool
{
static{//加载驱动是前提
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static void main(String[] args){
   Connection con = null;
   //1.登录数据库
   while((con=login())==null){};
   //2.处理用户命令
   process(con);
   //3.bye-bye
   try{
     con.close();
   }catch(Exception e){
     e.printStackTrace();
   }
   System.out.println("再见!"); }
public static Connection login(){
   Connection con = null;
   String url = prompt("请输入url:");
       String user = prompt("请输入用户名:");
   String pwd = prompt("请输入密码:");
   
   try{
     con = DriverManager.getConnection(url,user,pwd);
   }catch(Exception e){
     e.printStackTrace();
   }
   return con;
}
    public static void process(Connection con){
  boolean flag = true;
  String command = "";
  while(flag){
    command = getCommand();
    if("quit".equals(command)){
      flag=false;
}else{      
  processSql(con,command);
    }
  }
}
public static String getCommand(){///???????
  String command = "";
  StringBuffer sb = new StringBuffer();
  String message = "->";//这里的message是怎么获得控制台输入的指令的啊????
      int i = 0;
  while(!command.endsWith(";")){
    if(i++>0) message = i+"->";
sb.append(" "+prompt(message));
    command=sb.toString().trim();
  }
  return command.substring(0,command.length()-1);
}    public static void processSql(Connection con ,String sql){
  PreparedStatement ps = null;
  ResultSet rs = null;
  try{
    ps = con.prepareStatement(sql);
boolean flag = ps.execute();
int i = 0;
if(flag){
  rs = ps.getResultSet();
  System.out.println("rs->"+rs);
}else{
  i = ps.getUpdateCount();
  System.out.println("更新成功-"+i);
}
  }catch(Exception e){
    //e.printStackTrace();
System.out.println("执行SQL失败!");
  }finally{
    JdbcUtil.close(rs,ps,null);
  }
}
public static String prompt(String message){
   BufferedReader in = new BufferedReader(new InputStreamReader(System.in));//这句话在这个方法完了之后是不是就没效果了
   System.out.print(message);
   String command = "";
   try{
      command = in.readLine();
   }catch(Exception e){
      e.printStackTrace();
   }
   return command;
}
}

解决方案 »

  1.   

    上面问题解决了  我没注意看 
    大大们帮我看下把,问题在注释里面package day02;
    import java.sql.*;
    import java.io.*;
    public class DBTool
    {
    static{//加载驱动是前提
    try {
    Class.forName("oracle.jdbc.driver.OracleDriver");
    } catch (ClassNotFoundException e) {
    e.printStackTrace();
    }
    }
    public static void main(String[] args){
       Connection con = null;
       //1.登录数据库
       while((con=login())==null){};
       //2.处理用户命令
       process(con);
       //3.bye-bye
       try{
         con.close();
       }catch(Exception e){
         e.printStackTrace();
       }
       System.out.println("再见!"); }
    public static Connection login(){
       Connection con = null;
       String url = prompt("请输入url:");
           String user = prompt("请输入用户名:");
       String pwd = prompt("请输入密码:");
       
       try{
         con = DriverManager.getConnection(url,user,pwd);
       }catch(Exception e){
         e.printStackTrace();
       }
       return con;
    }
        public static void process(Connection con){
      boolean flag = true;
      String command = "";
      while(flag){
        command = getCommand();
        if("quit".equals(command)){
          flag=false;
    }else{      
      processSql(con,command);
        }
      }
    }
    public static String getCommand(){///???????
      String command = "";
      StringBuffer sb = new StringBuffer();
      String message = "->";
          int i = 0;
      while(!command.endsWith(";")){
        if(i++>0) message = i+"->";
    sb.append(" "+prompt(message));
        command=sb.toString().trim();
      }
      return command.substring(0,command.length()-1);
    }    public static void processSql(Connection con ,String sql){
      PreparedStatement ps = null;
      ResultSet rs = null;
      try{
        ps = con.prepareStatement(sql);
    boolean flag = ps.execute();        //这里返回值啥是true 啥时是false
    int i = 0;
    if(flag){
      rs = ps.getResultSet();
      System.out.println("rs->"+rs);
    }else{                                 //false为什么还要执行这里 跟新了多少条记录
      i = ps.getUpdateCount();
      System.out.println("更新成功-"+i);
    }
      }catch(Exception e){
        //e.printStackTrace();
    System.out.println("执行SQL失败!");
      }finally{
        JdbcUtil.close(rs,ps,null);
      }
    }
    public static String prompt(String message){
       BufferedReader in = new BufferedReader(new InputStreamReader(System.in));//这句话在这个方法完了之后是不是就没效果了
       System.out.print(message);
       String command = "";
       try{
          command = in.readLine();
       }catch(Exception e){
          e.printStackTrace();
       }
       return command;
    }
    }
      

  2.   

       public static String getCommand(){///获得sql语句
          String command = "";
          StringBuffer sb = new StringBuffer();
          String message = "->";
          int i = 0;
          while(!command.endsWith(";")){//sql语句的结束符为“;”
            if(i++>0) message = i+"->";
            sb.append(" "+prompt(message));
            command=sb.toString().trim();
          }
          return command.substring(0,command.length()-1);
        }.................... public static void processSql(Connection con ,String sql){
          PreparedStatement ps = null;
          ResultSet rs = null;
          try{
            ps = con.prepareStatement(sql);
            boolean flag = ps.execute();        //当你的sql语句争取执行时,它就返回true,否则返回false。PreparedStatement是预处理语句。
            int i = 0;
            if(flag){
              rs = ps.getResultSet();
              System.out.println("rs->"+rs);
            }else{                                 //只是判断下更新是否成功,如果不显示“更新成功-i”,你就知道自己的sql语句没有被数据库接收,起到一个提示的作用,因为你用上面的没有显示提示。除非你在上面if语句中加一条提示语句 System.out.println("更新成功没有成功");那么下面这个你可以不用写。
              i = ps.getUpdateCount();
              System.out.println("更新成功-"+i);
            }
          }catch(Exception e){
            //e.printStackTrace();
            System.out.println("执行SQL失败!");
          }finally{
            JdbcUtil.close(rs,ps,null);
          }
        }....................public static String prompt(String message){
           BufferedReader in = new BufferedReader(new InputStreamReader(System.in));//这句话是局部的复制,只有一个功能就是给in复制,结束后当然就没意义了
           System.out.print(message);
           String command = "";
           try{
              command = in.readLine();
           }catch(Exception e){
              e.printStackTrace();
           }
           return command;
        }
      

  3.   

    boolean flag = ps.execute(); //当你的sql语句正确执行时,它就返回true,否则返回false。打错了!!