亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频

? 歡迎來到蟲蟲下載站! | ?? 資源下載 ?? 資源專輯 ?? 關于我們
? 蟲蟲下載站

?? beginner using servlets to display, insert and update records in database.(1).txt

?? 我自自己在學習過程找到的一些jsp代碼
?? TXT
字號:
作者:spring.z
email: spring.z@elong.com
日期:2001-7-3 9:08:37
Displaying Records from the Database with Java Servlets.

Overview :

In this article I'll explain each step you need to know to display records from the database using Servlets. The steps for displaying records in JSP pages and Java Beans are almost the same. We will first build a small example Microsoft Access database, create a DSN for it and using JDBC ( Java Database Connectivity ) driver connect with it and display the records from a given table. Since 80% of your time developing applications will be spent on interacting with databases, you should pay utmost importance to this article.

Access Database :

You can use any database of your choice but for this article I will stick with Microsoft Access database on a Windows platform. The steps for creating such a database and assigning a DSN have already been explained in detail in an earlier article, you should consult that article for details. I will only briefly mention it here.

Create a new Access database with the name of 'odbc_exmp.mdb' and create a table 'Names' containing three fields 'ID', 'first_name' and 'last_name' where 'ID' is the primary key :



Go to the control panel and create a new DSN ( Data Source Name ) 'odbc_exmp' for it and point it to the path of your database on your computer.

Populate the 'Names' table with any values like the following so that we can display the records later :



We are finished with the database. Move on to the next page where we create our Java Servlet to display these records on the user screen.

DisplayServlet :

Create a new DisplayServlet.java file in the /APP_NAME/WEB-INF/classes/com/stardeveloper/servlets/db/ folder. Note /APP_NAME/ is the path of your application within your application server, in Tomcat 4.0 /APP_NAME/ will be /CATALINA_HOME/webapps/star/ where 'star' is the name of the application.

Copy and paste the following code into DisplayServlet.java file and compile it :

package com.stardeveloper.servlets.db;

import java.sql.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class DisplayServlet extends HttpServlet {
    
    public void doGet(HttpServletRequest req, HttpServletResponse res) 
        throws ServletException, IOException {
        
        res.setContentType("text/html");
        PrintWriter out = res.getWriter();
        
        out.print("<html><head>");
        out.print("</head><body>");
        
        out.print("<form action=\"");
        out.print( req.getRequestURI() );
        out.print("\" method=\"post\">");
        out.print("<input type=\"submit\" ");
        out.print("value=\" \">  ");
        out.print("Display Records</form>");
        
        out.print("</body></html>");

        out.close();
    }
    
    public void doPost(HttpServletRequest req, HttpServletResponse res) 
        throws ServletException, IOException {
        
        res.setContentType("text/html");
        PrintWriter out = res.getWriter();
        
        out.print("<html><head>");
        out.print("</head><body>");

        out.print("<code><pre>");
        out.print("<font color=green>ID\tFirst ");
        out.println("Name\tLast Name\n</font>");
        
        // debugging info
        
        long time1 = System.currentTimeMillis();

        // connecting to database

        Connection con = null;
        Statement stmt = null;
        ResultSet rs = null;
        
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            con = DriverManager.getConnection("jdbc:odbc:odbc_exmp");
        
            stmt = con.createStatement();
            rs = stmt.executeQuery("SELECT * FROM Names");
            
            // displaying records

            while(rs.next()) {
                out.print(rs.getObject(1).toString());
                out.print("\t");
                out.print(rs.getObject(2).toString());
                out.print("\t\t");
                out.print(rs.getObject(3).toString());
                out.print("\n");
            }

    
        } catch (SQLException e) {
            throw new 
            ServletException("Servlet Could not display records.", e);
        } catch (ClassNotFoundException e) {
            throw new 
            ServletException("JDBC Driver not found.", e);
        } finally {
            try {
                if(rs != null) {
                    rs.close();
                    rs = null;
                }
                if(stmt != null) {
                    stmt.close();
                    stmt = null;
                }                        
                if(con != null) {
                    con.close();
                    con = null;
                }
            } catch (SQLException e) {}
        }
        
        // debugging info
        
        long time2 = System.currentTimeMillis();

        out.print("</pre></code>");
        
        out.print("<p>Search took : ");
        out.print( (time2 - time1) );
        out.print(" ms.</p>");
        
        out.print("<p\"><a href=\"");
        out.print( req.getRequestURI() );
        out.print("\">Back</a></p>");

        out.print("</body></html>");
        out.close();
    }
}
Start your application server and point your browser to http://localhost:8080/star/servlet/com.stardeveloper.servlets.db.DisplayServlet to see the Servlet display records on your computer. To see the demo please move on to the last page of this article.

Move on to the next page where I explain the steps shown above to connect to the database.

Steps of Connecting to Database :

In the previous page we developed a Servlet 'DisplayServlet' which extends HttpServlet class and overrides doGet() and doPost() methods. In the doGet() method we display a Form to the user to click, after which he will be shown records from our 'odbc_exmp.mdb' database. Nothing much to talk about yet. In doPost() method we make a connection to the database and iterate through it's records. This is the method which needs more explanation.

Step 1 : Loading JDBC Driver :

First step to connect to any database using JDBC is to first load a JDBC driver. Once loaded we use methods of this driver to interact with the database. If JDBC driver is not loaded or could not be loaded we cannot connect to the database. For this example we are using Sun's JDBC/ODBC bridge driver though not a production class driver does allow us to connect to any ODBC compliant database on Windows platform. So the first step will be :

    Connection con = null;
    Statement stmt = null;
    ResultSet rs = null;
        
    try {
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        
Notice that we first declare the three objects we will be using, Connection, Statement and ResultSet objects. All of them are found in the java.sql package. We then start a try block to catch any exceptions that may be thrown. Last line is the main step where we dynamically load the JDBC driver using Class.forName() method. sun.jdbc.odbc.JdbcOdbcDriver is the JDBC driver we are using.

Step 2 : Making Connection :

Ok we have loaded the JDBC driver it is time to make connection to the database. We do this using DriverManager.getConnection() method. As an argument to this method we provide 'path' to the database we are going to connect with.

    con = DriverManager.getConnection("jdbc:odbc:odbc_exmp");
The path to our database is 'jdbc:odbc:odbc_exmp'. If you have to substitute some other DSN then replace 'odbc_exmp' with the newer one e.g. 'jdbc:odbc:newDSN'.

Step 3 : Retrieving Records :

We are connected to the database, how to display records? well to display records we will have to create just two more objects, Statement and ResultSet. Statement object encapsulates the SQL query we want to execute and ResultSet object is the real object which carries our records which we want to display.

    stmt = con.createStatement();
    rs = stmt.executeQuery("SELECT * FROM Names");
We create the Statement object using Connection.createStatement() method. Once created we provide it with the SQL query we want to use to retrieve records, which in this case is "SELECT * FROM Names". This query retrieves all records from the 'Names' table. Statement.executeQuery() method returns a ResultSet object containing the records retrieved using that query.

Last 3 steps are explained on the next page.

Step 4 : Displaying Records :

We now have a ResultSet object filled with records retrieved from the database, so how to show them? This is how we iterate through the records :

    while(rs.next()) {
        out.print(rs.getObject(1).toString());
        out.print("\t");
        out.print(rs.getObject(2).toString());
        out.print("\t\t");
        out.print(rs.getObject(3).toString());
        out.print("\n");
    }
Using a while loop we iterate through the records. Notice that ResultSet.next() methods does two things. One that it returns a boolean ( true / false ) value indicating more records are there to be shown and second that it moves the database cursor one row forward so that next time when we call ResultSet.getObject() method we get object from the next row.

Within the while loop we access records for each field of the table 'Names' using ResultSet.getObject(n) method where 'n' is the number of field from left to right. Since our table 'Names' had three fields we use this method thrice in the loop and each time incrementing it by one to show record from each field. We then use Object.toString() method to display a String value back to the user.

Step 5 : Catching Exceptions :

We connected to the database, retrieved records, showed it to the user, now what? catch the exceptions that may be thrown during unforseen situations.

    } catch (SQLException e) {
        throw new 
        ServletException("Servlet Could not display records.", e);
    } catch (ClassNotFoundException e) {
        throw new 
        ServletException("JDBC Driver not found.", e);
    }
Step 6 : Closing Connection :

In the end we close connection to the database.

    finally {
        try {
            if(rs != null) {
                rs.close();
                rs = null;
            }
            if(stmt != null) {
                stmt.close();
                stmt = null;
            }                        
            if(con != null) {
                con.close();
                con = null;
            }
        } catch (SQLException e) {}
    }
We close all the objects we created earlier and trap all the exceptions that might have been thrown.

This is pretty much it. These are all the steps you need to know to display records from the database using JDBC. In the next page I sum up what we have learned in this tutorial.

Summary :

In this tutorial we developed a 'DisplayServlet' which displays records from 'Names' table in 'odbc_exmp.mdb' database. We discussed all the steps of making a connection to the database to closing that connection in detail. Following are the steps we discussed :

Load the JDBC driver 
Make a Connection 
Retrieve Records 
Display Records 
Catch Exceptions 
Close Connection 
Tip :

To establish a connection to any database you need to have a JDBC driver with you. In most cases this will be provided to you by the database vendor e.g. in case of Oracle 8i. But some database providers haven't created JDBC drivers themselves due to political reasons e.g. Microsoft SQL Server, in which case you have to buy JDBC drivers from some other JDBC driver vendor at some price. Quite a few JDBC drivers of open source RDBMS are available for free e.g. MySQL and PostgreSQL.

JDBC driver is of four types ( I, II, III, IV ). Type IV is the fastest of all and you should always look for type IV JDBC driver, drill this into your brain; always use type IV JDBC driver. Almost all RDMS have type IV JDBC drivers available so there is no reason as to why not to use them.

Ok fellows this is it. Kindly post your questions in the Forum. Enjoy!

There is no associated material for download 
Click here to see the demo 

?? 快捷鍵說明

