0

I'm making a manager to use mysql through it and not that ugly console, so I get to put on a JTextPane the command "Create database user", and my var R gets that string and I make a substring to take 'user' only, however when I try to create another database and write for example "Create database example" it will take all the lines from the JTextPane and my var R will take "User create database example", so how can I make that var to get the text from a specific line of my JTextPane, here is the code I have so far:

private void jTextPane1KeyPressed(java.awt.event.KeyEvent evt) {                                      
    int key = evt.getKeyCode();
    if (key == KeyEvent.VK_ENTER) {
        try {
            sentencia = conexion.createStatement();
            se = jTextPane1.getText().toLowerCase();
            String r = se.replaceAll("['\n|\t|\r]", " ").substring(16);
            System.out.println(r);
            sentencia.executeUpdate("create database "+r);
            System.out.println("BBDD Created");
            r = "";
            } catch (Exception ae) {
                System.err.println(ae.getMessage());
                System.out.println("BBDD not Created");
            }//catch
    }//if  
}
  • Regarding the than -> thanks* - You can edit your post by clicking edit... – Bennett Yeo Mar 05 '16 at 22:21
  • 1
    Don't use `KeyListener` on a text component, in your case, I'd consider using a Key Binding (maybe [ctrl]+[Enter]) or an "execute" button – MadProgrammer Mar 05 '16 at 22:22
  • I was just going to say what @MadProgrammer stated above: don't use a KeyListener on a text component. Consider Key Bindings or perhaps a DocumentListener (as long as you're not going to use the listener to change the text component's text). – Hovercraft Full Of Eels Mar 05 '16 at 22:23
  • To get the "current" line (where the caret is), you could use something like [this](http://stackoverflow.com/questions/21686146/deleting-a-single-line-through-a-jbutton-in-a-textarea/21690976#21690976), note though, in your current implementation, the caret would have moved to a new line, so you will need to compensate for that – MadProgrammer Mar 05 '16 at 22:26
  • @MadProgrammer I used the KeyListener because all I want is to execute the query when the user press enter, just like MySQL does... On the other hand, I don't quiet get that cared line implementation. But I do think you understand what I want to do. – A. Zambrano Mar 05 '16 at 23:42
  • KeyListerner is still a poor choice, key bindings are the preferred mechanism (and tend to work in a known pattern) – MadProgrammer Mar 06 '16 at 01:22

0 Answers0