connecting to a database

J

Justin

I am designing a medical application, since I am a programming novice,
its a startup company with no capital, and I want to keep it easy to
install at Beta sites, I would like to use an Access Database to store
information.

So I really dont know how this stuff is done, but I was thinking about
having a class devoted to exchanging information between the Java
application and the Access DB. I envision a class calling a method
inside the databaseClass and passing some parameters like table names
and a DefualtListModel or an ArrayList to deposit information, or a
table name and a query field entry to retrieve information.

However, I have no idea how to set up a connection. I have spent about
2 hours tonight googling, all I can find are either super small
snippets of code that do me no good, or code that when I cut and paste
into netBeans, lights my screen up red.

So I was wondering if anyone out there had some working code for a
connection to an Access Database that they would be willing to post.
As I've said before, I learn best buy doing, so if I could just get a
start, I could most likely figure everything else on my own.
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Justin said:
I am designing a medical application, since I am a programming novice,
its a startup company with no capital, and I want to keep it easy to
install at Beta sites, I would like to use an Access Database to store
information.

So I really dont know how this stuff is done, but I was thinking about
having a class devoted to exchanging information between the Java
application and the Access DB. I envision a class calling a method
inside the databaseClass and passing some parameters like table names
and a DefualtListModel or an ArrayList to deposit information, or a
table name and a query field entry to retrieve information.

However, I have no idea how to set up a connection. I have spent about
2 hours tonight googling, all I can find are either super small
snippets of code that do me no good, or code that when I cut and paste
into netBeans, lights my screen up red.

So I was wondering if anyone out there had some working code for a
connection to an Access Database that they would be willing to post.
As I've said before, I learn best buy doing, so if I could just get a
start, I could most likely figure everything else on my own.

Java + Access = problems

There are no real JDBC driver for MS Access. Only the
JDBC ODBC bridge. And that is not good.

The syntax to connect is:

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con =
DriverManager.getConnection("jdbc:eek:dbc:;Driver={Microsoft Access Driver
(*.mdb)};Dbq=C:\\somedir\\something.mdb;Uid=xxxx;Pwd=yyyy;");

Arne
 
L

Lionel

Justin said:
I am designing a medical application, since I am a programming novice,
its a startup company with no capital, and I want to keep it easy to
install at Beta sites, I would like to use an Access Database to store
information.

I've written a medical application also, I used MySQL to ensure
cross-platform. You can install MySQL using the silent install option
and then do some config automatically to make it easy for the user.
Either way . . .

So I really dont know how this stuff is done, but I was thinking about
having a class devoted to exchanging information between the Java
application and the Access DB. I envision a class calling a method
inside the databaseClass and passing some parameters like table names
and a DefualtListModel or an ArrayList to deposit information, or a
table name and a query field entry to retrieve information.

I have a Singleton class that acts as an interface to the database. It
provides methods like

public static void addPatient(Patient patient) {...}

That approach is very programmer friendly. There is also hibernate which
is even better. It wasn't around when I started or I didn't know about
it but it's very good.

However, I have no idea how to set up a connection. I have spent about
2 hours tonight googling, all I can find are either super small
snippets of code that do me no good,

You're googling skills might be broken as my first result was:
http://www.javaworld.com/javaworld/javaqa/2000-09/03-qa-0922-access.html

You will need to scroll down to the resources section at the bottom.
There are links to a Sun tutorial, if you spend the time with these you
will be able to learn it. Make sure you get the driver or nothing will
work. I can't give detailed info about Access. To make things easy you
probably want to manually create a database first so that you have
something to connect to.


or code that when I cut and paste
into netBeans, lights my screen up red.

Well some of the errors would be good to help use help you. It's most
likely because you don't have the correct jar files. These are the
drivers. Please post the error messages here after reading the above.

Lionel.
 
J

Justin

I think, on your recommendation, ill just try mySQL. I am downloading
it now.

