I was making some improvements to my small system and I get the doubt because I've always been concatenated to data in the where and not after the select, since now the question is to concatenate the selected table (db) and the pk (dni) entered.
This is my code: 3
if (txtDniColaborador.getText().isEmpty()) {
JOptionPane.showMessageDialog(null, "DEBE INGRESAR UN DNI");
} else {
tabla = txtTabla.getText();
dni = txtDniColaborador.getText();
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conexion = DriverManager.getConnection("jdbc:mysql://localhost:3306/dbDaca?zeroDateTimeBehavior=convertToNull", "root", "1234");
Statement st = conexion.createStatement();
ResultSet rst = st.executeQuery("SELECT CONCAT("+tabla+") LIKE '%" + tabla + "%' FROM WHERE CONCAT(DniColaborador) LIKE '%" + dni + "%'");
ResultSetMetaData rsmt = rst.getMetaData();
int nrocolumnas = rsmt.getColumnCount();
DefaultTableModel modelo = new DefaultTableModel();
this.jTable1.setModel(modelo);
for (int x = 1; x <= nrocolumnas; x++) {
modelo.addColumn(rsmt.getColumnLabel(x));
}
while (rst.next()) {
Object[] fila = new Object[nrocolumnas];
for (int y = 0; y < nrocolumnas; y++) {
fila[y] = rst.getObject(y + 1);
}
modelo.addRow(fila);
}
} catch (ClassNotFoundException ce) {
ce.printStackTrace();
} catch (SQLException se) {
se.printStackTrace();
}
}
It certainly throws me wrong, but that my doubt does not succeed in concatenating my data well in the preparedStatement. Here is when I'm going to try it, in the cbTabla are my 11 tables that I added for the user to select the table that you want to search with the pk entered, the tables have the same name in my bd and are the same ones where you entered as an item in the cbTabla so that there is no problem when concatenating