jeudi 14 janvier 2016

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

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 !

Aucun commentaire:

Enregistrer un commentaire