I had seen that page, however, this is really the first time I have
written any code since my intro to computer science course I took 4
years ago. Didnt understand OOP then, it has recently kinda clicked
together, although, when it comes to inputting and outputting, I have
never written any code like that. So I just see those snippets and it
looks like a foreign language. I've been spoiled with connecting to
databases from using MS InfoPath. haha. Anyways, any chance you could
post that class that interfaces to the databases?
 
F

Furious George

Justin said:
I am designing a medical application, since I am a programming novice,
its a startup company with no capital, and I want to keep it easy to
install at Beta sites, I would like to use an Access Database to store
information.

Please don't do this. Since you are a novice, you will inevitably make
mistakes. That is OK unless you are working on highly critical
projects like medical applications. So please do your learning on
another type of project.
So I really dont know how this stuff is done, but I was thinking about
having a class devoted to exchanging information between the Java
application and the Access DB. I envision a class calling a method
inside the databaseClass and passing some parameters like table names
and a DefualtListModel or an ArrayList to deposit information, or a
table name and a query field entry to retrieve information.

However, I have no idea how to set up a connection. I have spent about
2 hours tonight googling, all I can find are either super small
snippets of code that do me no good, or code that when I cut and paste
into netBeans, lights my screen up red.

So I was wondering if anyone out there had some working code for a
connection to an Access Database that they would be willing to post.
As I've said before, I learn best buy doing, so if I could just get a
start, I could most likely figure everything else on my own.

I have created a toy Access Database application. It does not do
anything great and it does not follow the best programming practices,
but it does include working code for a connection to an Access
database. To make it work, you must use the ODBC manager to register
the youraccessdb database.

(1) Since your client must also register the database with the ODBC
manager, I do not consider it easy to install.
(2) If you use a properties.txt file like I did, then it would be
trivial for you to upgrade to a real database when you realize that
Access is only appropriate for toy applications (like mine) and
definitely not highly critical applications (like yours).

=====<SimpleDBApplication.java>=====
public class SimpleDBApplication
{
public static final void main ( final java . lang . String [ ] args
) throws java . lang . Exception
{
final java . sql . Connection connection = getConnection ( ) ;
try
{
application ( connection ) ;
}
finally
{
connection . close ( ) ;
}
}

/**
* Create a file called properties.txt with driverClassName
(sun.jdbc.odbc.JdbcOdbcDriver) and url (jdbc:eek:dbc:youraccessdb).
**/
private static final java . sql . Connection getConnection ( )
throws java . io . IOException , java . lang . ClassNotFoundException ,
java . sql . SQLException , java . lang . InstantiationException , java
.. lang . IllegalAccessException
{
final java . util . Properties properties = new java . util .
Properties ( ) ;
final java . lang . Class thisClass = SimpleDBApplication .
class ;
final java . io . InputStream propertiesStream = thisClass .
getResourceAsStream ( "properties.txt" ) ;
properties . load ( propertiesStream ) ;
propertiesStream . close ( ) ;
final java . lang . String driverClassName = properties .
getProperty (
"driverClassName" ) ;
final java . lang . Class driverClass = java . lang . Class .
forName (
driverClassName ) ;
final java . lang . Object driverObject = driverClass .
newInstance ( )
;
final java . sql . Driver driver = ( java . sql . Driver ) (
driverObject ) ;
java . sql . DriverManager . registerDriver ( driver ) ;
final java . lang . String url = properties . getProperty (
"url" ) ;
final java . sql . Connection connection = java . sql .
DriverManager .
getConnection ( url , properties ) ;
java . sql . DriverManager . deregisterDriver ( driver ) ;
return ( connection ) ;
}

private static final void application ( final java . sql .
Connection connection ) throws java . sql . SQLException
{
final java . sql . DatabaseMetaData metadata = connection .
getMetaData
( ) ;
final boolean supportsTransactions = metadata .
supportsTransactions ( ) ;
java . lang . System . out . println (
"supportsTransactions:\t" + supportsTransactions ) ;
final java . lang . String sqlKeywords = metadata .
getSQLKeywords ( ) ;
java . lang . System . out . println ( "sqlKeywords:\t" +
sqlKeywords ) ;
printTypeInfo ( metadata ) ;
}

private static final void printTypeInfo ( final java . sql .
DatabaseMetaData metadata ) throws java . sql . SQLException
{
final java . sql . ResultSet typeInfo = metadata . getTypeInfo
( ) ;
try
{
printTable ( typeInfo ) ;
}
finally
{
typeInfo . close ( ) ;
}
}

/**
* See http://java.sun.com/j2se/1.4.2/docs/api/index.html
**/
private static final void printTable ( final java . sql . ResultSet
resultSet ) throws java . sql . SQLException
{
final java . sql . ResultSetMetaData metadata = resultSet .
getMetaData
( ) ;
final int columnCount = metadata . getColumnCount ( ) ;
while ( resultSet . next ( ) )
{
for ( int i = 1 ; i <= columnCount ; i ++ )
{
final java . lang . Object object = resultSet .
getObject ( i ) ;
java . lang . System . out . print ( object +
"\t" ) ;
}
java . lang . System . out . println ( ) ;
}
}
}
=====</SimpleDBApplication>=====
=====<properties.txt>=====
driverClassName:sun.jdbc.odbc.JdbcOdbcDriver
url:jdbc:eek:dbc:youraccessdb
=====</properties.txt>=====
 
