Solved

Access to system or user DSN

Posted on 2002-07-23
4
210 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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

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…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…

759 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now