如何在jframe中将数据库值添加到arraylist

时间:2018-10-10 14:56:42

标签: database arraylist swingx

我已经创建了一个用于新用户注册的框架。当按下添加按钮时,我要检查数据库中是否已经存在用户名。如果已经存在,则会弹出消息框以输入另一个用户名。我必须使用集合。

public void actionPerformed(ActionEvent arg0)
{
    String a = textField_6.getText();
    ArrayList<String> c1 = new ArrayList<String>();
    try
    {
        Class.forName("com.mysql.jdbc.Driver");

        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/stock", "root", "root123");
        java.sql.Statement st = null;
        st = con.createStatement();
        String q = "select Username from admin";

        ResultSet rs = null;
        rs = st.executeQuery(q);
        if (rs.next())
        {
            c1.add(rs.getString("Username"));
        }
        st.close();
        con.close();
    }
    catch (ClassNotFoundException | SQLException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    if (c1.contains(a))
    {
        JOptionPane.showMessageDialog(null, "username already exists....try another! ");
    }
    else
    {
        try
        {
            //code to add data in database
        }
    }
}

0 个答案:

没有答案