Encontrei o texto abaixo hoje no Java Developer Connection da Sun , foi a pergunta da semana selecionada naquele site. N�o testei e n�o tenho ambiente JSP de teste no momento, estou repassando o que encontrei no site e acho que vai servir.
Espero que o Ingl�s n�o seja problema para voc�.
Arnaldo
Question of the Week No. 73
Researched by Joe Mocker
Question of the Week presents answers to key questions posed by the developer community. The intent is to pass this important, but not always easy-to-find, information on to JavaTM Developer ConnectionSM (JDC) members. The questions are selected from the {HYPERLINK "http://forum.java.sun.com"}JDC Forums generally because: they are frequently asked, they are significant or timely, or the answers are not easily accessible.
{HYPERLINK "/developer/qow/archive/index.html"}Question of the Week Archive
Topic: How can JDBCTM be accessed from JavaServer PagesTM?
Question:
I want to know how to access a database from a JSP? Can I use the JDBC coding inside the scriptlets, or does the JDBC code have to be done in Beans, where I am handling the data?
Answer:
You can directly access databases from within your .jsp file without resorting to a Bean. Here's how:
First, make sure your web server is configured to load the appropriate JDBC driver classes. If you can't do that (because you're not the web administrator, for example), you can do it from the page by writing the appropriate Class.forName
trick:
<%! Class.forName("my.appropriate.Driver"); %>
Next, import the SQL classes in the page tag:
<%@ page ... import="java.sql.*" %">
Then, create the database connection using the JSP declaration tag:
<%! Connection connection =
DriverManager.getConnection(...) %>
Finally, make a query and display the results:
<% Statement statement =
connection.createStatement();
ResultSet resultSet =
statement.executeQuery("select ..."); %>
<P> The results are:
</P>
<UL>
<% while (resultSet.next()) { %>
<LI><%= resultSet.getString(1) %></LI>
<% } >
</UL>
Don't forget to add an error page so SQL errors will get handled cleanly!
So, as you can see, it's certainly possible. However, as database schemas change and system architectures evolve, you'll find that it's probably better in the long run to wrap up such database access within entity classes and use intermediate Beans to access them from your .jsp file.
Good luck.
> On Mon, 24 Jan 2000, Claudio Miranda wrote:
>
> Caros, tenhos algumas rotinas de acesso a BD, em servlets, no
> entanto
> estou mudando algumas para JSP, em servlet utilizo os metodos service
> ou doGet para enviar os dados para o browser, mas como faco em JSP?,
> nos exemplos jah utilizei os metodos jspInit e jspDestroy, nao sei
> onde colocar as chamadas as conexos ao BD, receber parametros,
> realizar outras manipulacoes, procurei algo no jGURU, java.sun.com,
> mas nao encontrei exemplos de JSP + JDBC, se alguem puder me ajudar
> com links ou docs.
>
> Muito obrigado.
exit end Arnaldo Salaroli Rugai Diretor de Tecnologia - Fauna Inform�tica s/c Ltda [EMAIL PROTECTED] http://www.inbrapenet.com.br/fauna/ * Para n�o receber mais e-mails desta lista envie um e-mail para [[EMAIL PROTECTED]] e no corpo do email escreva [unsubscribe