J

Justin

Thanks for the code. I dont want to give you the wrong impression, I
am under no time constraints. The company consists of myself and a
surgeon from my day job. We've built and refined an InfoPath form over
the course of 4 years, and feel that we have out-grown the usefulness
of InfoPath, so I'm attempting to code it up in java. However,
depending on the performance, we may decide to sell it to other
surgeons. But thanks again for the code...


Furious said:
Justin said:
I am designing a medical application, since I am a programming novice,
its a startup company with no capital, and I want to keep it easy to
install at Beta sites, I would like to use an Access Database to store
information.

Please don't do this. Since you are a novice, you will inevitably make
mistakes. That is OK unless you are working on highly critical
projects like medical applications. So please do your learning on
another type of project.
So I really dont know how this stuff is done, but I was thinking about
having a class devoted to exchanging information between the Java
application and the Access DB. I envision a class calling a method
inside the databaseClass and passing some parameters like table names
and a DefualtListModel or an ArrayList to deposit information, or a
table name and a query field entry to retrieve information.

However, I have no idea how to set up a connection. I have spent about
2 hours tonight googling, all I can find are either super small
snippets of code that do me no good, or code that when I cut and paste
into netBeans, lights my screen up red.

So I was wondering if anyone out there had some working code for a
connection to an Access Database that they would be willing to post.
As I've said before, I learn best buy doing, so if I could just get a
start, I could most likely figure everything else on my own.

I have created a toy Access Database application. It does not do
anything great and it does not follow the best programming practices,
but it does include working code for a connection to an Access
database. To make it work, you must use the ODBC manager to register
the youraccessdb database.

(1) Since your client must also register the database with the ODBC
manager, I do not consider it easy to install.
(2) If you use a properties.txt file like I did, then it would be
trivial for you to upgrade to a real database when you realize that
Access is only appropriate for toy applications (like mine) and
definitely not highly critical applications (like yours).

