My code is supposed to get a string from text box and get a string from a selection of a jcombobox (search keyword from column) and create a results table (via a new class) after querying the database. I'm not getting any errors and i know (via a if statment that resultSet != null but my print statement in the while loop doesn't ever print - while(result.next()). no new class(pop up jframe) is ever created.
Any help is appreciated - my code is quite messy at the momoment after trying to find out the problem myself. so sorry about that
I now know the new class is being created it, the setvisible(true) wasn't til the last line in the constructer and it wasn't getting that far because the table it's meant to receive is null.
So the code below isn't creating any data for the jtable
Expand|Select|Wrap|Line Numbers
- if(obj.equals(searchButton))
- {
- String keyword = searchField.getText();
- String columnSearch = (String)combo.getSelectedItem();
- try
- {
- //s.close();
- //con.close();
- String dbURL = "jdbc:odbc:" + "pWarehouseDatabase";
- Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
- Connection con = DriverManager.getConnection(dbURL, "","");
- Statement s = con.createStatement();
- // s.execute("INSERT INTO customers//(name, address, email, phone, purchase)");
- System.out.println(columnSearch + keyword);
- //s.execute("INSERT INTO customers values(?,'" +name + "','" + address + "','" + email + "','" + phone + "','" + purchase + "')");
- //String query = "INSERT INTO customers(id, customerName, customerAddress, customerEmail, customerNumber, customerPurchase) values(?, '" +name + "','" + address + "','" + email + "','" + phone + "','" + purchase + "')";
- Boolean test = s.execute("SELECT * FROM customers WHERE '" + columnSearch + "' = '" + keyword + "'");
- //s.execute("SELECT * FROM customers WHERE 'customerFirstName' = 'hope'");
- ResultSet resultSet = s.getResultSet(); // get any ResultSet that came from our query
- ResultSetMetaData metaData = resultSet.getMetaData();
- int numberOfColumns = metaData.getColumnCount();
- Vector columnNames = new Vector();
- for(int column = 0; column < numberOfColumns; column++)
- {
- columnNames.addElement(metaData.getColumnLabel(column+1));
- }
- Vector rows = new Vector();
- if(resultSet == null)
- {
- }
- while (resultSet.next())
- {
- System.out.print(test);
- System.out.println("TEST");
- Vector newRow = new Vector();
- for (int i = 1; i <= metaData.getColumnCount(); i++)
- {
- newRow.addElement(resultSet.getObject(i));
- }
- rows.addElement(newRow);
- }
- JTable table = new JTable(rows, columnNames);
- s.close();
- con.close();
- //dispose();
- SearchResults results = new SearchResults(table);
- }
- catch(Exception p)
- {
- }
- }