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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

jsp database access on web 1

Status
Not open for further replies.

jsolutions

Programmer
Jul 18, 2002
157
US
I have a simple jsp that opens a connection to an Access database, reads the data, formats it and returns the data to a webpage. In testing it originally, I created a DSN for the database called MyDB and referenced it in the DriverManager.getConnnection method.

My Code is listed below and gives the desired results. My question is, how can I do this on a website. What I mean is that I want to put this database on a website along with the jsp and access the jsp from a web browser to yield the same results. in the getConnection statement, how is the database URL structured : "jdbc:eek:dbc:?????".

Also, are there any issues with having the driver on the website? Do I need to upload a specific .jar file?

Finally, if I am missing any other key points about putting a database on a website, other advice is appreciated as well.

Thanks

<code>
<%@ page import=&quot;java.sql.*&quot; %>

<html>
<head>
<title>Course Schedule</title>
</head>

<body background=&quot;danbg1.gif&quot;>
<center>

<h2>Bookmarks</h2>

<p> </p>

<table border=1 cellpadding=5>
<%
try {
Class.forName(&quot;sun.jdbc.odbc.JdbcOdbcDriver&quot;);
Connection con = DriverManager.getConnection( &quot;jdbc:eek:dbc:MyDB&quot; );
java.sql.Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery( &quot;select * from tblCategories&quot; );
ResultSetMetaData rsmd = rs.getMetaData();

int numColumns = rsmd.getColumnCount();
out.println( &quot;<tr>&quot; );
for ( int i = 1; i <= numColumns; i++ ) {
out.println( &quot;<td align=\&quot;center\&quot;><b>&quot; + rsmd.getColumnName( i ) + &quot;</b></td>&quot; );
}
out.println( &quot;</tr>&quot; );
out.println();

while ( rs.next() ) {
out.println( &quot;<tr>&quot; );
for ( int i = 1; i <= numColumns; i++ ) {
out.println( &quot;<td>&quot; + rs.getString( i ) + &quot;</td>&quot; );
}
out.println( &quot;</tr>&quot; );
out.println();
}

rs.close();
stmt.close();
con.close();
}
catch (Exception e) {
out.println( &quot;<tr><td>&quot; + e + &quot;</td></tr>&quot; );
}

%>
</table>

</center>
</body>

</html>
</code>
 
Hi,

You need to create a DSN on the server with the same name and should have your &quot;.mdb&quot; file which has all the data.
As far as the driver you are using JdbcOdbcDriver which is a part of the JDK, so there is no need of any extra .jar file in your classpath.

As far as missing key points I am really not sure, hope some one will answer this question for you.

Cheers,
Venu
 
Hi Venur - thanks for your reply. One issue, this is going to be on an ISP's webserver - not my own. How do I create a DSN on their server or do I have to get them to create one??

Also, is it possible to not use a DSN to access the database on the website?

Thanks for your help.
 
Hi,

You can use DNS Less connection to access the mdb file.

String driverName = &quot;jdbc:eek:dbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=<path_of_mdb_file>&quot;;
Class.forName(&quot;sun.jdbc.odbc.JdbcOdbcDriver&quot;);
connection = DriverManager.getConnection(driver, &quot;username&quot;, &quot;password&quot;);

username and password are optional. As your website is going to host by ISP it is better to copy the mdb file under you webapplication.

Try this.. it should work.

Cheers,
Venu
 
OK Venur - we are getting so close now. Your code got me to the DNSless point I needed to be. Thank you very much. I now only have one question.

In the DBQ=<path_of_mdb_file> statement, I gave it a literal path of C:\\databases\\MyDb.mdb which works perfectly and displays the data. I cannot, however, get it to work when I try a URL.

To test, I am running Java Web Server Dev kit. I figure if I can get it working here, I can then follow the same logic on my web page.

I put my jsp in the webpages folder and the database in the same folder. So, I cahnged the DBQ entry to DBQ=./MyDb.mdb. My thinking is that this should point to the same folder as the jsp and should therefore find MyDB.mdb. However, I receive an error Could not find file '(unknown)'

I am guessing that my URL notation is wrong, but have tried several different variations and cannot get it right.

Any thoughts?? Again thanks for your help.
 
Hi,

Try this...

String currentDir = &quot;&quot;;
File file = new File(&quot;.&quot;);
currentDir = file.getCanonicalPath();
String path_to_mdb = currentDir + &quot;\\MyDb.mdb&quot;;

String driverName = &quot;jdbc:eek:dbc:Driver={Microsoft Access Driver (*.mdb)};DBQ&quot; + path_to_mdb;
Class.forName(&quot;sun.jdbc.odbc.JdbcOdbcDriver&quot;);
connection = DriverManager.getConnection(driver, &quot;username&quot;, &quot;password&quot;);

Cheers,
Venu


 
Hi..

Sorry.. Change the variable name String driverName to driver.

Cheers,
Venu

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top