无法从两个项目位于不同系统中的另一个项目运行Java项目



我在不同的系统中有两个项目。两个系统都通过局域网电缆连接。我在我的PC上有Try.java,在另一台名为HRIDYA-PC的PC上有Hello.java。我想从Try.java调用MySum.java。我希望MySum.java在其他系统中执行,而不是在我的系统中执行。

这是我的代码Try.java

package pkgtry;
import java.io.*;
public class Try 
{
  private static void runProcess(String command) throws Exception 
  {
    Process pro = Runtime.getRuntime().exec(command);
    pro.waitFor();
    InputStream inputStream = pro.getInputStream();
    int b = -1;
    while ( (b =  inputStream.read()) != -1 ) 
    {
        System.out.write(b);
    }
  }
  public static void main(String[] args) 
  {
      try 
      {
          runProcess("javac -d . \HRIDYA-PC\Users\Hridya\Documents\NetBeansProjects\hello\src\hello\Hello.java");
          runProcess("java hello.Hello ");
      }
      catch (Exception e) 
      {
          e.printStackTrace();
      }
  } 
}

另一个系统中的java项目MySum包含两个java文件- connectdb .java和MySum .java

MySum.java的代码是

package mysum;
import java.sql.*;
import java.util.Scanner;
public class MySum 
{
    public static String dbName="partition",dbName1="results",tableName="p1",query;
    public static Connectdb con=new Connectdb();
    public static Connection conn=null;
    public static Connection conn1=null;
    public static String[] tnames;
    public static int sumTransactionId=0,sumCustomerId=0,sumDepositAmount=0,sumWithdrawAmount=0,totalNoOfRange=3;
    public static int i=0,select=0,range,size;
    public static int[] tid,id,amount;
    public static String[] type,date;
    public static void main(String[] args) 
    {
        try
        {
            conn=con.connect(dbName);
            conn1=con.connect(dbName1);
            query="create table partialSum(Attribute varchar(20),Sum int)";
            PreparedStatement ps=conn1.prepareStatement(query);
            ps.executeUpdate();
            Statement stm=conn.createStatement();
            ResultSet rs=stm.executeQuery("select count(*) from "+tableName);
            rs.next();
            size=rs.getInt(1);
            tid=new int[size]; 
            id=new int[size];
            date=new String[size];
            type=new String[size];
            amount=new int[size];
            rs=stm.executeQuery("select * from "+tableName);
            while(rs.next())
            {
                tid[i]=rs.getInt(1);
                id[i]=rs.getInt(2);
                date[i]=rs.getString(3);
                type[i]=rs.getString(4);
                amount[i]=rs.getInt(5);
                i++;
            }
            switch(select)
            {
                 case 0:
                        for(i=0;i<size;i++)
                        {
                            sumCustomerId+=id[i];
                        }
                        query="insert into partialSum values('Customer ID','"+sumCustomerId+"')";
                        ps=conn1.prepareStatement(query);
                        ps.executeUpdate();
                        break;
                case 1: 
                        for(i=0;i<size;i++)
                        {
                            sumTransactionId+=tid[i];
                        }
                        query="insert into partialSum values('Transaction ID','"+sumTransactionId+"')";
                        ps=conn1.prepareStatement(query);
                        ps.executeUpdate();
                        break;
                case 2:
                        for(i=0;i<size;i++)
                        {
                            if("Deposit".equals(type[i]))
                            {
                                sumDepositAmount+=amount[i];
                            }
                        }
                        query="insert into partialSum values('Amount Deposited','"+sumDepositAmount+"')";
                        ps=conn1.prepareStatement(query);
                        ps.executeUpdate();
                        break;
                case 3:
                        for(i=0;i<size;i++)
                        {
                            if("Withdrawal".equals(type[i]))
                            {
                                sumWithdrawAmount+=amount[i];
                            }
                        }
                        query="insert into partialSum values('Amount Withdrawed','"+sumWithdrawAmount+"')";
                        ps=conn1.prepareStatement(query);
                        ps.executeUpdate();
                        break;       
            }
        }
        catch(Exception e)
        {
            System.out.println(e);
        }
    }
}

Connectdb.java的代码是

package mysum;
import java.sql.*;
public class Connectdb {
    public String hostname;
    public String username;
    public String password;
    public String dbName;
    public String connectionUrl1;
    public Connection connect(String db)
    {
        hostname = "localhost";
        username = "postgres";
    password = "geethu";
        dbName = db;
    connectionUrl1 = "jdbc:postgresql://" + hostname + "/" + dbName;
        Connection conn=null;
        try{
             Class.forName("org.postgresql.Driver");
       } catch (ClassNotFoundException cnfe){
       System.out.println("Could not find the JDBC driver!");
       System.exit(1);
       }
        try {
                System.out.println("before Connected successfully = "+connectionUrl1);
                conn = DriverManager.getConnection(connectionUrl1,username, password);
                System.out.println("Connected successfully");
             } catch (SQLException sqle) {
                  System.out.println("Connection failed");
                  System.out.println(sqle);
                  System.exit(1);
                 }
         return conn;
     }
}

有谁能帮我一下吗

明白了!在远程计算机名称前再添加两个斜杠: HRIDYA-PC . .在现实世界中,双斜杠相当于一个斜杠(java转义)。希望这对你有帮助!

相关内容

  • 没有找到相关文章

最新更新