EDIT4:
I don’t know why, but the structure I used below didn’t work. I had to modify a file I found at java2s website, it’s called TableFromDatabase, then make the JTextFields public static, then instantiating the TableFromDatabase class in FrmMovimento and etc:
TableFromDatabase frame = new TableFromDatabase();
jDesktopPane2.add(frame);
frame.show();
I also modified TableFromDatabase line
frame.setDefaultCloseOperation( EXIT_ON_CLOSE_ );
to
frame.setDefaultCloseOperation( DISPOSE_ON_CLOSE );
ORIGINAL POST:
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.
EDIT: actually, it’s MovimentoVO2, MovimentoDAO2 and so on, because I did two files, one with setCodDeMov and setCodAteMov etc. and another one with only setCodigo, setAtivo and setData. I’m still kind of noob in using these techniques, sorry.
From table Cadastro2, only primary key Codigo will be used for the query.
Later we’ll create other table where we’ll register buy and sell of assets, so they’ll have the type of asset and movement date, which will be added to the SELECT … BETWEEN … AND we need. By now, I need only to understand how Java could get the value I typed in the JTextField and put it into the SELECT command.
EDIT2: In the client registration screen, query button is working 100%, as follows. BUT it’s a different situation, since in this case the button event makes all JTextFields to be completed if the register with specified Code (primary key in the table Cadastro2) is found. Now what I want to do is produce a table with all the registers in the interval.
EDIT3: I think the correct term is REPORT GENERATING. I heard about iReport and Jasper Reports, but I never used them. Considering this project must be presented in December in the university, is there time to learn these 2 tools? As I’m learning Java still, would it be a bad move? At first I think it is, so I’m doing everything only on Netbeans.
don’t use dynamic sql, it causes SQL injection. Use parameters.
replace
"SELECT * FROM Cadastro2 WHERE Codigo BETWEEN "+ txtCodDeMov +" AND "+ txtCodAteMov +";"to
EDIT:
and don’t get fields value by index if you don’t specify fields order in query. You can change fields order in database later, and your function will return incorrect rusult.
Get values by field name or specify fileds order.