首页 \ 问答 \ Unix shell脚本可以用来操作数据库吗?(Can Unix shell script be used to manipulate databases?)

Unix shell脚本可以用来操作数据库吗?(Can Unix shell script be used to manipulate databases?)

我必须从某些文件中读取数据并将数据插入到数据库中的不同表中。 Unix shell脚本足以胜任这项工作吗?

在shell脚本中做这项工作很容易,还是应该在Java中做这个工作?


I have to read data from some files and insert the data into different tables in a database. Is Unix shell script powerful enough to do the job?

Is it easy to do the job in shell script or should I go about doing this in Java?


原文:https://stackoverflow.com/questions/71775
更新时间:2022-11-06 19:11

最满意答案

您的方法变量Altitude是方法的本地变量。 这意味着每次调用方法时都会重新创建和重新分配值。 在java中,方法的所有参数都按值传递,这意味着方法内部参数的更改在其外部不可见,基本上它在调用时会创建参数的副本。 您需要通过在类中定义不在任何方法内部来使其成为实例变量。 每次调用方法时,您希望更改的其他变量也是如此。

import java.util.Scanner;

public class Pilot
{

  public static void main(String args[])
  {
    Scanner kb = new Scanner(System.in);


    double Altitude = 10.0;

    String Name = "Eagle";

    double fuelLeft = 1000.0;

    int shipWeight = 400;

    int maxThrust = 10000;

    int verticalSpeed = 0;

    double efficiency = 0;

    int maxFuel = 400; //max fuel flow

    LunarLander lander = new LunarLander(Altitude, verticalSpeed, fuelLeft); // create a LunarLander object

    System.out.println("Initial data: ");
    System.out.println("Altitude = " + Altitude);
    System.out.println("Speed = " + verticalSpeed);
    System.out.println("Fuel = " + fuelLeft);

    while (lander.crashed() != true && Altitude > 0)
    {

      System.out.println("Please enter a time in seconds: ");
      int time = kb.nextInt();
      System.out.println("Please enter a fuel rate between 0 and 1");
      double inputFuelRate = kb.nextDouble();
      System.out.println("Input time increment: " + time);
      System.out.println("Input fuel rate: " + inputFuelRate);

      lander.move(efficiency, maxFuel, time, inputFuelRate);


      System.out.println(lander.toString());
    }

  }
}

还有LunarLander课程

public class LunarLander {

    double Altitude;
    double verticalSpeed;
    double fuelLeft;

    public LunarLander(double Altitude, double verticalSpeed, double fuelLeft){
        this.Altitude = Altitude;
        this.verticalSpeed = verticalSpeed;
        this.fuelLeft = fuelLeft;
    }

    public void move(double efficiency, int maxFuel,
            int time, double inputFuelRate) {
        // formulas to change outputs
        double velocity = time * (efficiency - 1.62); // given formula to
                                                        // caluculate velocity
        double altChng = time * velocity; // creates a variable for atitude
                                            // chage

        // exceptions
        if (efficiency > 0 && fuelLeft == 0) { // changes efficiency to 0 when
                                                // there is no fuel left
            efficiency = 0;
        } else {
        }

        // new outputs
        Altitude = Altitude - altChng; // calculates new altitude by subtracting
                                        // altitude change
        velocity = time * (efficiency - 1.62); // given formula to caluculate
                                                // velocity
        altChng = time * velocity; // creates a variable for atitude chage
        verticalSpeed = velocity; // since the ship would only move and
                                            // not go back and forth velocity is
                                            // speed

        efficiency = inputFuelRate / maxFuel; // recalculates efficiency

        double fuelLoss = time * fuelLeft * maxFuel;// new variable to determine
                                                    // how much fuel was burned
                                                    // during time period
        fuelLeft = fuelLeft - fuelLoss; // changes the values for fuel left

    }

    public boolean crashed() {
        if (Altitude == 0 && verticalSpeed < -1) {
            return true;
        } else {
            return false;
        }
    }

