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

? 歡迎來(lái)到蟲(chóng)蟲(chóng)下載站! | ?? 資源下載 ?? 資源專(zhuān)輯 ?? 關(guān)于我們
? 蟲(chóng)蟲(chóng)下載站

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

?? java學(xué)習(xí)文檔
?? TXT
字號(hào):
作者:spring.z
email: spring.z@elong.com
日期:2001-7-3 9:10:13
Inserting records into the Database with Java Servlets.

Overview :

This article is next in the series of articles about selecting, inserting, updating and deleting records from the database using JDBC. In this article we will learn how to insert records into the database. If you have followed my earlier article about 'Displaying Records from the Database' then this article is not going to be difficult at all. 90% of the code will be same. So if you haven't read that article then I will suggest that you go through that article before starting this one as quite a few important things have been explained in detail there.

How to Insert Records ?

To insert records into the database we will have to learn about another JDBC class, PreparedStatement. Although we can insert records using the Statement class we discussed in the last article, the INSERT operation is less efficient and not optimized at all. PreparedStatement fills that gap and lets us build SQL queries which are compiled and thus more efficient.

Note that not all database vendors support PreparedStatement class but still it is not a bad habit to use this class so that the ones that do support PreparedStatement class get the extra efficiency.

PreparedStatement :

This class like other JDBC classes we have been discussing is present in the java.sql package. This is how you get handle on a PreparedStatement object :

    String sql = "INSERT INTO Names(first_name, last_name) VALUES (?,?)";

    // con is Connection object

    PreparedStatement ps = con.prepareStatement(sql);
    
Connection.prepareStatement() returns a reference to the PreparedStatement object. The only argument to the Connection.prepareStatement() method is an SQL statement containing optional '?' ( question mark ) containing SQL statement.

You should put '?' marks in the statement where you are going to put or change the values, for example in my example above I placed '?' marks at two places where I will put different values depending on the values inserted by the user.

So how to set the values of '?' parameters. You set the values by using a setXxx() methods of PreparedStatement class. setXxx() are over 25 methods whose syntax is setObject(int paramIndex, Object o) where paramIndex is the number of '?' mark from left to right in the SQL statement. For example we will use setString(1, value1) and setString(2, value2) methods to set the value of both parameters to two different values.

    ps.setString(1, "First Name");
    ps.setString(2, "Last Name");
    ps.executeUpdate();
    
Once the parameters are set in the PreparedStatement object, we execute the query using PreparedStatement.executeUpdate() method. You should use PreparedStatement.executeUpdate() for INSERT, UPDATE and DELETE SQL queries and PreparedStatement.executeQuery() for any SQL statement that returns records.

On the next page we make use of PreparedStatement object to develop a user Form page in which a user can enter his first and last name and when presses the 'submit' button the records are inserted into the database using the methods we just discussed.

InsertServlet :

Create a new InsertServlet.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 the InsertServlet.java file :

package com.stardeveloper.servlets.db;

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

public class InsertServlet extends HttpServlet {
    
    public void doGet(HttpServletRequest req, HttpServletResponse res) 
        throws ServletException, IOException {
        
        res.setContentType("text/html");
        PrintWriter out = res.getWriter();
        
        out.print("<html><body>");
                
        out.print("<form action=\"");
        out.print( req.getRequestURI() );
        out.print("\" method=\"post\">");
        out.print("First Name :<br>");
        out.print("<input type=\"text\" name=\"first\"><br>");
        out.print("Last Name :<br>");
        out.print("<input type=\"text\" name=\"last\">");
        out.print("<br><br><input type=\"submit\" value=\" \">");
        out.print("   Insert Record");
        out.print("    <input type=\"submit\" 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><body>");

        out.print("<code><pre>");
        out.println("ID\tFirst Name\tLast Name\n");
        
        // receiving parameters
        
        String first = req.getParameter("first").trim();
        String last = req.getParameter("last").trim();
        boolean proceed = false;
        
        if(first != null && last != null)
            if(first.length() > 0 && last.length() > 0)
                proceed = true;

        // connecting to database

        Connection con;
        Statement stmt;
        ResultSet rs;
        PreparedStatement ps;
        
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            con=DriverManager.getConnection("jdbc:odbc:odbc_exmp");
            
            String sql = "INSERT INTO Names(first_name,");
            sql += " last_name) VALUES (?,?)";
            ps = con.prepareStatement(sql);
            stmt = con.createStatement();
            
            // inserting records
            
            if(proceed) {
                ps.setString(1, first);
                ps.setString(2, last);
                ps.executeUpdate();
            }
    
            // displaying records

            rs = stmt.executeQuery("SELECT * FROM Names");
            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(e);
        } catch (ClassNotFoundException e) {
            throw new ServletException(e);
        } finally {
            try {
                if(rs != null)
                    rs.close();
                if(stmt != null)
                    stmt.close();
                if(ps != null)
                    ps.close();
                if(con != null)
                    con.close();
            } catch (SQLException e) {}
        }

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

