Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 219
  • Last Modified:

Access to system or user DSN

Hi gentelmen
I was just wondering if it is possible to write a java program that will let me create or enter a data source in the system or user DSN. some thing where i specify the data source and the drive that i want to use, and this is saved in the user and system data source.
if it is possible could somebody please give me some example how it can be done.
Thanks

D
0
Dewaana
Asked:
Dewaana
  • 2
  • 2
1 Solution
 
CEHJCommented:
You'd either have to use MS-specific classes or write a JNI wrapper around some native code. Why do you want to do it in Java? Is it part of a larger Java project or just a stand-alone utility?
0
 
DewaanaAuthor Commented:
it is part of a larger java project. A database needs to be installed and configured, and we wanted to see the possibilty of doing automatically rather than trying to do it manually every time software is installed on some machine.

regards
0
 
CEHJCommented:
This, which I wrote to wrap someone else's code, should do the job. Should be self-explanatory, but let me know if you need further explanation. And let me know how you get on!

/**
 * A wrapper to jWinregistry, a class to read and
 * write values to and from the Windows registry
 * by Jim Williams. jWinregistry was obtained from
 * http://www.algorekiss.com/wsnE682.html. The
 * intructions on the creation of the DSN were found
 * at http://support.microsoft.com/default.aspx?scid=KB;EN-US;q184608
 */

import jwinregistry.jWinregistry;

public final class CreateDsn {
  private static String KEY_STUB = "HKEY_LOCAL_MACHINE\\SOFTWARE\\ODBC\\ODBC.INI\\";

  public static void main(String[] args){
    if(args.length < 7){
     System.out.println("Usage: java CreateDsn <DataSourceName> <DatabaseName> <Description> <DriverPath> <LastUser> <Server> <DriverName>");
     System.exit(-1);
    }
    create(args[0],args[1],args[2],args[3],args[4],args[5],args[6]);
  }

  public static void create(String DataSourceName,
                            String DatabaseName,
                            String Description,
                            String DriverPath,
                            String LastUser,
                            String Server,
                            String DriverName){
    try {
      /**
       * Create the registry class. This causes data to be written to stdout
       * which you might have to swallow.
       */
      jWinregistry reg = new jWinregistry();
      /**
       * Create the new key from the datasource name and write the database
       * name to it
       */
      reg.WriteLocalMachineStringValue(KEY_STUB + DataSourceName,"DatabaseName",DatabaseName);
      /**
       * Now write the remaining values, as given below, to this key
       */
      reg.WriteLocalMachineStringValue(KEY_STUB + DataSourceName,"Description",Description);
      reg.WriteLocalMachineStringValue(KEY_STUB + DataSourceName,"DriverPath",DriverPath);
      reg.WriteLocalMachineStringValue(KEY_STUB + DataSourceName,"LastUser",LastUser);
      reg.WriteLocalMachineStringValue(KEY_STUB + DataSourceName,"Server",Server);
      reg.WriteLocalMachineStringValue(KEY_STUB + DataSourceName,"DriverName",DriverName);
    }
    catch(Exception e){
      e.printStackTrace();
    }
  }
}

/*
   This data appears in the following url, about programmatically writing
   a DSN to the registry: http://support.microsoft.com/default.aspx?scid=KB;EN-US;q184608

   DataSourceName = "<the name of your new DSN>"
   DatabaseName = "<name of the database to be accessed by the new DSN>"
   Description = "<a description of the new DSN>"
   DriverPath = "<path to your SQL Server driver>"
   LastUser = "<default user ID of the new DSN>"
   Server = "<name of the server to be accessed by the new DSN>"
   DriverName = "<name of the driver>"
*/
0
 
DewaanaAuthor Commented:
Thanks man, i will work with this and see how things go. And will get back to u as soon as possible.
And thank u very much
Regards
D
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now