[英]Trying to use a different variable that I have already declared in a different .java file in my new file
This is probably a simple fix or issue, but I am new to java and not sure how to do it. 这可能是一个简单的修复程序或问题,但是我是java的新手,不确定如何去做。 I have a web service that I am creating and a bunch of java files that I have with different data inputs. 我有一个正在创建的Web服务,以及一堆具有不同数据输入的Java文件。 I want to be able to use the variables already defined in my other java files in my new file. 我希望能够在新文件中使用其他Java文件中已经定义的变量。 Below you will see my code. 在下面,您将看到我的代码。 For example on the first part of code, SponsorOrganizationalIdentifier is a column name in my MySQL database, along with a name already declared in other java files. 例如,在代码的第一部分,SponsorOrganizationalIdentifier是我的MySQL数据库中的列名,以及在其他Java文件中已经声明的名称。 How do I use the different variables that I have already declared in my other webserivce .java files? 如何使用在其他webserivce .java文件中已经声明的不同变量?
Thanks for all your help! 感谢你的帮助!
package org.example.www.newwsdlfile3;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
public class JavaMYSQL {
public static void main(String[] args) throws Exception {
getConnection();
String sql = "INSERT INTO tableName values(':SponsorOrganiationalIdentifier', ':AgencyPersonGUID',':PersonID')";
String mySponsorID ="";
mySponsorID = "local"
sql = sql.replace(":SponsorOrganizationIdentifier", );
System.out.println(sql);
String AgencyGUID =""
AgencyGUID =
sql = sql.replace(:AgencyPersonGUID, )
System.out.println(sql)
String PersonIdent
PersonIdent =
sql = sql.replace(:PersonID,)
System.out.println(sql)
}
public static Connection getConnection() throws Exception{
try{
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/exchangeinformation";
String username = "root";
String password = "admin";
Class.forName(driver);
Connection conn = DriverManager.getConnection(url,username,password);
System.out.println("Connected");
return conn;
} catch (Exception e) {System.out.println(e);}
return null;
}
} }
If you have constant data like column names, consider creating a class then defining them as static final class data. 如果您有常量数据(例如列名),请考虑创建一个类,然后将其定义为静态最终类数据。
For example: 例如:
class TestClass {
public static final String MY_COLUMN_NAME = "COLUMN_NAME";
}
Then you can access this variable in another class using TestClass.MY_COLUMN_NAME
. 然后,您可以使用TestClass.MY_COLUMN_NAME
在另一个类中访问此变量。
If the data is not final (constant) then this becomes a more complicated problem which is solved by writing a correctly structured program. 如果数据不是最终的(恒定的),那么这将成为更复杂的问题,可通过编写正确结构的程序来解决。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.