简体   繁体   中英

Accessing berkeley db from another class in Java

I created a database in one of my java class files and was wondering how to access/open that database in another java class file to read through the data. I tried using openDatabase but how does it know the location of the database file? I've searched through many forums and all I could find is having the code in the same class and just accessing the database object.

ex.I created a database at the directory /documents/ in one of my java class files and all my java code is somewhere else. How do I access and use that database in my other source code?

Edit:

public static void main(String[] args) {

    try {

        EnvironmentConfig environmentConfig=new EnvironmentConfig();
        environmentConfig.setAllowCreate(true);
        Environment environment=new Environment(new File("user/documents/"),environmentConfig);
        DatabaseConfig databaseConfig=new DatabaseConfig();
        databaseConfig.setAllowCreate(true);
        Database db=environment.openDatabase(null,"mytable",databaseConfig);
    }
    catch (Exception e) {
        throw new RuntimeException(e);
 }

}

I tried the following and I keep getting this error when compiling.

openDatabase(com.sleepycat.db.Transaction,java.lang.String,java.lang.String,com.sleepycat.db.DatabaseConfig) in com.sleepycat.db.Environment cannot be applied to (<nulltype>,java.lang.String,com.sleepycat.db.DatabaseConfig)
            Database db=environment.openDatabase(null,key,databaseConfig);
                                   ^
1 error

Yeah since it is related to mysql database , that means you have to have a password and username as it is a secure school system , and you can use mysql connector to access the data you created .

To be more clear you have to have database.java file .

then in that file the main thing to know is the constructor and way to go ...
import java.sql.*;

  public class database
 {

   public static database bDatabase = null;
   protected String     connection_url = "";
   protected String     _name    = "";
   protected String     name           = "";
   protected String     user           = "";
   protected String     password       = "";
   protected Class      some_class   = null;
   protected Connection connection     = null;
   protected ResultSet  results        = null;
   protected String     current_table  = "";
   protected Boolean    error          = false;      

   public database(String name, String user, String password)
   {
     this(name, user, password, "jdbc:mysql://localhost:3306",    "com.mysql.jdbc.ClassName");
   }
  public database(String name, String user, String password, String       connection_url, String any_name)
   {
    this.name           = name;
    this.user           = user;
    this.password       = password;
    this.connection_url = connection_url;
    this._name    = any_name;
   }
   public static void openDatabase()
   {
     try
     {
      bDatabase = new database("dbname", "user_id",
          "password", "jdbc:mysql://host",
          "com.mysql.jdbc.Anyclass");
      bDatabase.open();
    }
    catch (Exception ex)
    {
      throw new InvalidQueryException("Unable to open database ");
     }
    }  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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