使用Mysql和Java在一列中插入多个选择值

时间:2015-04-10 04:06:13

标签: java mysql eclipse jsp

嗨,这是我的第一篇文章...希望能找到答案>。<

提醒(alertNosqlstatdbNameusernamepasswordipAddrrecipient,{{1 }})

comments

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

<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>

|收件人|


| 12345678 |

这是我目前的结果。

我希望得到像

这样的东西

|收件人|


| 12345678,87654321 |

2 个答案:

答案 0 :(得分:0)

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

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

当然,对于各自的getter和setter,这个pojo将是另一个java类,例如:Recipient.java。然后回到你的主类,你可以从收件人创建一个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         

    }

我希望能提供一些帮助。

答案 1 :(得分:0)

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