【问题标题】:insert multiple select value in one column using Mysql and Java使用 Mysql 和 Java 在一列中插入多个选择值
【发布时间】:2015-04-10 04:06:12
【问题描述】:

您好,这是我的第一篇文章...希望能找到答案>.

警报(alertNosqlstatdbNameusernamepasswordipAddrrecipientcomments

<select multiple name="recipient">
<option value="select">select</option>

                while (rs.next()) {
                        String recipient = rs.getString("hp") + " "
                                + rs.getString("lastname");
            %>
            <option value=<%=recipient%>><%=recipient%></option>
            <%
                }
                } catch (SQLException sqe) {
                    out.println("home" + sqe);
                }
            %>
        </select>

我希望将多个值保存到同一列(收件人)。 这是我的插入语句。

String sql = "INSERT INTO alert(sqlstat, dbName, username, password, ipAddr,recipient, comments)"+ "VALUES(?,?,?,?,?,?,?)";

|收件人|


|12345678 |

这是我目前的结果。

我希望得到类似的东西

|收件人|


|12345678,87654321 |

【问题讨论】:

  • 输出是什么?下拉菜单没有显示?
  • 您提供的查询不会插入同一列。它将值插入到一行的不同列中。
  • @manpreetbhamba 嗨,我添加了结果,请帮忙看看它不能解决它。
  • 嗨,您能否提供更多关于您的预期结果的编码和信息?
  • 这是我目前的结果。 |收件人 | |12345678 |我的预期结果是在一列中有两个数字,如下所示:|收件人| |12345678,87654321 |

标签: java mysql eclipse jsp


【解决方案1】:

Yoy 也许可以尝试为收件人创建一个 pojo,如下所示:

    private String sqlstat;
    private String dbName;
    private String username;
    private String password;

当然,有了各自的getter和setter,这个pojo就会是另一个java类,例如:Recipient.java。然后回到你的主类,你可以从 Recipient 创建一个 List 对象,像这样

    private List<Recipient> lstRecipient = new ArrayList<Recipient>();



    while (rs.next()){
        Recipient tempRecipient = new Recipient();
        tempRecipient.setsqlstat(rs.getString("hp"));
        tempRecipient.setdbName(rs.getString("dbName"));
        tempRecipient.setusername(rs.getString("username"));
        tempRecipient.setpassword(rs.getString("password"));

        //after you've saved the first row of data of rs on tempRecipient
        //you add the data of tempRecipient on the list

        lstRecipient.add(tempRecipient);

    }

如果你想显示你保存在 lstRecipient 中的所有数据,你可以简单地创建一个 for 语句来刷新数据

    for ( Recipient tempRecipient : lstRecipient){
        System.out.println("sqlstat: " + tempRecipient.getsqlstat() );
        System.out.println("dbName: " + tempRecipient.getdbName() );
        System.out.println("username: " + tempRecipient.getusername() );
        System.out.println("password: " + tempRecipient.getpassword() );

        //or if you want to send those values to an String 

        String sql = "INSERT INTO alert(sqlstat, dbName, username, password)"
                    + "VALUES('"+tempRecipient.getsqlstat()+"' ,'"+tempRecipient.getdbName() +
                    "','"+tempRecipient.getusername()  +"', '"+tempRecipient.getpassword() +"')";
        // then execute the sql         

    }

我希望能提供一些帮助。

【讨论】:

  • 感谢你们的帮助。我刚开始所以不是很熟悉所以我想创建另一个数据库表吗? @Brijesh Bhatt
【解决方案2】:

对于那些想和我做同样事情的人,请对 sql 中的 String tokenizer 进行更多研究。 Does PL/SQL have an equivalent StringTokenizer to Java's?

【讨论】:

    猜你喜欢
    • 2011-03-24
    • 1970-01-01
    • 2018-09-27
    • 2015-02-19
    • 1970-01-01
    • 2012-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多