=====<SimpleDBApplication.java>=====
public class SimpleDBApplication
{
public static final void main ( final java . lang . String [ ] args
) throws java . lang . Exception
{
final java . sql . Connection connection = getConnection ( ) ;
try
{
application ( connection ) ;
}
finally
{
connection . close ( ) ;
}
}

/**
* Create a file called properties.txt with driverClassName
(sun.jdbc.odbc.JdbcOdbcDriver) and url (jdbc:eek:dbc:youraccessdb).
**/
private static final java . sql . Connection getConnection ( )
throws java . io . IOException , java . lang . ClassNotFoundException ,
java . sql . SQLException , java . lang . InstantiationException , java
. lang . IllegalAccessException
{
final java . util . Properties properties = new java . util .
Properties ( ) ;
final java . lang . Class thisClass = SimpleDBApplication .
class ;
final java . io . InputStream propertiesStream = thisClass .
getResourceAsStream ( "properties.txt" ) ;
properties . load ( propertiesStream ) ;
propertiesStream . close ( ) ;
final java . lang . String driverClassName = properties .
getProperty (
"driverClassName" ) ;
final java . lang . Class driverClass = java . lang . Class .
forName (
driverClassName ) ;
final java . lang . Object driverObject = driverClass .
newInstance ( )
;
final java . sql . Driver driver = ( java . sql . Driver ) (
driverObject ) ;
java . sql . DriverManager . registerDriver ( driver ) ;
final java . lang . String url = properties . getProperty (
"url" ) ;
final java . sql . Connection connection = java . sql .
DriverManager .
getConnection ( url , properties ) ;
java . sql . DriverManager . deregisterDriver ( driver ) ;
return ( connection ) ;
}

private static final void application ( final java . sql .
Connection connection ) throws java . sql . SQLException
{
final java . sql . DatabaseMetaData metadata = connection .
getMetaData
( ) ;
final boolean supportsTransactions = metadata .
supportsTransactions ( ) ;
java . lang . System . out . println (
"supportsTransactions:\t" + supportsTransactions ) ;
final java . lang . String sqlKeywords = metadata .
getSQLKeywords ( ) ;
java . lang . System . out . println ( "sqlKeywords:\t" +
sqlKeywords ) ;
printTypeInfo ( metadata ) ;
}

private static final void printTypeInfo ( final java . sql .
DatabaseMetaData metadata ) throws java . sql . SQLException
{
final java . sql . ResultSet typeInfo = metadata . getTypeInfo
( ) ;
try
{
printTable ( typeInfo ) ;
}
finally
{
typeInfo . close ( ) ;
}
}

/**
* See http://java.sun.com/j2se/1.4.2/docs/api/index.html
**/
private static final void printTable ( final java . sql . ResultSet
resultSet ) throws java . sql . SQLException
{
final java . sql . ResultSetMetaData metadata = resultSet .
getMetaData
( ) ;
final int columnCount = metadata . getColumnCount ( ) ;
while ( resultSet . next ( ) )
{
for ( int i = 1 ; i <= columnCount ; i ++ )
{
final java . lang . Object object = resultSet .
getObject ( i ) ;
java . lang . System . out . print ( object +
"\t" ) ;
}
java . lang . System . out . println ( ) ;
}
}
}
=====</SimpleDBApplication>=====
=====<properties.txt>=====
driverClassName:sun.jdbc.odbc.JdbcOdbcDriver
url:jdbc:eek:dbc:youraccessdb
=====</properties.txt>=====
 
L

Lionel

Justin said:
I think, on your recommendation, ill just try mySQL. I am downloading
it now.

I had seen that page, however, this is really the first time I have
written any code since my intro to computer science course I took 4
years ago. Didnt understand OOP then, it has recently kinda clicked
together, although, when it comes to inputting and outputting, I have
never written any code like that. So I just see those snippets and it
looks like a foreign language. I've been spoiled with connecting to
databases from using MS InfoPath. haha. Anyways, any chance you could
post that class that interfaces to the databases?

Drop me an email at the above address but remove the underscore. I'll
see what I can do. I have some constraints regarding what I can release
until we finally release the application under gpl. However, using MySQL
I can provide you with the necessary code and links to the best
resources to get you going. I would strongly suggest you consider
looking into hibernate. Hibernate will hide a lot of the details and
make your databasing experience simpler. Do a google search to find out
more.

Lionel.
 
S

Simon Brooke

Lionel said:
I've written a medical application also, I used MySQL to ensure
cross-platform. You can install MySQL using the silent install option
and then do some config automatically to make it easy for the user.
Either way . . .

If you're doing anything serious, I'd question the MySQL decision; it's not
really a very good database. My view is you should write portable ASCII as
close to the ANSI92 spec as possible. I use a C preprocessor file to
generate different SQL syntaxes; it's a working solution, but not a
wonderful one.

Whichever way you're going you should use some sort of a database
abstraction layer to isolate any database engine specific code.
 
T

Thomas Kellerer

My view is you should write portable ASCII as
close to the ANSI92 spec as possible

Can you elaborate what you mean with "portable ASCII" ????

