Solved

Access to system or user DSN

Posted on 2002-07-23
4
213 Views
Last Modified: 2008-02-01
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
Comment
Question by:Dewaana
  • 2
  • 2
4 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 7172795
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
 

Author Comment

by:Dewaana
ID: 7172900
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
 
LVL 86

Accepted Solution

by:
CEHJ earned 50 total points
ID: 7173126
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
 

Author Comment

by:Dewaana
ID: 7174472
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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question