Este exemplifica o acesso ao banco de dados dentro da própria página
<%@ page import="java.sql.*" %>
Usado para importar as classes necessárias
<%! ResultSet rs = null;%>
Declara um variável do tipo ResultSet para guardar os resultados de um pesquisa ao banco de dados
rs = s.executeQuery("select * from customer");
Guarda os resultdos da pesquisa em rs
while (rs.next()) {
O método next retorna true enquanto houver registros dentro de rs
<td width="20"><%= rs.getInt(1) %></td>
Acessa o primeiro campo do registro com inteiro
<td width="70"><%= rs.getString(2) %></td>
Acessa o segundo campo comoString
*****
<!-- JSP Directives -->
<%@ page import="java.sql.*" %>
<html>
<head>
<title>Insurance Quoting System</title>
</head>
<body>
<basefont face="Arial">
<!-- JSP Declarations -->
<%! ResultSet rs = null;%>
<!-- JSP Scriptlet -->
<%
try {
Class.forName("org.gjt.mm.mysql.Driver");
Connection db = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/quoting");
Statement s = db.createStatement();
rs = s.executeQuery("select * from customer");
}
catch (Exception e) {
// For now, just report the error to the system log
System.out.println(e.toString());
}
%>
<!-- Template text -->
<table width="550" border="0" align="center">
<tr>
<td bgcolor="#006633">
<div align="center">
<font size="6" color="#FFFFFF"><b>Insurance Quoting System</b></font>
</div>
</td>
</tr>
<tr>
<td>
<p> </p>
<p> </p>
<p align="center"><b>Customers</b></p>
<table width="290" border="0" align="center">
<%
try {
while (rs.next()) {
%>
<!-- JSP Expressions used within template text -->
<tr>
<td width="20"><%= rs.getInt(1) %></td>
<td width="70"><%= rs.getString(2) %></td>
<td width="70"><%= rs.getString(3) %></td>
<td width="40">
<a href="custMaint.jsp?id=<%= rs.getString(1) %>&action=edit">
edit
</a>
</td>
<td width="40">
<a href="custMaint.jsp?id=<%= rs.getString(1) %>&action=delete">
delete
</a>
</td>
<td width="40">
<a href="custMaint.jsp?id=<%= rs.getString(1) %>&action=newQuote">
new quote
</a>
</td>
</tr>
<%
}
}
catch (SQLException e) {
// For now, just report the error to the system log
System.out.println(e.toString());
}
%>
</table>
</td>
</tr>
<tr>
<td>
<p> </p>
<p align="center"><a href="custMaint.jsp?action=add">New Customer</a></p>
</td>
</tr>
</table>
</body>
</html>
Nenhum comentário:
Postar um comentário