How to assign null or clean the resultset when nothing is found? example:
Select name from names where code = 0;
When this search does not return anything, I would like the resultset returned null because nothing was found, what is happening is that the resultset only updates the value when a result is found, when no result is found it keeps the last value, and when someone enters a name that does not exist in the table it always returns the last name found, this error delayed me a lot, and I found nothing that I could understand to solve my problem, thanks a lot guys! Have a nice day.
Note : I use Java.
6 Replies - 659 Views - Last Post: 10 November 2013 - 10:15 AM
#1
How to assign null or clean the resultset when nothing is found?
Posted 10 November 2013 - 07:46 AM
Replies To: How to assign null or clean the resultset when nothing is found?
#2
Re: How to assign null or clean the resultset when nothing is found?
Posted 10 November 2013 - 07:48 AM
Can you show us the code where you are querying the database?
#3
Re: How to assign null or clean the resultset when nothing is found?
Posted 10 November 2013 - 08:16 AM
CasiOo, on 10 November 2013 - 07:48 AM, said:
Can you show us the code where you are querying the database?
/>
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
public class FrameAcessoBanco extends javax.swing.JFrame {
/**
* Creates new form FrameAcessoBanco
*/
public FrameAcessoBanco() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jButton1 = new javax.swing.JButton();
field = new javax.swing.JTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jButton1.setText("GO!");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(field, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(131, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(38, 38, 38)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(field)
.addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 40, Short.MAX_VALUE))
.addContainerGap(106, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String teste = field.getText();
try{
Connection con = DriverManager.getConnection("jdbc:derby://localhost:1527/Banco","Ramom","banco123");
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("SELECT name from app.names where codigo="+teste);
while(rs.next()){
System.out.println(rs.getString(1));
}
}
catch(Exception e){
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(FrameAcessoBanco.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(FrameAcessoBanco.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(FrameAcessoBanco.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(FrameAcessoBanco.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new FrameAcessoBanco().setVisible(true);
}
});
}
// Variables declaration - do not modify
public javax.swing.JTextField field;
private javax.swing.JButton jButton1;
// End of variables declaration
}
#4
Re: How to assign null or clean the resultset when nothing is found?
Posted 10 November 2013 - 08:23 AM
This is a short exemple, Thank you my friend! I like it when the search does not find anything, I could clean the resultset or set null inside. so I can use the IF when he was null, so I need to clean it.
Thank you again my friend! Very thanks for atention !
Thank you again my friend! Very thanks for atention !
#5
Re: How to assign null or clean the resultset when nothing is found?
Posted 10 November 2013 - 09:08 AM
I don't see why your above example would behave as you described, sorry
#6
Re: How to assign null or clean the resultset when nothing is found?
Posted 10 November 2013 - 09:37 AM
CasiOo, on 10 November 2013 - 09:08 AM, said:
I don't see why your above example would behave as you described, sorry
I understand, I can not post all the code as it is not the company, I just want to clear the resultset because it only updates the data when he gets a valid value when it does not find the value, it keeps the old value and keeps repeating the old value to a new value that someone type in a name to search the table table possesses. Do you know any way to clean the resultset?
#7
Re: How to assign null or clean the resultset when nothing is found?
Posted 10 November 2013 - 10:15 AM
So the code you posted is not the actual code you are having trouble with?
Are the "bugs" present in the code posted?
Are the "bugs" present in the code posted?
Page 1 of 1

New Topic/Question
Reply


MultiQuote


|