For explanation of InsertServlet code above, please proceed to the next page.

Explanation :

Our InsertServlet class extends from HttpServlet class and overrides two methods; doGet() and doPost(). In doGet() we simply display a Form to the user with two input fields for first and last names and two submit buttons, one for inserting and the other one for displaying records.

    String first = req.getParameter("first").trim();
    String last = req.getParameter("last").trim();
    boolean proceed = false;
        
    if(first != null && last != null)
        if(first.length() > 0 && last.length() > 0)
            proceed = true;
In doPost() we retrieve the first and last name values entered by the user using HttpServletRequest.getParameter() method.

Using a double if statement we make sure that we are not entering null values into the database. If user has entered both first and last name then we proceed.

    Connection con;
    Statement stmt;
    ResultSet rs;
    PreparedStatement ps;
We declare the objects we are going to use to interact with the database.

    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    con=DriverManager.getConnection("jdbc:odbc:odbc_exmp");
We load the Sun's JDBC/ODBC driver and establish connection to our database using the DSN 'odbc_exmp'. Notice that this is the same database we used in 'Displaying Records from the Database'. Please consult that article to see the steps of creating such a database and assigning DSN.

    String sql = "INSERT INTO Names(first_name,");
    sql += " last_name) VALUES (?,?)";
    ps = con.prepareStatement(sql);
    stmt = con.createStatement();
We build the SQL statement which we will use to insert records into the database. Next we create the PreparedStatement and Statement objects using Connection object's methods.

    if(proceed) {
        ps.setString(1, first);
        ps.setString(2, last);
        ps.executeUpdate();
    }
Next we set the two '?' mark parameters in our PreparedStatement object and insert the records using PreparedStatement.executeUpdate() method.

    rs = stmt.executeQuery("SELECT * FROM Names");
    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");
    }
We create our ResultSet object by executing the SELECT query. We then iterate through the records and display it to the user. Notice that the new record we just inserted will also be visible to the user during this iteration.

    if(rs != null)
        rs.close();
    if(stmt != null)
        stmt.close();
    if(ps != null)
        ps.close();
    if(con != null)
        con.close();
Close all the objects that we created.

I haven't mentioned try..catch statements that we used to catch different exceptions that may be thrown during opening and closing of database connection.

On the next page I sum up what we learned in this article.

Summary :

In this step by step tutorial we learned what is PreparedStatement class and how to use it to build fast SQL statements. We then moved forward to build a simple Form application in which a user enters his first and last name and these values are inserted into the database. After that all the names entered are displayed to user.

The database we used in this article was a Microsoft Access database 'odbc_exmp.mdb' that we built in 'Displaying Records from the Database' article. The driver we used was JDBC/ODBC driver, this driver comes with Java Development Kit so you don't need to download and install it separately. For more information on how we built the 'odbc_exmp.mdb' database and what are different types JDBC drivers please consult the above mentioned article.

That's it for this article. Kindly post your questions in the Forum. Thanks.

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

?? 快捷鍵說(shuō)明