    public String toString() {
        String output = "";
        output += "Eagle: \n";
        output += "Altitude = " + Altitude + "\n";
        output += "Speed = " + verticalSpeed + "\n";
        output += "Fuel = " + fuelLeft + "\n";

        return output;
    }

}

Your method variable Altitude is local to the method. This means its recreated and reassigned value every time you call the method. In java all parameters to a method are passed by value, which means the change in the parameter inside the the method is not visible outside it, basically it creates a copy of the parameter when invoked. You need to make it an instance variable by defining in the class not inside any method. same goes for the other variables that you want to be changed every time you call the method.

import java.util.Scanner;

public class Pilot
{

  public static void main(String args[])
  {
    Scanner kb = new Scanner(System.in);


    double Altitude = 10.0;

    String Name = "Eagle";

    double fuelLeft = 1000.0;

    int shipWeight = 400;

    int maxThrust = 10000;

    int verticalSpeed = 0;

    double efficiency = 0;

    int maxFuel = 400; //max fuel flow

    LunarLander lander = new LunarLander(Altitude, verticalSpeed, fuelLeft); // create a LunarLander object

    System.out.println("Initial data: ");
    System.out.println("Altitude = " + Altitude);
    System.out.println("Speed = " + verticalSpeed);
    System.out.println("Fuel = " + fuelLeft);

    while (lander.crashed() != true && Altitude > 0)
    {

      System.out.println("Please enter a time in seconds: ");
      int time = kb.nextInt();
      System.out.println("Please enter a fuel rate between 0 and 1");
      double inputFuelRate = kb.nextDouble();
      System.out.println("Input time increment: " + time);
      System.out.println("Input fuel rate: " + inputFuelRate);

      lander.move(efficiency, maxFuel, time, inputFuelRate);


      System.out.println(lander.toString());
    }

  }
}

And the LunarLander class

public class LunarLander {

    double Altitude;
    double verticalSpeed;
    double fuelLeft;

    public LunarLander(double Altitude, double verticalSpeed, double fuelLeft){
        this.Altitude = Altitude;
        this.verticalSpeed = verticalSpeed;
        this.fuelLeft = fuelLeft;
    }

    public void move(double efficiency, int maxFuel,
            int time, double inputFuelRate) {
        // formulas to change outputs
        double velocity = time * (efficiency - 1.62); // given formula to
                                                        // caluculate velocity
        double altChng = time * velocity; // creates a variable for atitude
                                            // chage

        // exceptions
        if (efficiency > 0 && fuelLeft == 0) { // changes efficiency to 0 when
                                                // there is no fuel left
            efficiency = 0;
        } else {
        }

        // new outputs
        Altitude = Altitude - altChng; // calculates new altitude by subtracting
                                        // altitude change
        velocity = time * (efficiency - 1.62); // given formula to caluculate
                                                // velocity
        altChng = time * velocity; // creates a variable for atitude chage
        verticalSpeed = velocity; // since the ship would only move and
                                            // not go back and forth velocity is
                                            // speed

        efficiency = inputFuelRate / maxFuel; // recalculates efficiency

        double fuelLoss = time * fuelLeft * maxFuel;// new variable to determine
                                                    // how much fuel was burned
                                                    // during time period
        fuelLeft = fuelLeft - fuelLoss; // changes the values for fuel left

    }

    public boolean crashed() {
        if (Altitude == 0 && verticalSpeed < -1) {
            return true;
        } else {
            return false;
        }
    }

    public String toString() {
        String output = "";
        output += "Eagle: \n";
        output += "Altitude = " + Altitude + "\n";
        output += "Speed = " + verticalSpeed + "\n";
        output += "Fuel = " + fuelLeft + "\n";

        return output;
    }

}

相关问答

