简体   繁体   English

如何使用另一个类中的字符串变量

[英]How to use a string variable from another class

I would like to access a string variable from another class to put it into an SQlite DB but I get an error "cannot be resolved or not a field Here is the code timestamp.java 我想从另一个类访问一个字符串变量以将其放入SQlite DB中,但出现错误“无法解析或没有字段,这是代码timestamp.java

import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;




import org.apache.commons.io.FileUtils;

   public class timestamp {



public static String doSomething() throws IOException {     


    File source = new File("C:/Users/India/Desktop/Test/Sub/Test.docx");



    SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy HH-mm-ss-ms");

    String ts=sdf.format(source.lastModified());
    System.out.print(ts);
    //String[] TimeStamp = source.lastModified().toString();

    String name = source.getName();
    String ext = name.substring(name.lastIndexOf("."));
    name = name.substring(0, name.lastIndexOf("."));
    String outFileName = name + " " + ts + ext;
    //System.out.println(" new file name is " + outFileName);

    File destination = new File("C:/Users/India/Desktop/Test", outFileName);

    FileUtils.copyFile(source,destination);
    return ts;  
    }

    public static void main (String [] args) throws IOException
    { doSomething();}}

SQLiteJDBC.java SQLiteJDBC.java

import java.io.IOException;
import java.sql.*;

    public class SQLiteJDBC{ 

 public static void InsertValues( String args[] ) throws IOException 
    { timestamp t = new timestamp();
     String var = t.ts;

     System.out.println(t.ts);
     Connection c = null;
Statement stmt = null;
try {
  Class.forName("org.sqlite.JDBC");
  c = DriverManager.getConnection("jdbc:sqlite:test.db");
  c.setAutoCommit(false);
  System.out.println("Opened database successfully");

  stmt = c.createStatement();
  PreparedStatement prep1 = c.prepareStatement ("INSERT INTO ACTIONLOG VALUES (?,?, ?);") ;
  prep1.setString(1, var);

  prep1.addBatch();


  c.setAutoCommit(false);
  prep1.executeBatch();

  c.setAutoCommit(true);



  stmt.close();
  c.commit();
  c.close();
} catch ( Exception e ) {
  System.err.println( e.getClass().getName() + ": " + e.getMessage() );
  System.exit(0);
}
System.out.println("Records created successfully");
 }


    public static void main( String args[] ) throws IOException
    { InsertValues(args);
     }
     }

I am not able to access the ts string in the database class. 我无法访问数据库类中的ts字符串。 I either get a NULL value or an error. 我得到一个NULL值或一个错误。 Thanks a lot in advance ! 在此先多谢!

Since You are Trying to Access Local Variable from Another Class. 由于您正在尝试从另一个类访问局部变量。 better then that go ahead with global variable.update code like that only one line define after class 更好的是继续使用全局变量。更新代码,像这样,仅在课后定义一行

public class timestamp {
 String ts;// Define Here (global variable )
 }

You need to create an accessor method within the class and change 'ts' to a global variable. 您需要在该类中创建一个访问器方法,并将“ ts”更改为全局变量。 This way, when you instatiate this class, you can simply retrieve it using something like: 这样,当您实例化该类时,可以使用类似以下内容的方法简单地检索它:

MyClass.getStringName();

where 'getStringName()' is something like: 其中“ getStringName()”类似于:

public String getStringName()
{
    return ts;
}

All of your variables are in class methods, not in class area. 您所有的变量都在类方法中,而不是在类区域中。 In Class add variable String, not in class method. 在类中添加变量String,而不是在类方法中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM