简体   繁体   English

尝试使用在新文件的其他.java文件中已经声明的其他变量

[英]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.

相关问题 我有不同版本的javac和java。 我正在尝试在终端上运行.java文件。 但是它可以在日食中工作 - I have different versions of javac and java. I'm trying to run my .java file on terminal. But it works in eclipse 使用 android 中不同 Java class 中声明的变量 - Use variable declared in different Java class in android 尝试从 war 文件启动新应用程序时出现 java.net.BindException: Address already in use - Trying to start a new app from war file I get java.net.BindException: Address already in use Java-如何使用文本文件中声明的变量 - Java - How to use a variable declared in a text file 我的类fileds正在初始化可能是因为我有一个@bean用于同一个类,但是在一个不同的.java文件中 - My class fileds are getting initialized may be because I have a @bean for the same class but in a different .java file 无法加载 JVM DLL。因为我已经声明了 JAVA_HOME 变量 - Failed to load JVM DLL. As i have already declared JAVA_HOME Variable 我如何在java中使用来自不同文件的类? - How do i use a class from a different file in java? 试图在不同的时间有不同的成本 - Java - Trying to have different costs for different time - Java 如何在不同的Java文件中使用setter和getters - How to use setters and getters in different java file 为什么 Java 文件和 Jar 文件有不同的回报? - Why would a Java file and Jar file have different returns?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM