Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

setup data source through java

Status
Not open for further replies.

la

Programmer
Dec 5, 2004
6
IE
hi all,

this is my first post as i have just joined the tek-tips community. long time reader though, love the site.

i want to set up a data source from within a servlet. the servlet will take in the two variables.

these will be the path to the database and the datasource name.

then another button such as continue will be clicked and the datasource will be created.

all i want is code for the main function of setting up a datasource with a designated name using java.

would really, really appreciate some help on this one.
thanks.
 
This is how to create an InitialContext and retrieve a DataSource (assuming there is one bound).

Code:
  // Obtain a JNDI javax.naming.Context object
  Context ctx = new InitialContext();
  // Retrieve a DataSource object using JNDI
  DataSource ds = (DataSource)ctx.lookup("java:comp/env/myPoolName");
  // Extract a connection from the datasource
  Connection c = ds.getConnection();
  // use the connection as normal (eg)
  // Get a Statement
  Statement s = c.createStatement();
  // Execute a query and get a ResultSet
  ResultSet rs = s.executeQuery("select * from users where clue > 0");
  while (rs.next()) {
     // do something
  }
  // Return the Connection to the pool
  if (rs != null) rs.close();
  if (s != null) s.close();
  if (c != null) c.close();

--------------------------------------------------
Free Database Connection Pooling Software
 
thanks for the reply sedj

the only lines new to me are:

Code:
Context ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup("java:comp/env/myPoolName");
Connection c = ds.getConnection();

maybe it's because i've never user JNDI. would you be so kind as to give me a brief introduction as to what it is and what is its purpose, etc. not necessary, but i would appreciate it.

looking at the code, i take it that my datasource will be called "ds" and will point to a database called "java:comp/env/myPoolName" at that location.

what is the point of "java:"

could you do another example connecting to a MS Access database, e.g. located at "C:\Web\shopping" and setting the datasource name to "shop"

thanks again for your reply sedj.
 
Well, JNDI is the javax.naming package - an API for binding and looking up objects inside a JVM - usually within a J2EE or Servlet container like JBoss, Tomcat or Resin. Look at the tutorials for JNDI at . The "java:" binding is a root binding, for various container specific resources - its all in the JNDI spec.

However, seeing as you mentioned MS Access - I don't think you mean a DataSource as in the JNDI context do you ? You mean a datasource as in setting up your Access *database* in MicroSoft's ODBC manager, and you want to access that via JDBC ? Is this the case ?



--------------------------------------------------
Free Database Connection Pooling Software
 
On the chance that this is the case, this tutorial shows you how to use Access using the JDBC-ODBC bridge :
Plus, you may want to search the forum for "Access JDBC" - there have plenty of posts about it over the ether.

--------------------------------------------------
Free Database Connection Pooling Software
 
hello again.

i had a feeling JNDI is wasn't what i needed. let me try and explain what i want a bit clearer. i know how to use JDBC with databases, odbc, etc.

now, to setup a datasource you have to go to control panel -> administrative tools -> data sources (odbc).

then u would click on "Add" and it would ask you what type of driver you wanted. We would pick for example "Microsoft Access Driver (.mdb)" and click "Finish".

Now we would be presented with a dialogue asking for a "Data Source Name" and we would "Select" the location and name of database that we want it the dtaasource to point to point.

excellent. have we got all that.

now, what i want is to do all this through java. the user would give you a "Data Source Name" and the location and name of the database it should point to.

You would carry out all the required functions to create this datasource.

thank you for your time.
 
Are you asking how to set up Microsft Access databases in the ODBC data source manager using Java, on the fly ?

If you are, then you want to be programming in Visual C++, not Java ...

--------------------------------------------------
Free Database Connection Pooling Software
 
that, my friend, is exactly what i am asking.

the thing is i know it can be done, because a friend of mine did before (using Runtime.exec() or something.

don't ask me about the friend plz :)

it is a very simple task too, only about 7 - 10 lines of code.
 
I guess there's a way to do that from the command line, but don't ask me how, I can't remember it rigth now.

If so, you can write your Java program that takes some arguments, build an String with the command and use the System.exec() to execute it as a command line order.

Cheers,

Dian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top