0

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

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

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. I either get a NULL value or an error. Thanks a lot in advance !

2
  • create ts as a global variable then access Commented Jan 14, 2016 at 10:32
  • Define String ts; after class name then access Commented Jan 14, 2016 at 10:33

3 Answers 3

1

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 )
 }
Sign up to request clarification or add additional context in comments.

Comments

0

You need to create an accessor method within the class and change 'ts' to a global variable. This way, when you instatiate this class, you can simply retrieve it using something like:

MyClass.getStringName();

where 'getStringName()' is something like:

public String getStringName()
{
    return ts;
}

Comments

0

All of your variables are in class methods, not in class area. In Class add variable String, not in class method.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.