更多
  • 您的方法变量Altitude是方法的本地变量。 这意味着每次调用方法时都会重新创建和重新分配值。 在java中,方法的所有参数都按值传递,这意味着方法内部参数的更改在其外部不可见,基本上它在调用时会创建参数的副本。 您需要通过在类中定义不在任何方法内部来使其成为实例变量。 每次调用方法时,您希望更改的其他变量也是如此。 import java.util.Scanner; public class Pilot { public static void main(String args[]) { ...
  • 解决方案如下: 通常创建服务。 该服务通过JMX公开了许多方法,或者只是通过监听TCP / unix-domain套接字来获取一组有限的已知命令。 当需要控制时,服务是专用进程(可能是用Java编写的实用程序,与服务分开),通过指定的命令通道发出所需命令。 我在评论中提到的SO问题包含所有必要的技术细节。 实际上所有的学分应该在那里,我只是重新安排他们的话。 或者在类Unix系统中,包括Linux,您可以为给定的pid发送SIGSTOP信号( kill -STOP ),并且在收到SIGCONT之 ...
  • 好吧,这有点令人尴尬,但我终于找到了问题,离我的调试器让我相信的地方很远。 简而言之,我有一个try块,它从文件加载一些数据,如果抛出异常,则在catch块中调用System.exit(1) 。 由于该文件位于.jar中,因此它的路径与它在原始项目中位于src文件夹中时的路径不同。 这导致抛出FileNotFoundException,退出程序。 出于某种原因,我的调试器报告说程序在到达此代码之前很久就失败了。 为此,我仍然没有解释。 如果其他人有这个问题,我建议从代码中删除所有出现的System.exit ...
  • 所以,我想问一下,我不能在字符串以外的main方法中使用不同的类型。 不,你不能。 Main方法的参数是在命令行上传递的参数,因此它只能是一个字符串数组。 你怎么会在命令行上传递Mtb.Application ? So, I want to ask, cant I use differnt type in main method other than string. No, you can't. The arguments to the Main method are the ones passed on ...
  • 你可以定义一个“主机”接口,在那里有方法,并将其传递给插件。 或者,您可以使用仅包含委托签名的共享源文件(因此没有引用也没有依赖关系),这基本上就是您在使用Func时所做的事情,只有您可以使其更精确和更好地命名。 I have removed some of the discretionary Actions from the Interface and instead have created a custom event handler statically, within a class (In th ...
  • 但是如果我使用线程休眠,那么它会在每次迭代中得到延迟。 我正在使用c#4.0。 你可以使用Thread.Sleep(0) ,它表示: 该线程应该被挂起以允许其他等待线程执行。 static void Main(string[] args) { while(true) { processData(); Thread.sleep(0); } } 这将允许线程放弃对其他线程的一些处理,防止它使用100%的CPU,但仍然允许它以接近全速运行。 请注意,使用任何延迟虽然会降低整体CPU使 ...
  • 欢迎来到java :) 我尝试使用最简单的单词。 回答你的问题: 当您运行程序时,系统会调用main方法,因此您必须使用此名称(main),因为当您的应用程序启动时,有人默认调用方法main。 如果你选择另一个名字...你不能运行你的程序,因为当系统(我称之为系统,因为我认为你需要阅读一点)调用主方法,如果它找不到它你就无法启动你的程序。 试着想一想: 必须有人必须开始你的程序吗? 但它如何知道你的程序必须从哪里开始呢? 因此,java(还有其他语言)决定begin是方法main。 Welcome to j ...
  • 问题是:source-paths project.clj :source-paths 。 Lein希望您的命名空间文件夹直接位于源路径中。 您可以将naac文件夹直接移动到src (例如src/naac/server.clj )或使用["src/clj"]作为值:source-paths The problem is :source-paths in your project.clj. Lein expects your namespace folder to be directly in the sour ...
  • 这段代码有很多问题,很难知道从哪里开始。 这里有一些建议: 好名字很重要。 对于一堂课,你可以而且必须比A2做得更好。 学习并遵循Sun Java编码标准。 风格和可读性很重要。 学习一个好的代码布局并坚持下去。 从这开始。 它运行并给出正确的结果: import javax.swing.*; /** * A2 * @author Michael * @link https://stackoverflow.com/questions/30965862/calling-method-to-a-main ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。