锘??xml version="1.0" encoding="utf-8" standalone="yes"?>麻豆传媒在线观看,国产中文字幕在线播放,欧美日韩综合一区http://www.aygfsteel.com/Yang/category/9716.html瀛﹁?鎵浠ヤ慨鎬т篃.瑙?鍚?璦,璨?鎬?鎬т箣鎵鏈変篃.瀛﹀垯姝?鍚﹀垯閭? 澶辮觸鏄洜涓烘斁寮?涓嶆斁寮冨氨涓嶄細鏈夊け璐?zh-cnFri, 02 Mar 2007 11:49:41 GMTFri, 02 Mar 2007 11:49:41 GMT60Using DatabaseMetaDate and ResultSetMetaDatahttp://www.aygfsteel.com/Yang/articles/40073.html椋炴壃椋炴壃Sun, 09 Apr 2006 03:48:00 GMThttp://www.aygfsteel.com/Yang/articles/40073.htmlhttp://www.aygfsteel.com/Yang/comments/40073.htmlhttp://www.aygfsteel.com/Yang/articles/40073.html#Feedback0http://www.aygfsteel.com/Yang/comments/commentRss/40073.htmlhttp://www.aygfsteel.com/Yang/services/trackbacks/40073.html 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:


椋炴壃 2006-04-09 11:48 鍙戣〃璇勮
]]>
Basic JDBC Examplehttp://www.aygfsteel.com/Yang/articles/40063.html椋炴壃椋炴壃Sun, 09 Apr 2006 02:42:00 GMThttp://www.aygfsteel.com/Yang/articles/40063.htmlhttp://www.aygfsteel.com/Yang/comments/40063.htmlhttp://www.aygfsteel.com/Yang/articles/40063.html#Feedback0http://www.aygfsteel.com/Yang/comments/commentRss/40063.htmlhttp://www.aygfsteel.com/Yang/services/trackbacks/40063.html T his exaple using JDBC-ODBC bridge drive program.Using table NorthwindTest.
NorthwindTest.java

package skyey.snow;
import java.sql.*:

/** A JDBC example that connects to the Microsoft Acess sample Northwind database,issues a simple SQL query to the
聽 *聽聽employee table, and prints the results.
*/

public class NorthwindTest {
聽聽聽 public static void main(String[] args)
聽聽聽 {
聽聽聽聽聽聽聽 String driver="sun.jdbc.odbc.JdbcOdbcDriver";
聽聽聽聽聽聽聽 String url="jdbc:odbc:Northwind";
聽聽聽聽聽聽聽 String username=""; //No username/password required
聽聽聽聽聽聽聽 String password=""; //for desktop access to MS Access.
聽聽聽聽聽聽聽 showEmployeeTable(driver,url,username,passwrd);
聽聽聽聽聽聽聽
聽聽聽聽聽聽聽 /** Query the employee table and print the first and
聽聽聽聽聽聽聽聽 *聽 last names.
聽聽聽聽聽聽聽聽 */

聽聽聽聽聽聽聽 public static void showEmployeeTable(String driver,String url,String username,String password)
聽聽聽聽聽聽聽 {
聽聽聽聽聽聽聽聽聽聽聽 try
聽聽聽聽聽聽聽聽聽聽聽 {
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 //load database driver if it's not already loaded.
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 Class.forName(driver);
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 //Establish network connection to database.
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 Connection connection=
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 DriverManager.getConnection(url,username,password);
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 System.out.println("Employees\n"+"=============================");
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 //Create a Statement for executing queries.
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 Statement statement=connection.clearWarnings();
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 String query=
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 "select firstName,lastName form employee";
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 //send query to database and store results.
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 ResultSet resultSet=statement.executeQuery(query);
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 //print results.
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 while(resultSet.next())
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 {
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 System.out.println(resultSet.getString("firstName")+"");
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 System.out.println(resultSet.getString("lastName")+"");
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 }
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 connection.close();
聽聽聽聽聽聽聽聽聽聽聽 }
聽聽聽聽聽聽聽聽聽聽聽 catch(ClassNotFoundException e)
聽聽聽聽聽聽聽聽聽聽聽 {
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 System.err.println("Error loading driver:"+e);
聽聽聽聽聽聽聽聽聽聽聽 }
聽聽聽聽聽聽聽聽聽聽聽 catch(SQLException e1)
聽聽聽聽聽聽聽聽聽聽聽 {
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 System.err.println("Error with connection:"+e1);
聽聽聽聽聽聽聽聽聽聽聽 }
聽聽聽聽聽聽聽 }
聽聽聽 }
}

from example we should konw how to write a simpleness class for Database connection,and in this example i find a very good method we should study that is we should connection two or much more class using :
public class NorthwindTest {
聽聽聽 public static void main(String[] args)
聽聽聽 {
聽聽聽聽聽聽聽 String driver="sun.jdbc.odbc.JdbcOdbcDriver";
聽聽聽聽聽聽聽 String url="jdbc:odbc:Northwind";
聽聽聽聽聽聽聽 String username=""; //No username/password required
聽聽聽聽聽聽聽 String password=""; //for desktop access to MS Access.
聽聽聽聽聽聽聽 showEmployeeTable(driver,url,username,passwrd);
聽聽聽聽聽聽聽
聽聽聽聽聽聽聽 /** Query the employee table and print the first and
聽聽聽聽聽聽聽聽 *聽 last names.
聽聽聽聽聽聽聽聽 */

聽聽聽聽聽聽聽 public static void showEmployeeTable(String driver,String url,String username,String password)
聽聽聽聽聽聽聽 {}
if we do like this our codes will became short,simpleness and beautiful.

椋炴壃 2006-04-09 10:42 鍙戣〃璇勮
]]>
主站蜘蛛池模板: 博湖县| 黄大仙区| 高淳县| 扶绥县| 台前县| 左云县| 清远市| 军事| 望奎县| 赤壁市| 蕉岭县| 页游| 共和县| 顺义区| 安泽县| 昌图县| 大悟县| 安阳市| 平邑县| 长子县| 察隅县| 岑溪市| 仙游县| 博客| 镇平县| 定兴县| 德令哈市| 中江县| 应用必备| 江安县| 乌兰浩特市| 尖扎县| 垦利县| 玉溪市| 天峻县| 务川| 贵德县| 库尔勒市| 江阴市| 彭泽县| 丰县|