Why do I keep on getting an exception-illegal operation on ResultSet?
Here is the code:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* SearchParts.java
*
* Created on 08-Mar-2010, 12:14:31
*/
package garits;
import java.sql.*;
import javax.swing.*;
/**
*
* #author Deniz
*/
public class SearchParts extends javax.swing.JFrame {
/** Creates new form SearchParts */
public SearchParts() {
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")
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
if (!jTextField1.getText().equals("")) {
String result = "";
int Partnumber = Integer.parseInt(jTextField1.getText());
DB db = new DB();
try {
db.connect();
String query = "Select * from Stock Where Part_no =" + "'" + jTextField1.getText() + "'";
ResultSet rs = db.execSQL(query);
if (rs.equals(null)) {
PartNotFound nf = new PartNotFound();
nf.setVisible(true);
} else {
ResultSetMetaData rsmd = rs.getMetaData();
int numberOfColumns = rsmd.getColumnCount();
int RowCount = 0;
for (int i = 1; i < numberOfColumns; i++) {
rs.getString(i);
result += i + "/n";
}
if (!result.equals("")) {
Receptionist_FranchiseePartFound part = new Receptionist_FranchiseePartFound();
part.setVisible(true);
while (rs.next()) {
RowCount++;
}
part.getTable().addRowSelectionInterval(0, RowCount);
} else {
PartNotFound nf = new PartNotFound();
}
}
} catch (Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(jButton1, "More information needed for search", "Error Message", JOptionPane.ERROR_MESSAGE);
}
} else if (!jTextField2.getText().equals("")) {
String result = "";
DB db = new DB();
try {
db.connect();
String query = "Select * from Stock Where Part_name =" + "'" + jTextField2.getText() + "'";
ResultSet rs = db.execSQL(query);
if (rs.equals(null)) {
PartNotFound nf = new PartNotFound();
nf.setVisible(true);
} else {
ResultSetMetaData rsmd = rs.getMetaData();
int numberOfColumns = rsmd.getColumnCount();
int RowCount = 0;
for (int i = 1; i < numberOfColumns; i++) {
rs.getString(i);
result += i + "/n";
}
// Receptionist_FranchiseePartFound part = new Receptionist_FranchiseePartFound();
// part.setVisible(true);
if (!result.equals("")) {
Receptionist_FranchiseePartFound part = new Receptionist_FranchiseePartFound();
part.setVisible(true);
while (rs.next()) {
RowCount++;
}
part.getTable().addRowSelectionInterval(0, RowCount);
} else {
PartNotFound nf = new PartNotFound();
nf.setVisible(true);
}
}
} catch (Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(jButton1, "More information needed for search", "Error Message", JOptionPane.ERROR_MESSAGE);
}
} else if (jTextField1.getText().equals("") && jTextField2.getText().equals("")) {
String result = "";
DB db = new DB();
try {
db.connect();
String query = "Select * from Stock Where Manufacturer =" + "'" + jTextField3.getText() + "'AND Vehicle_type ='" + jTextField4.getText() + "'";
ResultSet rs = db.execSQL(query);
if (rs.equals(null)) {
PartNotFound nf = new PartNotFound();
nf.setVisible(true);
}
else{
ResultSetMetaData rsmd = rs.getMetaData();
int numberOfColumns = rsmd.getColumnCount();
int RowCount = 0;
for (int i = 1; i < numberOfColumns; i++) {
rs.getString(i);
result += i + "/n";
}
// Receptionist_FranchiseePartFound part = new Receptionist_FranchiseePartFound();
// part.setVisible(true);
if (!result.equals("")) {
Receptionist_FranchiseePartFound part = new Receptionist_FranchiseePartFound();
part.setVisible(true);
while (rs.next()) {
RowCount++;
}
part.getTable().addRowSelectionInterval(0, RowCount);
} else {
PartNotFound nf = new PartNotFound();
nf.setVisible(true);
}
}
} catch (Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(jButton1, "More information needed for search", "Error Message", JOptionPane.ERROR_MESSAGE);
}
} else if (jTextField3.getText().equals("") || jTextField4.getText().equals("")) {
JOptionPane.showMessageDialog(jButton1, "More information needed for search", "Error Message", JOptionPane.ERROR_MESSAGE);
}
}
/**
* #param args the command line arguments
*/
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JLabel jLabel8;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
private javax.swing.JTextField jTextField3;
private javax.swing.JTextField jTextField4;
// End of variables declaration
}
I see a few things
First rs is never null. If there are no rows then rs.next() will return false
ResultSet starts position BEFORE the first row. You need to call rs.next() to move to the first row which, as above, will return false if there are no rows.
The typical pattern for ResultSet use is...
while (rs.next())
{
rs.getXXX();
}
ResultSet should ALWAYS be close()d when you are done, otherwise you may leak database resources. This also applies to Connection, Statement, PreparedStatement so check your DB code in other classes.
When looping over the column metadata you need to do..
for (int i = 1; i <= numberOfColumns; i++) {
}
Note the <= rather than <. As you have correctly found that JDBC column indexes start at 1.
It would be worth considering using a library that hides all the JDBC noise. Consider spring which has a basic JDBC abstraction while retaining all the power or all the way up to hibernate which is very powerful but has quite a learning curve. In your case I'd recommend just use the spring stuff.
See: Spring docs
You always need to call next() on the ResultSet, before accessing it. If not, the pointer points to the row before the first. You do this with while-loops in your code, but some places you dont, which generates an error:
for (int i = 1; i < numberOfColumns; i++) {
rs.getString(i);
result += i + "/n";
}
Ohh, and as another poster mentions, you dont assign the result to a variable, so the rs.getString(i) call has no effect. But this is probably your source of error.
if rs is null, then rs.equals(null) will throw a NullPointerException instead of returning false. Nevertheless if that is not the exception you are getting then the problem lies somewhere else.
Also, take a look into the javadoc/code for the DB class' execSQL(query) method. Typically, a ResultSet is never null, e.g. like when you use PreparedStatement.executeQuery. However, check into the that execSQL method to verify whether or not it will ever return a null ResultSet.
Remove the following line, it serves no purpose.
Its trying to the the column value from the ResultSet but you haven't scrolled to the first row yet
rs.getString(i);
Several have pointed out some of your problems in the code. The reason for your exception is because you have not advanced to the first row using rs.next(). If you expect that your SQL statement will only retrieve a single row then you probably should change your rs.equals(null) statement to:
if (!rs.next()) {
...code here to set not found...
} else {
...code here to retrieve the columns...
}
Here are some other tips. In the code to retrieve the columns, your use of rs.getString(i) doesn't do anything, the result variable is just accumulating the column numbers with "/n" (not new-line; probably that should be "\n"). So the loop inside that section should probably become:
for (int i = 1; i <= numberOfColumns; i++) {
result += rs.getString(i) + "\n";
}
But concatenating to a immutable String is not good and can result in slow execution for large numbers of concatenations. Use a StringBuilder instead and initialize it to a reasonable size, something like this:
StringBuilder sb = new StringBuilder(256);
for (int i = 1; i <= numberOfColumns; i++) {
sb.append(rs.getString(i)).append("\n");
}
result = sb.toString();
Later you loop through the results set, so in the loop through the columns are you trying to get column names from the meta data? In that case:
StringBuilder sb = new StringBuilder(256);
for (int i = 1; i <= numberOfColumns; i++) {
sb.append(rsmd.getColumnName(i)).append("\n");
}
result = sb.toString();
But since you need to check for the first row the next loop will not count the number of rows correctly so you will need to adjust for that, perhaps (and there is no need to check result at that point since it is guaranteed to no longer be an empty string since there must be at least one column in the table):
do {
RowCount++; // recommend using rowCount.
} while (rs.next());
In the end, what are you using result for? It seems that this is just used to determine if there were some columns in the result. If that is all then you can eliminate most of this code. For what you actually have (I don't know if this is your intent), this could be reduced to (in the else if where you do the DB interaction):
DB db = new DB();
try {
db.connect();
String query = "select count(1) from Stock where Part_name =" + ...;
ResultSet rs = db.execSQL(query);
if (!rs.next()) {
PartNotFound nf = ...;
nf.setVisible(true);
} else {
Receptionist_FranchiseePartFound part = new Receptionist_FranchiseePartFound();
part.setVisible(true);
rowCount = rs.getInt(1);
part.getTable().addRowSelectionInterval(0, rowCount);
}
} catch (Exception e) {
...error handling with stack trace/JOptionPane...
}
Note that if you are just after the count of rows then it is best to let the database engine do that for you - looping through the result rows just to count them also means that all that data must be sent to your application. Instead, if you just use the aggregate function then the database only needs to send a single row and column to your application.
Related
So I selected some values from a database table to divide them in little groups and insert it into another table within the database by putting the selected values in an ArrayList and taking the size of that ArrayList to do the math.
But after I do the math, I'm just left with the variables containing the information I need. But, I cant put them into the other table because they're not the same things that I selected in the beginning. They're just numbers now. I really need to insert those variables into the other table but when I do, I get foreign key constraint fails error and I think that's because the variable is not a foreign key anymore but just a number. I don't know if there is a solution but it would help me allot. Here is my code:
private void spelersVerdelenMouseClicked(java.awt.event.MouseEvent evt) {
String comboBoxValue = jComboBoxDeelnemer.getSelectedItem().toString();
String spelerRonde1 = "SELECT lid, toernooi FROM deelnemer where toernooi LIKE " + comboBoxValue ;
ArrayList<String> dlnmrs = new ArrayList<>();
try {
PreparedStatement pstat = con.prepareStatement(spelerRonde1);
ResultSet rs = pstat.executeQuery();
while (rs.next()) {
dlnmrs.add(rs.getString("lid"));
for (int i = 0; i < dlnmrs.size(); i++) {
}
}
} catch (Exception e) {
JOptionPane.showMessageDialog(rootPane,e);
}
verdeelTafels(1, dlnmrs.size());
private void verdeelTafels(int ronde, int aantalDeelnemers) {
System.out.println(aantalDeelnemers);
int aantalTafels = (int) aantalDeelnemers / AANTAL_SPELERS_PER_TAFEL;
int restSpelerPerRonde = aantalDeelnemers % AANTAL_SPELERS_PER_TAFEL;
if (aantalDeelnemers == (AANTAL_SPELERS_PER_TAFEL * 2)) {
aantalTafels = 1;
restSpelerPerRonde = 0;
}
for (int i = 0; i < aantalTafels; i++) {
int maxSpelersPerTafel = AANTAL_SPELERS_PER_TAFEL;
if (i == aantalTafels - 1) {
maxSpelersPerTafel += restSpelerPerRonde;
}
System.out.println("Tafel " + (i + 1) + " heeft " + maxSpelersPerTafel + " deelnemers.");
//spelersDoorlopen
for (int j = 0; j < maxSpelersPerTafel; j++) {
String query = "insert into spelerPerTafel (lid,tafel,ronde) select lcode,tfcode,rcode from lid,tafel,ronde ";
try{
PreparedStatement pstat = con.prepareStatement(query);
pstat.execute(query);
}
catch (Exception e) {
JOptionPane.showMessageDialog(rootPane,e);
System.out.println(e);
}
}
}
}
try
String query = "insert into tableA select * from tableB where XXX";
using where condition can implement to divide the tableB
I have following codes:
-------class------------
private class SystemHealthAlert implements Work {
List<MonitorAlertInstance> systemHealthAlertList;
private String queryString;
// private java.util.Date startDate;
// private java.util.Date endDate;
#Override
public void execute(Connection connection) throws SQLException {
PreparedStatement ps = connection.prepareStatement(queryString);
int index = 1;
ResultSet rs = ps.executeQuery();
int columnCount = rs.getMetaData().getColumnCount();
while(rs.next())
{
//String[] row = new String[columnCount];
//results.set(index, element);
//for (int i=0; i <columnCount ; i++)
// {
// row[i] = rs.getString(i + 1);
// }
systemHealthAlertList.add(row);
}
rs.close();
ps.close();
}
}
---------Method-----------
public List<MonitorAlertInstance> getSystemHealthAlert(Long selectedSensorId) {
List<MonitorAlertInstance> systemHealthAlertList;
try {
// Add SELECT with a nested select to get the 1st row
String queryString = "select min(MONITOR_ALERT_INSTANCE_ID) as MONITOR_ALERT_INSTANCE_ID, description" +
" from ems.monitor_alert_instance " +
" where description in (select description from monitor_alert_instance" +
" where co_mod_asset_id = " + selectedSensorId +
" )" +
" group by description";
SystemHealthAlert work = new SystemHealthAlert();
// work.coModAssetId = coModAssetId;
work.queryString = queryString;
getSession().doWork(work);
systemHealthAlertList = work.systemHealthAlertList;
} catch (RuntimeException re) {
// log.error("getMostRecentObservationId() failed", re);
throw re;
}
//log.info("End");
return systemHealthAlertList;
}
My query returns three rows from DB. How can I return systemHealthAlertList from the class that will have all the three rows of the query.
In method execute, you should fill your List<MonitorAlertInstance> systemHealthAlertList with instances of MonitorAlertInstance. Create a new instance of MonitorAlertInstance inside the while loop where you retrieve the data:
//You don't need this line, remove it
//int columnCount = rs.getMetaData().getColumnCount();
while(rs.next()) {
//create a new instance of MonitorAlertInstance per ResultSet row
MonitorAlertInstance monitor = new MonitorAlertInstance();
//set the fields from the ResultSet in your MonitorAlertInstance fields
//since I don't know the fields of this class, I would use field1 and field2 as examples
monitor.setField1(rs.getInt(1));
monitor.setField2(rs.getString(2));
//and on...
systemHealthAlertList.add(monitor);
}
Apart from this problem, you should initialize your List<MonitorAlertInstance> systemHealthAlertList variable before use it:
systemHealthAlertList = new ArrayList<MonitorAlertInstance>();
while(rs.next()) {
//content from previous code...
}
Define a class/bean to hold the data from one given row. Loop through your rows, and create one instance of that class for each row you have. Add these instances to some List. Return the List of these 3 instances.
As mentioned in the header I cannot get my JTable to update with a new row unless I restart the program. When I restart, the new row is there and everything is as it should be. I have tried revalidating/repainting the panel and frame, I have tried the fire methods. I'm at a loss. Thanks in advance
ActionListener (in adminGUI class) for 'Add' button:
if(source.equals(add2)){
String c = itb.getText();
int a = main.getResults();
boolean matches = Pattern.matches("[A-Z][a-z]+", c);
if(matches == true){
main.addGenre(a, c);
}
String Method(in main class) to add a row to the database table:
public static void addGenre(int a, String b){
int rowsAdded;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection connect =DriverManager.getConnection("jdbc:odbc:MovieDB");
Statement stmt = connect.createStatement();
String query = "INSERT INTO Genres (genre_id, genre_name)" + "VALUES(" + a + ", '" + b + "')";
rowsAdded = stmt.executeUpdate(query);
}catch(Exception exc){}
}
Method(also in main class) to increment the auto-increment-key column:
public static int getResults(){
int a = 0;
ResultSet ints = main.getResults("Select genre_id from Genres");
try {
while(ints.next()){
int d = ints.getInt("genre_id");
if(d>a){
a = d;
}
a++;
}
} catch (SQLException ex) {
Logger.getLogger(main.class.getName()).log(Level.SEVERE, null, ex);
}
return a;
}
JTable details:
ResultSet rs1 = main.getResults("Select * from Genres");
JTable tab1 = new JTable(DbUtils.resultSetToTableModel(rs1));
DbUtils.resultSetToTableModel details :
public class DbUtils {
public static TableModel resultSetToTableModel(ResultSet rs) {
try {
ResultSetMetaData metaData = rs.getMetaData();
int numberOfColumns = metaData.getColumnCount();
Vector columnNames = new Vector();
// Get the column names
for (int column = 0; column < numberOfColumns; column++) {
columnNames.addElement(metaData.getColumnLabel(column + 1));
}
// Get all rows.
Vector rows = new Vector();
while (rs.next()) {
Vector newRow = new Vector();
for (int i = 1; i <= numberOfColumns; i++) {
newRow.addElement(rs.getObject(i));
}
rows.addElement(newRow);
}
return new DefaultTableModel(rows, columnNames);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
"I cannot get my JTable to update with a new row unless I restart the program."
I think what you're expecting is that when the database table update, so should your JTable. It doesn't really work like that. You need to update the TableModel, and the JTable will be automatically updated
Since resultSetToTableModel returns a DefuaultTableModel, you can use either of the two methods from DefaultTableModel:
public void addRow(Object[] rowData) - Adds a row to the end of the model. The new row will contain null values unless rowData is specified. Notification of the row being added will be generated.
public void addRow(Vector rowData) - Adds a row to the end of the model. The new row will contain null values unless rowData is specified. Notification of the row being added will be generated.
So when your are adding the data to the database, you also want to update the DefaultTableModel like this
public static void addGenre(Integer a, String b){
...
rowsAdded = stmt.executeUpdate(query);
if (rowsAdded > 0) {
DefaultTableModel model = (DefaultTableModel)tab1.getModel();
model.addRow( new Object[] { a, b });
}
}
Also noticed I changed the method signature to Integer instead of int so it will fit with the Object[] passed to addRow. The int you pass to it will get autoboxed to Integer
SIDE NOTES
Don't swallow you exception by putting nothing in the catch block. Put something meaningful that will notify you of any exceptions that may occur, like
catch(Exception ex) {
ex.printStackTrace();
}
You should also close your Connections, Statements, and ResultSets
You should use PreparedStatement instead of Statement, to avoid SQL injection.
private void resetListData() throws ClassNotFoundException, SQLException
{
Connection cne = null;
try {
Class.forName("org.sqlite.JDBC");
cne = DriverManager.getConnection("jdbc:sqlite:table.sqlite");
cne.setAutoCommit(false);
PreparedStatement psd = (PreparedStatement) cne.prepareStatement("Select * from Genres");
psd.execute();
ResultSet r = psd.getResultSet();
ResultSetMetaData rsmd = r.getMetaData();
int count = rsmd.getColumnCount();
String[] meta = new String[count];
for (int i = 0; i < count; i++)
{
String name = rsmd.getColumnName(i + 1);
meta[i] = name;
//System.out.println(name);
}
model = new DefaultTableModel(new Object[][]{}, new String[]{"name", "address"});
jTable1.setModel(model);
while (r.next())
{
Object[] row = new Object[count];
for (int i = 1; i <= count; ++i)
{
row[i - 1] = r.getString(i); // Or even rs.getObject()
}
model.addRow(row);
}
cne.close();
} catch (ClassNotFoundException | SQLException e) {
}
}
Use this code. so you can insert one row at the end of Jtable without restarting application.,
Thanks..
I have a webservice where from the Client-side some parameters are passed to perform a query on the DB, the Server-Side is supposed to carry out the query and return the results.Since the result might be more than one row and i will have to use it on the client-side to show an output this what i did:
1.Perform the query
2.take each row of the result and put it in an array
3.convert the array to String and pass it to the client side(converted array to String, because it was simple)
BUT the problem is that it doesnt pass the the array-turned-string but only the value which was used to initialize the string, here is the code
String ris = "";
String q;
String beta = null;
String one="";
String errore = connetti();
try {
if (errore.equals("")) {
Statement st = conn.createStatement();
//ESECUZIONE QUERY
q = "SELECT DISTINCT nome FROM malattia WHERE eta='" + age + "' AND sesso='" + sexstr + "' AND etnia='" + etniastr + "' AND sintomi IN(" + tes + ")";
ResultSet rs = st.executeQuery(q);
if (!rs.last()) {
ris = "no";
}
//This is the part which i'm talking about
else {
//getRowCount is another class used to find out number of rows,I use it to declare an array which would contain the result of the query
int two=getRowCount(rs);
String[] alpha= new String[two];
//Loop through the resultstatement and put result from the column **nome** in the array **alpha**
while(rs.next()){
alpha[i]=rs.getString("nome");
i++;
}
//The value of ris which is empty, is returned
ris="";
//instead of this one, where i convert the array **alpha** to String
ris=arrayToString(alpha,",");
}
}
else {
ris = errore;
}
conn.close();
} catch (Exception e) {
ris = e.toString();
}
return ris;
}
//returns the number of rows of **ris**
public static int getRowCount(ResultSet set) throws SQLException
{
int rowCount;
int currentRow = set.getRow(); // Get current row
rowCount = set.last() ? set.getRow() : 0; // Determine number of rows
if (currentRow == 0) // If there was no current row
set.beforeFirst(); // We want next() to go to first row
else // If there WAS a current row
set.absolute(currentRow); // Restore it
return rowCount;
}
//converts the array to String
public String arrayToString(String[] array, String delimiter) {
StringBuilder arTostr = new StringBuilder();
if (array.length > 0) {
arTostr.append(array[0]);
for (int i=1; i<array.length; i++) {
arTostr.append(delimiter);
arTostr.append(array[i]);
}
}
return arTostr.toString();
Thanks alot in advance!
After conn.close() you return beta instead of ris. This may be the cause of the behavior you are experiencing. However, I am not sure because I can not properly see how you open and close the curly brackets.
I query a database and get a lot information back that should be presented to the user. In the database I have fields a, b, c, d and e. Now, the user should be able to indicate which of these fields that should be printed on screen (i.e. the user can choose to view only a subset of the data retrieved from the database).
How do I dynamically create a print statement that sometimes prints two of the fields, sometimes four, sometimes three etc. depending on what the user wants?
If all the hard work is already done and you just have a result set to print, then it could be as simple as a succession of calls to System.out.print() for each result and then finish the line with a \n. It can be nested in a FOR loop, so if you have an int with the number of fields to print, just iterate through them.
In a more complicated case when you have a full list where some fields are chosen and others not, then you could use a (slightly) crude method like this:
...
String[] chosenFields = {"Field 1", "Field 2" /*, (et cetera) */};
for (int i = 0; i < numberOfFields; i++)
{
for (int j = 0; j < chosenFields.length; j++)
{
if (fieldsName[i].equals(chosenFields[j]))
System.out.print(fields[i] + " ");
break;
}
}
System.out.println();
...
Sorry about bad indentation; not sure how to sort it on here!
If field names are indeterminate at runtime and you're using Java to execute queries, consider using class ResultSetMetaData to get them.
EDIT:
As an example, here's some of my code which gets all the field names from a table, then creates a tickbox for each, which the user can select or deselect. All the JFrame GUI stuff I've omitted. When the user presses a submit button, the application check each tickbox and constructs an SQL statement to suit the users request.
...
JCheckBox[] jcb;
ResultSetMetaData rsmd;
private void makeCheckBoxes()
{
initConnection(); // Establish connection to MySQL server
try
{
Statement query = connection.createStatement();
ResultSet rs = query.executeQuery("SELECT * FROM client_db;");
rsmd = rs.getMetaData();
noOfColumns = rsmd.getColumnCount();
jcb = new JCheckBox[noOfColumns];
for (int i = 0; i < noOfColumns; i++)
{
jcb[i] = new JCheckBox(rsmd.getColumnName(i + 1));
jpCheckBoxes.add(jcb[i]);
jcb[i].setEnabled(false);
jcbComboBox.addItem(rsmd.getColumnName(i + 1));
}
jcb[0].setSelected(true);
rs.close();
query.close();
connection.close();
}
catch (SQLException e)
{
System.err.println("!> Caught SQLException:\n" + e.getMessage());
System.exit(1);
}
}
...
if (e.getSource() == jbSubmit)
{
String query = "";
initConnection();
if (jtfSearch.getText().isEmpty() == true) // JTextField
{
jtaResults.setText(null); // JTextArea
jtaResults.append("Please enter some search text in the text box above!\n");
return;
}
else
{
int selectedFields;
if (jrbAll.isSelected() == true) // JRadioButton
{
query = "SELECT *";
selectedFields = -1;
}
else
{
query = "SELECT";
selectedFields = 0;
for (int i = 0; i < noOfColumns; i++)
if (jcb[i].isSelected() == true)
{
try
{
if (selectedFields > 0)
query += ",";
query += " " + rsmd.getColumnName(i + 1);
}
catch (SQLException err)
{
System.err.println("!> Caught SQLException:\n" + err.getMessage());
System.exit(1);
}
selectedFields++;
}
}
if (selectedFields == 0)
{
jtaResults.setText(null);
jtaResults.append("No fields were selected!!\n");
return;
}
else
{
query += " FROM client_db WHERE " + jcbComboBox.getSelectedItem() + " LIKE '%" + jtfSearch.getText() + "%'";
if (jcbCurrentClients.isSelected() == true)
query += " AND currentClient LIKE 'y'";
query += ";";
}
}
System.out.println("Query = \"" + query + "\"");
/* Now, print it out in the text area!! */
try
{
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery(query);
ResultSetMetaData rsMetaData = rs.getMetaData();
int columnCount = rsMetaData.getColumnCount();
jtaResults.append("--------------------------------\n");
int noOfResults = 0;
jtaResults.setText(null);
while (rs.next() == true)
{
if (noOfResults > 0)
jtaResults.append("\n");
jtaResults.append("* Search match " + (noOfResults + 1) + ":\n");
for (int i = 0; i < columnCount; i++)
{
jtaResults.append("-> " + rsMetaData.getColumnName(i + 1) + ": " +
rs.getString(i + 1) + "\n");
}
noOfResults++;
}
if (noOfResults == 0)
{
jtaResults.append("No results were returned; please try again with more ambiguous search terms.\n\n");
}
//scroller.setScrollPosition(0, 1048576);
rs.close();
stmt.close();
connection.close();
}
catch (SQLException err)
{
System.err.println("!> Caught SQLException:\n" + err.getMessage());
System.exit(1);
}
}
}
Hopefully this helps. The sustained concatenation to query forms a valid SQL statement based on the fields the user chose. Hopefully a few modifications to this to just print certain fields will help you. The System.out.println() call to print query about two-thirds down is a good place to work from.
The natural way to switch an optional value on or off would be a radiobutton. For 5 fields i.e. an array of 5 radiobuttons.
StringBuffer sb = new StringBuffer (5 * 10);
for (int i = 0; i < 5; ++i)
if (rb[i])
s.append (field[i]).append (" ");
Maybe you're better of only selecting interesting columns from the database? Then a dummy-column is helpful:
sql = new StringBuffer ("SELECT 1 "); // the dummy-column
for (int i = 0; i < 5; ++i)
if (rb[i])
sql.append (", ").append (fieldname[i]);