I'm finishing a project, which in a window designed with Java, and within this window with two textArea and one button. You write some SQL statements in the textArea, you select the one you want to execute and when you click on the button depending on what the String contains, since you execute one method or another.
Adjutno code fragment of the ActionPerformed:
jbRun.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String seleccion = jtaConsulta.getSelectedText();
if(jtaConsulta.getText().contains("select")){
jtaResultado.setText(bd.consulta(jtaConsulta.getSelectedText()));
}
else if(jtaConsulta.getText().contains("insert") || jtaConsulta.getText().contains("update") || jtaConsulta.getText().contains("delete")){
jtaResultado.setText(String.valueOf(bd.consultaAccion(seleccion)));
}
else{
JOptionPane.showMessageDialog(null, "Error global");
}
}
});
What it does is ask, if the string contains "select" it calls the query method and makes a selection of the table and that's it. If it contains "update" or "insert" or "delete" it executes the method queryAction that does the corresponding operation. But this is where I have the problem, I write my SQL statements and select the one I want to execute (for that I have the method getSelectedText ()) and for the select no problem, but if I want to update, it returns the catch of my exception, saying then the message that I have put there in case of any error.
If instead of using else if (condition), I put if, as in this code snippet:
jbRun.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String seleccion = jtaConsulta.getSelectedText();
if(jtaConsulta.getText().contains("select")){
jtaResultado.setText(bd.consulta(jtaConsulta.getSelectedText()));
}
if(jtaConsulta.getText().contains("insert") || jtaConsulta.getText().contains("update") || jtaConsulta.getText().contains("delete")){
jtaResultado.setText(String.valueOf(bd.consultaAccion(seleccion)));
}
else{
JOptionPane.showMessageDialog(null, "Error global");
}
}
});
the instructions are executed perfectly, but each and every one of the instructions return the catch. I do not know how to raise the conditional, or if I could put it another way.
public String consulta(String consulta){
String cadena = "";
try {
stmt = conexion.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
resultSet = stmt.executeQuery(consulta);
while(resultSet.next()){
int id = resultSet.getInt("id");
int age = resultSet.getInt("age");
String first = resultSet.getString("first");
String last = resultSet.getString("last");
cadena += "id: " + id + "\t" + age + "\t" + first + "\t" + last + "\n";
}
} catch (SQLException ex) {
JOptionPane.showMessageDialog(null, "Error al hacer la consulta select");
}
return cadena;
}
public String consultaAccion(String consulta){
int afectadas = 0;
try {
stmt = conexion.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
afectadas = stmt.executeUpdate(consulta);
stmt.close();
} catch (SQLException ex) {
JOptionPane.showMessageDialog(null, "Error al hacer inserccion o actualización");
}
return "Filas afectadas: " + afectadas;
}
thanks and best regards