復制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號 Ctrl + =
減小字號 Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
亚洲成人免费视| 亚洲国产一区在线观看| 色婷婷久久一区二区三区麻豆| 亚洲一区二区不卡免费| 久久一区二区视频| 在线免费精品视频| 国产成人精品亚洲日本在线桃色| 一区二区三区精密机械公司| 久久久精品影视| 欧美日韩精品系列| caoporn国产一区二区| 日本欧美久久久久免费播放网| 日本一区二区免费在线| 91精品国模一区二区三区| 成人黄动漫网站免费app| 日韩av一二三| 亚洲图片自拍偷拍| 国产精品久久777777| 欧美第一区第二区| 欧美丰满高潮xxxx喷水动漫| 99天天综合性| 国产精品一级片| 97久久超碰国产精品电影| 狠狠色丁香婷婷综合| 日日骚欧美日韩| 一区二区三区四区激情| 中文字幕日韩欧美一区二区三区| 日韩精品专区在线影院观看| 欧美精品黑人性xxxx| 色伊人久久综合中文字幕| 国产成人精品三级麻豆| 国内不卡的二区三区中文字幕| 亚洲第一搞黄网站| 亚洲欧美一区二区三区国产精品 | 2020国产精品自拍| 欧美另类一区二区三区| 在线影院国内精品| 色天天综合久久久久综合片| 色综合视频在线观看| 成人app软件下载大全免费| 国产福利一区在线观看| 国产夫妻精品视频| 国产剧情一区二区| 国产成人免费网站| 国产成人午夜精品5599| 国产91综合网| av在线播放成人| eeuss影院一区二区三区| 成人精品国产一区二区4080| 成人av资源网站| av不卡免费在线观看| 91麻豆成人久久精品二区三区| 91女人视频在线观看| 色哟哟日韩精品| 欧美日韩免费在线视频| 日韩三级精品电影久久久| 日韩精品一区二区三区在线播放 | 青青草成人在线观看| 日韩成人免费在线| 久久99精品久久久久久国产越南| 久久99国产精品成人| 国产成人a级片| av中文字幕一区| 欧美三级中文字| 欧美不卡123| 日本一区二区三区免费乱视频| 亚洲成a人在线观看| 蜜臀久久久99精品久久久久久| 九色porny丨国产精品| 成人av网址在线| 欧美视频中文字幕| 日韩天堂在线观看| 中文字幕免费不卡在线| 亚洲精品免费在线播放| 日韩av午夜在线观看| 国产成人精品免费看| 日本高清不卡aⅴ免费网站| 欧美一区二区三区免费| 国产人成亚洲第一网站在线播放| 亚洲视频一二三| 日本在线观看不卡视频| 丁香激情综合五月| 欧美亚洲精品一区| 精品不卡在线视频| 国产精品成人免费精品自在线观看| 一区二区高清视频在线观看| 久久99精品国产麻豆不卡| 成人一级黄色片| 欧美色综合网站| ww久久中文字幕| 亚洲综合图片区| 国产精选一区二区三区| 91高清视频在线| 久久―日本道色综合久久| 亚洲影视在线播放| 国产一区二区三区蝌蚪| 欧美影视一区在线| 中文字幕高清不卡| 日韩黄色小视频| 91老师国产黑色丝袜在线| 精品国产一区二区国模嫣然| 亚洲福利一区二区| 国产高清成人在线| 日韩欧美一级精品久久| 亚洲欧美日韩一区二区 | 国产精品丝袜在线| 日韩精品久久理论片| 99精品视频在线免费观看| 欧美精品一区二区三区很污很色的 | 色婷婷激情综合| 久久久亚洲综合| 视频在线观看91| 在线看国产日韩| 国产精品久久久久久久久久免费看 | 国产日韩精品一区二区三区在线| 日本视频一区二区| 色呦呦国产精品| 中文字幕亚洲综合久久菠萝蜜| 国产一区二区三区国产| 欧美一二三在线| 午夜精品久久久久久久99水蜜桃| 色婷婷久久久亚洲一区二区三区| 亚洲国产成人私人影院tom | 欧美日韩亚洲高清一区二区| 日韩毛片在线免费观看| 成人白浆超碰人人人人| 久久久久久免费毛片精品| 美国毛片一区二区三区| 91精品国产综合久久久蜜臀粉嫩| 亚洲一区在线视频观看| 色八戒一区二区三区| 亚洲人成亚洲人成在线观看图片| 成人性生交大合| 久久久不卡网国产精品一区| 国精品**一区二区三区在线蜜桃| 欧美美女视频在线观看| 午夜精品久久久久久久| 欧美另类一区二区三区| 天使萌一区二区三区免费观看| 欧美伊人久久久久久久久影院| 亚洲午夜在线视频| 色噜噜夜夜夜综合网| 一区二区在线电影| 在线一区二区三区做爰视频网站| 亚洲网友自拍偷拍| 欧美麻豆精品久久久久久| 蜜桃精品在线观看| 欧美哺乳videos| 丰满岳乱妇一区二区三区| 日本一区二区免费在线观看视频 | 日韩精品91亚洲二区在线观看| 欧美蜜桃一区二区三区| 日韩和的一区二区| 日韩免费一区二区三区在线播放| 伦理电影国产精品| 精品国产一区二区三区av性色| 国产精品亚洲人在线观看| 国产欧美综合在线观看第十页| 成人激情综合网站| 亚洲猫色日本管| 欧美浪妇xxxx高跟鞋交| 极品尤物av久久免费看| 国产精品天干天干在线综合| 一本色道久久加勒比精品| 亚洲福利电影网| 久久女同互慰一区二区三区| 99久久免费精品| 午夜视频在线观看一区| 精品美女一区二区三区| 成人午夜av电影| 亚洲小少妇裸体bbw| 精品国产乱码久久久久久夜甘婷婷 | 欧美aaa在线| 日本一区二区三区视频视频| 在线看国产日韩| 久久精品国产亚洲高清剧情介绍 | 日本一区二区电影| 日本韩国一区二区| 久久91精品国产91久久小草| 1区2区3区国产精品| 6080亚洲精品一区二区| 国产91精品露脸国语对白| 亚洲人成伊人成综合网小说| 日韩免费成人网| 色综合久久久久久久久| 美女网站视频久久| 中文字幕亚洲综合久久菠萝蜜| 3d动漫精品啪啪一区二区竹菊| 成人做爰69片免费看网站| 视频一区二区国产| 国产精品日韩精品欧美在线| 在线播放视频一区| 夫妻av一区二区| 久久精品国产精品亚洲红杏| 亚洲欧美日韩中文播放| 久久久亚洲欧洲日产国码αv| 欧美亚洲综合久久| eeuss国产一区二区三区| 久久国产精品99精品国产| 亚洲图片有声小说|