我有两个项目,每个项目都在不同的系统中。两个系统都通过 LAN 电缆连接。我的 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;
}
}
任何人都可以帮助我。