復(fù)制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號(hào) Ctrl + =
減小字號(hào) Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
麻豆久久久久久| 99精品欧美一区二区三区综合在线| 久久精品一区二区三区不卡 | 一级特黄大欧美久久久| 日韩精品专区在线影院观看| 91亚洲精品久久久蜜桃网站 | 欧美日韩精品一区二区天天拍小说 | 亚洲欧美国产77777| 欧美电影免费观看高清完整版| 91色乱码一区二区三区| 激情综合五月天| 视频一区免费在线观看| 国产精品入口麻豆九色| 精品入口麻豆88视频| 欧美中文字幕一二三区视频| 丁香啪啪综合成人亚洲小说 | 亚洲精品高清在线| 日本一区二区三区dvd视频在线| 宅男噜噜噜66一区二区66| 91在线视频免费91| 国产精品亚洲人在线观看| 日韩激情av在线| 亚洲va国产天堂va久久en| 亚洲欧洲精品一区二区精品久久久 | 国产中文字幕一区| 美女视频免费一区| 日韩精品成人一区二区在线| 亚洲在线一区二区三区| 亚洲色欲色欲www在线观看| 国产亚洲一区二区三区在线观看 | 成人免费毛片片v| 国产精品夜夜爽| 国产精品1区二区.| 国产一区二区精品久久99| 精品一区二区三区影院在线午夜| 视频一区在线视频| 五月婷婷激情综合| 午夜电影久久久| 亚洲18女电影在线观看| 亚洲国产成人tv| 亚洲与欧洲av电影| 亚洲一区二区成人在线观看| 亚洲精品美腿丝袜| 国产精品综合一区二区三区| 国精品**一区二区三区在线蜜桃| 免费在线观看不卡| 美女网站色91| 国产一区二区三区免费观看| 精品一区二区在线视频| 国产精品小仙女| 成人97人人超碰人人99| 色综合一区二区三区| 色网站国产精品| 欧美日韩的一区二区| 91精品黄色片免费大全| 一区二区三区加勒比av| 亚洲动漫第一页| 日本三级韩国三级欧美三级| 麻豆精品一区二区三区| 国产一区二区成人久久免费影院| 国产精品一区二区免费不卡| 国产精品18久久久久| 成人av片在线观看| 欧洲激情一区二区| 日韩欧美综合在线| 久久久精品综合| 日韩毛片在线免费观看| 亚洲成av人片| 国产做a爰片久久毛片| 99国产精品国产精品毛片| 欧美性做爰猛烈叫床潮| 欧美一区二区啪啪| 国产日韩av一区二区| 日韩理论电影院| 人妖欧美一区二区| 成人精品免费看| 欧美日韩久久不卡| 国产日韩av一区二区| 亚洲一区电影777| 国产高清成人在线| 欧美三级欧美一级| 国产日韩欧美综合在线| 香蕉久久一区二区不卡无毒影院| 韩国精品一区二区| 日本道色综合久久| 337p粉嫩大胆噜噜噜噜噜91av| 国产精品乱人伦| 裸体歌舞表演一区二区| 91蜜桃婷婷狠狠久久综合9色| 欧美一区二区三区在线观看| 国产精品久久久久国产精品日日| 午夜精品福利一区二区蜜股av| 国产成人精品免费| 日韩一级片在线播放| 亚洲欧美一区二区在线观看| 日韩vs国产vs欧美| 成人动漫一区二区三区| 精品视频在线看| 亚洲精品一区二区精华| 亚洲国产精品视频| 六月婷婷色综合| av动漫一区二区| 在线播放欧美女士性生活| 久久久噜噜噜久久中文字幕色伊伊 | 99精品久久只有精品| 7777精品伊人久久久大香线蕉| 国产欧美日韩亚州综合| 亚洲www啪成人一区二区麻豆| 欧美日韩综合在线免费观看| 欧美v日韩v国产v| 亚洲视频电影在线| 国产一区欧美二区| 欧美久久一二区| 亚洲天堂中文字幕| 蜜臀av性久久久久av蜜臀妖精| 91影院在线观看| 欧美韩国日本一区| 日韩精品每日更新| 色婷婷精品久久二区二区蜜臀av| 久久看人人爽人人| 日韩精品电影在线| 色欧美日韩亚洲| 亚洲少妇30p| 国产成人精品免费看| 欧美精品 国产精品| 日韩美女久久久| 国产黑丝在线一区二区三区| 日韩精品一区二区三区视频 | 日韩视频免费直播| 亚洲国产精品久久艾草纯爱| 91在线视频网址| 国产精品视频看| 国产乱一区二区| 日韩一二三区不卡| 日韩激情一区二区| 欧美日韩国产系列| 一区二区三区中文字幕在线观看| 国产成人精品一区二区三区四区 | 国产二区国产一区在线观看| 日韩免费视频线观看| 夜夜揉揉日日人人青青一国产精品| 成人黄色小视频| 国产女主播一区| 国产精品456露脸| 26uuu国产电影一区二区| 青青草97国产精品免费观看无弹窗版| 91久久精品一区二区三区| 国产精品电影院| 成人性生交大片免费看中文| 久久久久久一二三区| 国产最新精品精品你懂的| 日韩午夜三级在线| 国产精品影视天天线| 精品国产乱码91久久久久久网站| 首页国产丝袜综合| 日韩午夜激情av| 麻豆成人久久精品二区三区红| 日本精品视频一区二区三区| 天天综合天天做天天综合| 国产精品久久久久久久久免费桃花| 福利一区二区在线| 国产精品久久99| 91网站在线播放| 最新不卡av在线| 欧美日韩国产小视频在线观看| 婷婷开心激情综合| 91麻豆精品国产91久久久更新时间 | 洋洋成人永久网站入口| 欧美主播一区二区三区| 午夜精品一区二区三区三上悠亚| 欧美日韩色综合| 美女免费视频一区二区| 制服丝袜亚洲色图| 成人免费高清在线| 亚洲欧美日韩在线不卡| 在线免费观看日韩欧美| 亚洲va韩国va欧美va精品| 日韩欧美第一区| 韩国毛片一区二区三区| 中文字幕一区二| 欧美日韩综合色| 久久99国产精品尤物| 国产欧美日韩另类视频免费观看| 粉嫩在线一区二区三区视频| 最新国产精品久久精品| 欧美日韩一级片在线观看| 免费在线观看不卡| 国产日韩亚洲欧美综合| 7777精品伊人久久久大香线蕉的| 国产一区二区三区蝌蚪| 国产精品毛片久久久久久久| 欧美视频在线一区二区三区| 老司机精品视频线观看86| 亚洲欧美一区二区三区国产精品| 欧美日韩国产大片| 粉嫩蜜臀av国产精品网站| 亚洲一卡二卡三卡四卡| 久久综合九色欧美综合狠狠| 欧美天堂亚洲电影院在线播放| 理论片日本一区|