SELECT * FROM <table> BETWEEN <value typed in a JTextField> AND <idem>
Posted
by
Rodrigo Sieja Bertin
on Stack Overflow
See other posts from Stack Overflow
or by Rodrigo Sieja Bertin
Published on 2012-09-17T14:59:42Z
Indexed on
2012/09/17
15:37 UTC
Read the original article
Hit count: 243
In this part of the program, the JInternalFrame file FrmMovimento, I made a button called Visualizar ("View"). Based on what we type on the text fields, it must show the interval the user defined.
There are these JTextFields: Code from: _ To: _ Asset: _
And these JFormattedTextFields: Date from: _ To: _
There are registers appearing already in the JDesktopPane from JInternalFrame FrmListarMov, if I use another SELECT statement selecting all registers, for example. But not if I type as I did in the title:
public List<MovimentoVO> Lista() throws ErroException, InformacaoException{
List<MovimentoVO> listaMovimento = new ArrayList<> ();
try {
MySQLDAO.getInstancia().setAutoCommit(false);
try (PreparedStatement stmt = MySQLDAO.getInstancia().prepareStatement(
"SELECT * FROM Cadastro2 WHERE Codigo BETWEEN "+ txtCodDeMov +" AND "+ txtCodAteMov +";") {
ResultSet registro = stmt.executeQuery();
while(registro.next()){
MovimentoVO Movimento = new MovimentoVO();
Movimento.setCodDeMov(registro.getInt(1));
Movimento.setCodAteMov(registro.getInt(2));
Movimento.setAtivoMov(registro.getString(3));
Movimento.setDataDeMov(registro.getString(4));
Movimento.setDataAteMov(registro.getString(5));
listaMovimento.add(Movimento);
}
}
} catch (SQLException ex) {
throw new ErroException(ex.getMessage());
} catch (InformacaoException ex) {
throw ex;
}
return listaMovimento;
}
In the SELECT line, txtCodDeMov is how I named the JTextField of "Code from" and txtCodAtemov is how I named the JTextField of the first "To".
Oh, I'm using NetBeans 7.1.2 (Build 201204101705) and MySQL Ver 14.14 Distrib 5.1.63 in a Linux Mint 12 64-bits.
© Stack Overflow or respective owner