Tuesday, April 20, 2010

What Do You Put In A Wedding Scroll

Servlet + JDBC

This code is based on what is created automatically by choosing as NetBeans 6.8 Web application project type are reported
:
1. the code of the HTML page launcher (Actually JSP code)
2. the code of a servlet to be placed in the file MHS.java.
Note that: a.
you must install the JDBC driver for ODBC on Windows XP with MySql.
b. SQL syntax is tested on MySQL.
c. need to define a DSN called prova3.
d. prova3 must allow access to a database named test that contains the tables and pet pal whose fields may be inferred from a reading of the SQL string and SQL1.
e. Is done use the "?" that allows you to 'condense' an if then else.
f. In the folder should be added to the package source code (Manzione on top of the file. Java)
g. given the constraints of reporting on a blog some html tags in as arguments to out.print () are not reported, ie the html produced by the servlet should be adjusted




/ / place the file index.jsp


/ / write html
\u0026lt;form action = "http://localhost:8080/Quinta/MHS" method = "GET">;
/ / put at least one submit button






/ / define the package code in your source code package
;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet. annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql .*;
import java.util.ArrayList;
import java.util.List;
/
** * * @ author
We
* / @
WebServlet (name = "MHS" urlPatterns = {"/ MHS"}) public class
MHS extends HttpServlet {private
Connection con = null;
private Statement st = null;
private ResultSet rs = null;
private ResultSetMetaData rsmd = null;
private String error = null;
class Database {private

private Database () {
/ / load the
driver try {Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");}
catch (ClassNotFoundException e) {
error = e.getMessage ();}

/ / create the connection

try {con = DriverManager.getConnection ("jdbc: odbc: prova3");
st = con.createStatement ();}

catch (SQLException e) {
error = e.getMessage ();}


try {/ / String sql = "select pal.name, pet.name, pal.dayOfBirth from test.pal INNER JOIN test.pet on PET.`ownCode`=PAL.cf;";
String sql1 = "SELECT * FROM test.pet";
rs = st.executeQuery(sql1);
rsmd = rs.getMetaData();
}
catch(SQLException e){
errore = e.getMessage();
}



}
}
/**
* Processes requests for both HTTP GET and POST methods.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest (HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {


Database db = new Database ();


response.setContentType ("text / html; charset = UTF-8");
PrintWriter out = response.getWriter ();

out.println ("open body and html");
out.println ("Servlet

MHS at" + ((error == null)? "ok": "problem: ". concat (error)) +"
");

nomiColonne List = new ArrayList ();


try {int i = 1;
while (i \u0026lt;= rsmd.getColumnCount ()) nomiColonne.add (rsmd.getColumnName (i ++));} catch (SQLException e) {error = e.getMessage ();}

if (error == null) for (Object columnName: nomiColonne) out.print ("column" + columnName + "
");
else out.print (" error:" + error +"\u0026lt;"/ h1>); out.println

("closing body and html");
/ /







} / /
/ ** * Handles the HTTP
GET method.
* @ param request servlet request * @ param response
servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

/**
* Handles the HTTP POST method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
processRequest (request, response);}


/ ** * Returns
a short description of the servlet.
* @ return a String containing servlet
description * / @ Override

getServletInfo public String () {
return "Short description";
} / /}



0 comments:

Post a Comment