Thomas
 
I

Ian Wilson

Justin said:
I am designing a medical application, since I am a programming novice,
its a startup company with no capital, and I want to keep it easy to
install at Beta sites, I would like to use an Access Database to store
information.

I'd consider an embeddable pure-Java DBMS like Derby
http://db.apache.org/derby/

There are many alternatives, some people seem to like
http://www.hsqldb.org/ which has a smaller footprint.

However, I have no idea how to set up a connection. I have spent about
2 hours tonight googling, all I can find are either super small
snippets of code that do me no good, or code that when I cut and paste
into netBeans, lights my screen up red.

Sample source code at
http://db.apache.org/derby/papers/DerbyTut/embedded_intro.html

You'll need to download and install Derby first and tell NetBeans about it.
 
F

Furious George

Thomas said:
Can you elaborate what you mean with "portable ASCII" ????

I think he means portable SQL. The more portable the SQL, the easier
it is to change database engines.
 
L

Lionel

Simon said:
I'd question the MySQL decision; it's not
really a very good database.

Can you elaborate on MySQL not being a good database? Google any many
hundred of other enormous users seem to be very happy with it.

Lionel.
 
D

Daniel Dyer

Can you elaborate on MySQL not being a good database? Google any many
hundred of other enormous users seem to be very happy with it.

Lionel.

MySQL is perfectly adequate for many tasks but, because of the licensing,
I rarely use it. Specifically, the JDBC driver is GPL, rather than LGPL
as it used to be, which results in legal ambiguity if you use it for
commercial development or non-GPL open source development (unless you pay
for a separate licence). Bizarrely, I can, without charge, bundle an
Oracle JDBC driver with my non-GPL application but not a MySQL one.

PostgreSQL is a solid product, better in some respects than MySQL, and the
licence is much more agreeable to freeloaders like me.

Dan.
 
J

Justin

first release is just something to get a small customer base so we can
afford to hire employees who really know how to program, as opposed to
me whos taken one 4 credit course in java. I think all we need is a
nice Beta version to release to get some cash flow. Once docs get
their hands on it, they will want it.

So I'm definatly looking for a cheap/free database for short term use
until we have enough capital to build the massive database I have
envisioned.
 
D

Daniel Dyer

first release is just something to get a small customer base so we can
afford to hire employees who really know how to program, as opposed to
me whos taken one 4 credit course in java. I think all we need is a
nice Beta version to release to get some cash flow. Once docs get
their hands on it, they will want it.

So I'm definatly looking for a cheap/free database for short term use
until we have enough capital to build the massive database I have
envisioned.

For ease of install (of your application), an embedded pure Java database,
such as Derby (http://db.apache.org/derby/) or HSQLDB
(http://www.hsqldb.org/), would be a good choice. They will work on any
platform that your application does. I don't know which one is best but I
believe Derby will be included with JavaSE 6.0 when it is released, so it
might be the obvious choice.

Avoid Access. If you want something more heavyweight (and separate from
the application) use PostgreSQL (http://www.postgresql.org). PostgreSQL
will probably be sufficient for the massive database that you envisage
(unless you're talking high-end Oracle "massive").

As for how to connect to the database, you will need to use JDBC, but not
necessarily directly. There are sevaral Object-Relational Mapping (ORM)
tools available for Java that allow you to mostly think in terms of
objects rather than tables or SQL. Currently the most popular ORM library
is Hibernate (http://www.hibernate.org). Hibernate supports several
popular RDBMS products including Derby, HSQLDB, PostgreSQL and MySQL. In
theory, if you use Hibernate, you should be able to switch databases later
with a single configuration change (it's never quite that simple, but is
still reasonably painless).

Dan.
 
J

Justin

lol, thanks. Ya, I'm thinking when this is finished, there will be
10's of thousands of tables. So I am talking a ton. But I'm also
thinking of building different applications that share the same
database. Each application only connecting to the certain tables that
it needs to. But ya, definatly talking about a massive database.

Thanks for all the input.....
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,526
Members
44,997
Latest member
mileyka

Latest Threads

Top