JSP检索从数据库动态添加选择选项值到文本框onblur()

时间:2017-04-28 05:06:13

标签: jsp

从数据库中检索动态添加的选择选项值到文本框onblur()。我已将值动态添加到select选项中,但现在我想通过使用onBlur()从select选项中检索该值。我可以保存数据,onblur()我可以检索除select选项之外的所有数据。亲切的帮助:))

<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.SQLException"%>
<%@page import="java.io.UnsupportedEncodingException"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Ledger Creation</title>
<link href="ledgercss.css" rel="stylesheet" type="text/css">
<script src="js/jquery-1.6.1.js"></script>
<script>
    $(document).keypress(function(event) {
        if (event.which == '13') {
            event.preventDefault();
        }
    });

    function getVal(newValue) {
        var x = document.getElementById("code").value;
        iframe1.location.href = "getldg.jsp?param=" + x;
    }
</script>
<%!String varmsg = "";
    String varmsg1 = "";
    Connection conn = null;
    String code, ename, kname, asstgrp, libgrp, rpord, schyn, trcd1, sumyn,
            plord, plcd, blord, scyn = "";

    public void connect() {
        try {
            Class.forName("com.mysql.jdbc.Driver");

            conn = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/ledger", "root", "");
            varmsg = "connected";
        } catch (Exception ex) {
            varmsg = ex.getMessage();
        }
    }

    public void gettxt(HttpServletRequest request) {
        code = request.getParameter("code");
        ename = request.getParameter("ename");
        kname = request.getParameter("kname");
        asstgrp = request.getParameter("asstgrp");
        libgrp = request.getParameter("libgrp");
        rpord = request.getParameter("rpord");
        schyn = request.getParameter("schyn");
        trcd1 = request.getParameter("trcd1");
        sumyn = request.getParameter("sumyn");
        plord = request.getParameter("plord");
        plcd = request.getParameter("plcd");
        blord = request.getParameter("blord");
        scyn = request.getParameter("scyn");
    }

    public void insert(HttpServletRequest request){
            String sql="insert into ldg (code,ename,kname,asstgrp,libgrp,rpord,schyn,trcd1,sumyn,plord,plcd,blord,scyn) "+
                    "values (?,?,?,?,?,?,?,?,?,?,?,?,?)"; 
            try{
                PreparedStatement stmt=conn.prepareStatement(sql);
                    stmt.setInt(1,Integer.parseInt(code));
                    stmt.setString(2, ename);
                    stmt.setString(3, kname);
                    stmt.setString(4, asstgrp);
                    stmt.setString(5, libgrp);
                    stmt.setInt(6,Integer.parseInt(rpord));
                    stmt.setString(7, schyn);
                    stmt.setInt(8,Integer.parseInt(trcd1));
                    stmt.setString(9, sumyn);
                    stmt.setInt(10,Integer.parseInt(plord));
                    stmt.setString(11, plcd);
                    stmt.setInt(12,Integer.parseInt(blord));
                    stmt.setString(13, scyn);
                try{
                    request.setCharacterEncoding("UTF-8");
                }catch(UnsupportedEncodingException ex){      
                }
                stmt.executeUpdate();
                varmsg="saved";
            }catch(SQLException ex){
                System.out.println(ex);
                ex.printStackTrace();
                System.out.println(ex.getMessage());
                varmsg=ex.getMessage();
            }
        }%>
</head>
<body>
    <%
        if (request.getParameter("save") != null) {
            connect();
            gettxt(request);
            insert(request);
        }
    %>
    <%!String driverName = "com.mysql.jdbc.Driver";%>
    <%!String url = "jdbc:mysql://localhost:3306/ledger";%>
    <%!String user = "root";%>
    <%!String psw = "";%>
    <form method="post" name="form1">
        <table cellspacing="15" width="800" frame="box" align="center">
            <h1 align="center">Ledger</h1>
            <tr>
                <td>Code:</td>
                <td><input type="number" style="width: 20%;" name="code"
                    id="code" onblur="getVal(this.value)"></td>
            </tr>
            <tr>
                <td>Name:</td>
                <td><input type="text" name="ename" style="width: 175%;">
                </td>
            </tr>
            <tr>
                <td>Kannada name:</td>
                <td><input type="text" name="kname" style="width: 175%;">
                </td>
            </tr>
            <tr>
                <td>Asset Group:</td>
                <td>
                    <%
                        Connection con = null;
                        PreparedStatement ps = null;
                        try {
                            Class.forName(driverName);
                            con = DriverManager.getConnection(url, user, psw);
                            String sql = "SELECT * FROM dropdown";
                            ps = con.prepareStatement(sql);
                            ResultSet rs = ps.executeQuery();
                    %> <select name="asstgrp">
                        <%
                            while (rs.next()) {
                                    String fname = rs.getString("name1");
                        %>
                        <option value="<%=fname%>"><%=fname%></option>
                        <%
                            }
                        %>
                </select> <%
    } catch (SQLException sqe) {
        out.println(sqe);
    }
 %>
                </td>
            </tr>
            <tr>
                <td>Liability Group:</td>
                <td>
                    <%
                        try {
                            Class.forName(driverName);
                            con = DriverManager.getConnection(url, user, psw);
                            String sql = "SELECT * FROM dropdown1";
                            ps = con.prepareStatement(sql);
                            ResultSet rs = ps.executeQuery();
                    %> <select name="libgrp">
                        <%
                            while (rs.next()) {
                                    String fname = rs.getString("entry");
                        %>
                        <option value="<%=fname%>"><%=fname%></option>
                        <%
                            }
                        %>
                </select> <%
    } catch (SQLException sqe) {
        out.println(sqe);
    }
 %>
                </td>
            </tr>
            <tr>
                <td>RP Order:</td>
                <td><input type="number" name="rpord"></td>
                <td>Schedule in Balance Sheet:</td>
                <td><select name="schyn">
                        <option value="yes" selected>YES</option>
                        <option value="no">NO</option>
                </select></td>
            </tr>
            <tr>
                <td>Trading Order:</td>
                <td><input type="number" name="trcd1"></td>
                <td>Summary in Cash Book:</td>
                <td><select name="sumyn">
                        <option value="yes" selected>YES</option>
                        <option value="no">NO</option>
                </select></td>
            </tr>
            <tr>
                <td>P & L Order:</td>
                <td><input type="number" name="plord"></td>
                <td>P & L Account Head:</td>
                <td><select name="plcd">
                        <option value="yes" selected>YES</option>
                        <option value="no">NO</option>
                </select></td>
            </tr>
            <tr>
                <td>Balance Sheet Order:</td>
                <td><input type="number" name="blord"></td>
                <td>Section:</td>
                <td><select name="scyn">
                        <option value="yes" selected>YES</option>
                        <option value="no">NO</option>
                </select></td>
            </tr>
            <tr>
                <th colspan="2">

                    <button type="submit" name="save" value="save">Save</button>
                    <input type="button" name="delete" value="Delete"> </input>
                </th>
            </tr>
        </table>
        </div>
        <iframe name="iframe1" hidden></iframe>
    </form>
    <%=varmsg%>
</body>
</html>

0 个答案:

没有答案