example for using DatabaseMetaData and ResultSetMetaData
package coreservlet;

import java.io.*;
import java.sql.*;

import javax.servlet.*;
import javax.servlet.http.*;

/**
 *
 * @author DuYang
 * @version
 */
public class NorthwindServlet extends HttpServlet {
   
    /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     */
   
    /** A simple servlet that connects to a database and presents the results from the query in an
     *  HTML table.Ther driver,URL,username,,passwrd, and query are taken from input prarmeters.
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out=response.getWriter();
        String docType=
                "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 "+
                "Transitional//EN\"\n";
        String title="Northwind Results";
        out.println(docType+
                "<HTML>\n"+
                "<HEAD><TITLE>"+title+"</TITLE></HEAD>\n"+
                "<BODY BGCOLOR=\"#FDF5E6\"><CENTER>\n"+
                "<H1>Database Results</H1>\n");
        String driver=request.getParameter("driver");
        String url=request.getParameter("url");
        String username=request.getParameter("username");
        String password=request.getParameter("password");
        String query=request.getParameter("query");
        showTable(driver,url,username,password,query,out);
        out.println("</CENTER></BODY></HTML>");
    }
        public void showTable(String driver,String url,
                                 String username,String password,
                                 String query,PrintWriter out)
        {
           try
            {
                //load database driver if it's not already loaded.
                Class.forName(driver);
                //Establish netword connection to database.
                Connection connection=
                        DriverManager.getConnection(url,username,password);
                //Look up info about the database as a whole.
                DatabaseMetaData dbMetaData=connection.getMetaData();
                out.println("<UL>");
                String productName=
                        dbMetaData.getDatabaseProductName();
                String productVersion=
                        dbMetaData.getDatabaseProductVersion();

                out.println("<LI><B>Database:</B>"+productName+
                              "<LI><B>Version:</B>"+productVersion+"</UL>");
                Statement statement=connection.createStatement();
                //Send query to database and store queries.
                ResultSet resultSet=statement.executeQuery(query);
                //Print result.
                ResutlSetMetaData resultSetMetaData=
                        resultSet.getMetaData();
                int columCount=resultSetMetaData.getColumnCount();
                out.println("<TR>");
                //Column index statrs at 1(a la SQL), not 0(a la Java).
                for(int i=1;i<columnCount;i++)
                {
                    out.println("<TH>"+resultSetMetaDate.getColumnName(i));
                }
                out.println();
                //Step through each row, retrieving the data in each
                // column cell as String.
                for(int i=1;i<columnCount;i++)
                {
                    out.println("<TD>"+resultSet.getString(i));
                }
             out.println();
            }
             out.println("</TABLE>");
             connection.close();
        }
        catch(ClassNotFoundException e)
        {
            System.err.println("Error loading driver:"+e);
        }
        catch(SQLException e1)
        {
            System.err.println("Error connecting:"+e1);
        }
        catch(Exception ee)
        {
            System.err.println("Error with input:"+ee);
        }
    }
    private static void showResults(ResultSet results)throws SQLException
    {
        while(results.next())
        {
            System.out.println(results.getString(1)+"");
        }
        System.out.println("");
    }
    public static void printUsage()
    {
        System.out.println("Usage:PreparedStatements host"+"dbName username password"+
                "vendow [print].");
    }
 }

NorthwindForm.htm

Query Input:

Driver:
URL:
Username:
Password:
Query:
posted on 2006-04-09 11:48 飞扬 阅读(284) 评论(0)  编辑  收藏 所属分类: Sound Code

只有注册用户登录后才能发表评论。


网站导航:
 

统计

FAQ网站

JSP/Java/Servlet开发工具

JSP/Java/Servlet技术著名网站

JSP/Servlet应用程序站点

优选博客