?
Solved

Access to system or user DSN

Posted on 2002-07-23
4
Medium Priority
?
217 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 200 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

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…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month10 days, 6 hours left to enroll

765 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