Solved

Access to system or user DSN

Posted on 2002-07-23
4
215 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will learn how to implement Singleton Design Pattern in Java.

726 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