|
 |
|
Dev Hardware Forums
> SOFTWARE
> Programming
|
how to connect jsp with Microsoft SQL Server
Discuss how to connect jsp with Microsoft SQL Server in the Programming forum on Dev Hardware. how to connect jsp with Microsoft SQL Server Programming forum discussing administration and coding scripts in languages including PHP, C++, C, Perl, Java, Visual Basic, XML, HTML, CGI, ActionScript, CSS, JavaScript, Fortran, BASH, and issues related to databases and even batch files.
|
|
 |
|
|
|
|

Dev Hardware Forums Sponsor:
|
|
|

May 28th, 2004, 01:58 AM
|
|
n00b DevH'er
|
|
Join Date: May 2004
Posts: 5
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
how to connect jsp with Microsoft SQL Server
Can anyone please teach me how to make a connection between jsp file and Microsoft SQL Server database? I'm using Windows XP and I found that my computer do not have JDBC. Is this mean that I have to install something in order to make the connection?
If possible, please show me some example so that I can understand better..Thanks a lot.
Regards,
san san
|

May 31st, 2004, 10:07 PM
|
|
n00b DevH'er
|
|
Join Date: May 2004
Posts: 1
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
First, you need a JDBC driver that supports SQL Server.
jTDS is an excellent open source 100% pure Java (type 4) JDBC 2.1 driver for the Microsoft SQL Server
series (6.5, 7.x and 2000) and Sybase 10.
I've used it with good results in production systems.
Get it at http://jtds.sourceforge.net/
Since it's a type 4 driver, you won't require any additional client-side libraries or .dll's.
Use the latest released .jar, e.g. jtds-0.7.1.jar (or the latest release candidate if you're feeling
adventurous--OK for development, but wouldn't use an rc version for production).
You'll need to make sure the .jar is on the CLASSPATH for your JSP host--Jetty,
Tomcat, whatever you're using.
With that, it's just standard JDBC programming. Write a little stub test program in straight Java
to make sure your driver setup is working, and you're off to the races.
That done, it's easy enough to access within JSP's, although it violates every precept of good
multi-tiered design. JSP's are presentation layer and shouldn't be accessing the DB directly
unless it's a one-off utility or quick hack.
There are a number of ways, including via custom tags. Including your JDBC code in
a scriptlet block (e.g. between <% and %>) is easy enough, though hideous. You'll also
need to import whatever classes you need from java.sql, e.g.
<%@ page import="java.sql.*" %>
A less hideous way of accessing JDBC from JSP's (possibly an oxymoron, but we'll go with it)
would be to wrap the JDBC accessor code in methods within a declarations block, with is for
code that will be outside the _jspService method, in the servlet generated from the JSP, e.g.
<%!
public String someJdbcWhatnot(HttpServletRequest req, HttpServletResponse resp) {
// whatever
}
%>
and then call it from scriptlet code, e.g.
<%
String myOutput = someJdbcWhatnot(request, response);
...
%>
This at least localizes the damage, and can actually be a convenient way to
rapid prototype code within an app server, by taking advantage of the auto-recompile
of JSP's into servlets. We do this all the time with JBoss, using an exploded .ear.
With an enterprise app, it saves you from redeploying (much less restarting the app server)
when you're just working on the presentation output from some JSP's. Presentation with
a small amount of embedded logic is the raison d'etre for JSP's. Don't fall into the antipattern
of mixing in a bunch of Java code with the HTML. Putting JDBC code in JSP's is the software
equivalent of cutting a hole in the floor to get downstairs--it's not the way to go.
|
Developer Shed Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|