简体   繁体   中英

How can I get recently typed string from textarea

I have to open an window, which contains the keywords information, which will be taken from database. If I enter an keyword into the textarea, it opens only one window. And I cant open the the window for next keyword.

This is my coding.

    String str = textArea.getText();

    //Connection con = new DBConnection().connect();  

    Class.forName(driver).newInstance();
    conn = DriverManager.getConnection(url+dbName,userName,password);

    String stm="select url from pingatabl where functn=?";

    PreparedStatement st = conn.prepareStatement(stm);

    st.setString(1, str);

    ResultSet rs = st.executeQuery();

    if (rs.next()) {

    String s = rs.getString(1);

    JFrame f=new JFrame();
    f.setVisible(true);
    f.setSize(500,750);
    JEditorPane jm=new JEditorPane();
    f.add(jm);
    jm.setPage(ClassLoader.getSystemResource(s));
            } else {
    JOptionPane.showMessageDialog(null, "function name not Found");
    }
    }
    catch (Exception ex) {

    System.out.println(ex);
    }

If I enter an keyword into the textarea, it opens only one window. And I cant open the the window for next keyword.

String str = textArea.getText();

It only opens one window because you only have one string. If you expect to have multiple keywords in the string then you need to parse each keyword and do the database query separately on each keyword.

So the code might be something like:

Connection con = new DBConnection().connect();  
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url+dbName,userName,password);

String[] keywords = textArea.getText().split();

for (String keyword: keywords)
{
    createFrame(conn, keyword);    
}

The method createFrame(...) will then create the PreparedStatement, do the query and build the frame to display the data returned from the database.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM