PreparedStatement cannot retrieve based on enumeration using Postgres - java

I'm developing a cinema Database, using PostgreSQL, and implementing it graphically with Java.
Everything is working fine but this: i have a PreparedStatement used in a method written like this:
getFilmByGenrePS = connection.prepareStatement("SELECT * FROM Film WHERE genre = ?");
public List<Film> getFilmByGenre(String Genre) throws SQLException{
getFilmByGenrePS.setString(1, Genre);
ResultSet rs = getFilmByGenrePS.executeQuery();
List<Film> list = new ArrayList<Film>();
while(rs.next()) {
Film f = new Film(rs.getInt("CodF"));
f.setFilmName(rs.getString("Name"));
f.setGenreFilm(rs.getString(3));
f.setLenght(rs.getInt("lenght"));
f.setCast(rs.getString("castfilm"));
f.setDirector(rs.getString("director"));
f.setDate(rs.getDate("date"));
f.setDateout(rs.getDate("dateout"));
list.add(f);
}
rs.close();
return list;
}
Genre is an ENUM TYPE in pgadmin.
Then, in my GUI, i have a Combobox with every Genre listed in it, written like this:
JComboBox GenrecomboBox2 = new JComboBox();
GenrecomboBox2.setBounds(60, 485, 150, 27);
panelViewFilm.add(GenrecomboBox2);
GenrecomboBox2.addItem("Animation");
GenrecomboBox2.addItem("Adventure");
GenrecomboBox2.addItem("Action");
Then, I have a button that, when pressed, takes my selected Genre from the Combobox, and passes it to the GetAllFilmByGenre() method as a parameter, so it can substitute it to the '?' in the PreparedStatement.
JButton btnViewFilmGenre = new JButton( new AbstractAction("Vai") {
#Override
public void actionPerformed( ActionEvent e ) {
String genreFilm = (String) GenrecomboBox.getSelectedItem();
DefaultTableModel model = (DefaultTableModel) tableViewFilm.getModel();
model.setRowCount(0);
try {
List<Film> listfilm = new ArrayList<>();
listfilm = filmdao.getFilmByGenre(genreFilm);
System.out.println(listfilm);
for (Film f : listfilm) {
Object[] o = new Object[8];
o[0] = f.getCodF();
o[1] = f.getFilmName();
o[2] = f.getDirector();
o[3] = f.getGenreFilm();
o[4] = f.getCast();
o[5] = f.getLenght();
o[6] = f.getDate();
o[7] = f.getDateEnd();
((DefaultTableModel) tableViewFilm.getModel()).addRow(o);
}
} catch (SQLException e1) {
System.out.println("");
}
}
});
Thing is, this does absolutely nothing. Overall, I think the code works. I used exact same versions of these code by retrieving different parameters and it works flawlessly.
Worth noting: the problem is that it doesn't populate my list at all. I tried to print the list after creating it, and nothing shows up. I get no SQLExceptions or anything.
I think the problem is the parameter i pass. Like, the query "SELECT * FROM FILM WHERE Genre = ?" (with ? substituted with, for example, 'Action') gets executed, but simply doesn't find anything that matches, thus giving me nothing. But if I write "SELECT * FROM FILM WHERE Genre = 'Action'", the query works.
I don't know how to resolve this. I used this exact Combobox for inserting values in my Database and it works. I tried for hours. I'm sure I'm slipping on something stupid, maybe the interaction between the String I use as a parameter in substitution of the '?' is wrong on syntax, but i can't get it. Any help?

Related

How do I create an update button using JComboBox dropdown list in netbeans 8.2?

I wanted to create an update button for a dropdown list that gets the data from the database in the netbeans. Tried this method below but couldn't get it to work. Any suggestions? Thanks alot.
public class ViewProduct extends javax.swing.JFrame {
ResultSet rs;
PreparedStatement pst;
Connection conn;
final void FillList(){
try{
//establish connection to table
String url = "jdbc:derby://localhost:1527/ProductInformation";
String username = "admin1";
String password = "admin1";
Connection conn = DriverManager.getConnection(url,username,password);
Statement stat = conn.createStatement();
// get data from table in sql database
String Query = "SELECT * FROM VIEWPRODUCT";
ResultSet rs = stat.executeQuery(Query);
//
DefaultListModel DLM = new DefaultListModel();
while(rs.next()){
JList list = new JList();
JComboBox ProductID_dropdown = new JComboBox();
DefaultComboBoxModel listModel = new DefaultComboBoxModel();
list.setModel(listModel);
ProductID_dropdown.setModel(listModel);
}
}catch(SQLException ex){
JOptionPane.showMessageDialog(null, ex.toString());
}
There are several things not right or missing in your code. For instance you have no GUI.
And as I dont know the exact structure of your project or your database, I have to improvise and give you a pseudo-code approach.
You will have to change and configure several things I am showing here.
First, create a JComboBox outside of your method and add it to a container like JPanel:
JPanel pane = new JPanel();
JComboBox productID_dropdown = new JComboBox();
JButton btn_updateViewProducts = new JButton("Update Results");
Here the Button for updating your results is added, it contains an ActionListener, that "listens" to when someone is clicking on the button.
Usually you want to retrieve a ResultSet and put its contents into variables, like in this example:
// ActionListener for the button
btn_updateViewProducts.add(new ActionListener{
#Override
// When the button is clicked...
public void actionPerformed(ActionEvent arg0) {
// ... get the results out of your database (here it's an example database!
// Configure for your own one)
ResultSet rs = stat.executeQuery(Query);
while(rs.next()){
// "de-construct" every result of the ResultList into variables
String query = "select COF_NAME, SUP_ID, PRICE, " + "SALES, TOTAL " + "from " + dbName + ".COFFEES";
String coffeeName = rs.getString("COF_NAME");
int supplierID = rs.getInt("SUP_ID");
float price = rs.getFloat("PRICE");
int sales = rs.getInt("SALES");
int total = rs.getInt("TOTAL");
System.out.println(coffeeName + "\t" + supplierID + "\t" + price + "\t" + sales + "\t" + total);
// Put items into JComboBox
productID_dropdown.addItem(coffeeName);
}
}
});
// Add the now filled JComboBox to the pane
pane.add(productID_dropdown);
Other issues you should think about:
GUI is missing
the method FillList is final, which seems unusual, any reason behind this? final in a method means, that it cant be overridden by subclasses, it this needed here?
FillList is a method and should be written with a small letter at the start due to coding convention, the same goes for ProductID_dropdown (coding convention)
most of the time it's quite okay to use short variables like rs for a ResultSet, but if your projects get bigger, it gets harder to keep all those variables in mind. Better: make them tell you, what they are. Instead of rs -> resultSetViewProduct or similar.
PreparedStatement pst is never used
I hope this helps. :)

Using PreparedStatement to sort records in MySQL

Im trying to write a code in which when a user will click an a "Sort by Name" button, my program will sort the records of my Database and put them in a JTable,combining 2 DB Tables with INNER JOIN. I have managed to do this by using a resultSet and selecting for example Ascending Order. But because I dont want to have 2 buttons, one for the ASC and one for the DESC, I thought of using preparedStatement and an showInputDialog in which the user will select if he wants to have an ASC or a DESC ordering and execute the order. Also, I remembered that some programs I've seen used a feature in which the first time you clicked the button it sorted DESC and if you pressed it again would order by ASC(havent managed to find in on the WEB).About my first thought, I tried to do it but I could get past this one
ResultSetMetaData mdsort = rssort.getMetaData();
I should have an ResultSet variable(rssort) to use getMetaData() but if I selected to make the program with my PreparedStatement i would get an error there. Any suggestions??
try{
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/test1?user=me&password=12345");
Statement stmtsort = conn.createStatement();
ResultSet rssort = stmtsort.executeQuery("SELECT * FROM consoles INNER JOIN hardware ON consoles.id=hardware.id ORDER BY consoles.name ASC");
// ERROR HERE!!! needs resultset,not preparedStatement
ResultSetMetaData mdsort = rssort.getMetaData();
columnCount = mdsort.getColumnCount();
String[] colssort = new String[columnCount];
for (i=1;i<= columnCount;i++)
{
colssort[i-1] = mdsort.getColumnName(i);
}
DefaultTableModel model = new DefaultTableModel(colssort,0);
while (rssort.next())
{
Object[] rowsort = new Object[columnCount];
for (i = 1 ; i <= columnCount ; i++)
{
rowsort[i-1] = rssort.getObject(i);
}
model.addRow(rowsort);
}
JTable table = new JTable(model);
model.fireTableDataChanged();
table.setCellSelectionEnabled(true);
table.setColumnSelectionAllowed(true);
table.setFillsViewportHeight(true);
table.setSurrendersFocusOnKeystroke(true);
table.setBounds(218,59,529,360);
frame.getContentPane().add(table);
model.fireTableDataChanged();
conn.close();
stmtsort.close();
rssort.close();
} catch (SQLException case1)
{case1.printStackTrace();
} catch (Exception case2)
{case2.printStackTrace();}
}
});
UPDATE
OK I managed to fix this issue with the getMetaData() but now the thing is that I dont use any ResultSet variables/instances and cant use next() method to create my DB.
String name = "SELECT * FROM consoles INNER JOIN hardware ON consoles.id=hardware.id ORDER BY consoles.name ?";
PreparedStatement psname = conn.prepareStatement(name);
String strin = JOptionPane.showInputDialog(null,"ASC or DESC order ? : ");
psname.setString(1,strin);
psname.executeUpdate();
ResultSetMetaData mdsort = psname.getMetaData();
int columnCount = mdsort.getColumnCount();
.
.
.
// error coming up here,because i deleted the ResultSet
while (psname.next())
.
.
.
Better make a bit more complex TableModel.
That is more optimal.
Keep the data from the ResultSet in an original TableModel.
Use a wrapping TableModel to sort, and maybe filter.
Use the ResultSetMetaData for the column type, if it is Number (Integer, BigDecimal, ...) then use that type instead of Object for the column type: gives a right aligned column.
Maybe first do an internet search for ResultSetTableModel; other peoply must have done it already.
try{
conn = DriverManager.getConnection("jdbc:mysql://localhost/test1?user=me&password=12345");
String strin = JOptionPane.showInputDialog(null,"ASC or DESC order ? : ");
stmtsortname = conn.createStatement();
rssortname = stmtsortname.executeQuery("SELECT * FROM consoles INNER JOIN hardware ON consoles.id=hardware.id ORDER BY consoles.name "+strin);
mdsortname = rssortname.getMetaData();
columnCount = mdsortname.getColumnCount();
String[] colssortname = new String[columnCount];
for (i=1;i<= columnCount;i++)
{
colssortname[i-1] = mdsortname.getColumnName(i);
}
model = new DefaultTableModel(colssortname,0);
while (rssortname.next())
{
Object[] rowsortname = new Object[columnCount];
for (i = 1 ; i <= columnCount ; i++)
{
rowsortname[i-1] = rssortname.getObject(i);
}
model.addRow(rowsortname);
}
table = new JTable(model);
model.fireTableDataChanged();
table.setCellSelectionEnabled(true);
table.setColumnSelectionAllowed(true)
table.setFillsViewportHeight(true);
table.setSurrendersFocusOnKeystroke(true);
table.setBounds(146,59,763,360);
frame.getContentPane().add(table);
model.fireTableDataChanged();
conn.close();
stmtsortname.close();
rssortname.close();
} catch (SQLException case1)
{
case1.printStackTrace();
}
catch (Exception case2)
{
case2.printStackTrace();
}
}
});

Make a List from a DB varchar data - NetBeans + Derby

my problems is that i dont know how to make a list (Jlist from NetBeans) with some data from my derby db
EX:
I have a form that saves the id,name,phone and email from a "client", and i want to show all the names from my previously registered clients in a list, the problem is that i need to convert the varchar data into a listmodel. How can i do that? I tried this, but didn't work:
while (rs.next()){
int id_col = rs.getInt("clientID");
String name = rs.getString("clientName");
client_listClients.setText(name);
}
It says that "couldn't find symbol" for .setText
EDIT using DefaultListModel
WORKING RESULT!
public void DoConnect( ) {
DefaultListModel model = new DefaultListModel();
client_listClients.setModel(model);
try {
String host = "jdbc:derby://localhost:1527/ANAIA_DB";
String uName = "*****";
String uPass = "*****";
con = DriverManager.getConnection(host, uName, uPass);
stmt = con.createStatement();
String sql = "SELECT * FROM APP.CLIENT";
rs = stmt.executeQuery(sql);
while (rs.next()){
int id_col = rs.getInt("clientID");
String name = rs.getString("clientName");
model.addElement(name);
}
} catch (SQLException err) {
JOptionPane.showMessageDialog(main.this, err.getMessage());
}
}
Thx aloooot #peeskillet
"It says that "couldant find symbol" for .setText"
JList has no method setText(). When in doubt, read the docs
Instead, you'll want to use a ListModel. If you don't want to create your own, you can use the provided DefaultListModel implementation. There you have the method addElement.
You first need to declare your your list with the model.
DefaultListModel model = new DefaultListModel();
JList client_listClients = new JList(model);
Then you can use the method addElement, which will automatically update the UI after all additions are made. No need to do anything with the list.
while (rs.next()){
String name = rs.getString("clientName");
model.addElement(name);
}
You can see more at How to use Lists, and focus on the section Creating a Model

How to set textfield value based on combobox options in Java?

I've a table in database named customer that have attribute like code and name. I've called the value of the customer in other table and displayed it using combo-box. I've displayed the code in combo-box, and all I wanna do is when I choose the code in combo-box, value 'name' can be displayed in text-field , the 'name' appear based on code.
here is my code :
try {
Connections con = new Connections();
con.setConnections();
String sql = "select * from customer";
Statement stat = con.conn.createStatement();
ResultSet rs=stat.executeQuery(sql);
while (rs.next()){
cmb_custCode.addItem(rs.getString("custCode"));
txt_custName.setText(rs.getString("custName")); // i'm confused in here, how can i call the name based on the code
}
}
catch(Exception e){
e.printStackTrace();
}
}
for example :
when the code 'B0001' is selected in the combobox, the Jtextfield must also display "Bob" , because code B0001 is belongs to Bob.
EDIT:
Ok. So let's say that you have a user Bob and his code is B001.
ItemStateChanged Method
...
String code = (String) cmb.getSelectedItem();
try{
String sql = "SELECT * FROM customer WHERE code='"+code"'"
PreparedStatement prst = con.prepareStatement();
ResultSet rs = prst.executeQuery(sql);
String name = "";
while(rs.next()){
name = rs.getString('name');
}
txt.setText(name);
}catch(Exception ex){
}
You shouldn't actually connect inside the itemStateChanged but this is just an example.
Take a look at this article. It tells you everything you need to know on how to use combo boxes.
http://docs.oracle.com/javase/tutorial/uiswing/components/combobox.html
In your own example code, you can declare the enclosing class as an ActionListener. then use the following after declaring your cmb_custCode
...
cmb_custCode.addActionListender(this);
...
when implementing an ActionListener, you have to implement the method actionPerformed(). I've cribbed the following from : http://docs.oracle.com/javase/tutorial/uiswing/components/combobox.html#listeners but adapted to your code
public void actionPerformed(ActionEvent e) {
JComboBox cb = (JComboBox)e.getSource();
String custCode = (String)cb.getSelectedItem();
updateLabel(custCode);
}
I've maintained the encapsulation of updateLabel(String custCode) from the example on the trail. You can assume that that method is defined as something like :
private void updateLabel(String code) {
txt_custName.setText(map_cust.get(code));
}
I've brough a Map into it with map_cust. It's just a map between code and name, stored in a field
Map<String, String> map_cust = new HashMap<String, String>();
and populated it, this would go in your code, just after retrieving the ResultSet
...
cmb_custCode.addItem(rs.getString("custCode"));
map_cust.put(rs.getString("custCode"), rs.getString("custName");
So, when you get your result set, you populate a map and your combo box, when the client picks and item from the combo box, he fires and actionPerformed, into the registered listener, where the event is manipulated to get the selected item, the String of which is the key to the map_cust, containing mapped key, value pairs, custCode mapped to custName. When that name is pulled from the map, it may be used to update the text of the label.
Here is my code, and it solved my problem. Hope it's useful for you too :).
private void fillText(){
String code = (String) cmb_custCode.getSelectedItem();
try {
Connections con = new Connections();
con.setConnections();
String sql = "select * from customer WHERE custCode='"+code +"'";
Statement stat = con.conn.createStatement();
ResultSet rs=stat.executeQuery(sql);
while (rs.next()){
txt_custName.setText(rs.getString("custName"));
}
}
catch(Exception e){
e.printStackTrace();
}

Storing Result set into an array

i know this should be simpel and im probably staring straight at the problem but once again im stuck and need the help of the code gurus.
im trying too take one row from a column in jdbc, and put them in an array.
i do this as follows:
public void fillContactList()
{
createConnection();
try
{
Statement stmt = conn.createStatement();
ResultSet namesList = stmt.executeQuery("SELECT name FROM Users");
try
{
while (namesList.next())
{
contactListNames[1] = namesList.getString(1);
System.out.println("" + contactListNames[1]);
}
}
catch(SQLException q)
{
}
conn.commit();
stmt.close();
conn.close();
}
catch(SQLException e)
{
}
creatConnection is an already defined method that does what it obviously does.
i creat my result set
while theres another one,
i store the string of that column into an array.
then print it out for good measure. too make sure its there.
the problem is that its storing the entire column into contactListNames[1]
i wanted to make it store column1 row 1 into [1]
then column 1 row 2 into [2]
i know i could do this with a loop. but i dont know too take only one row at a time from a single column. any ideas?
p.s ive read the api, i jsut cant see anything that fits.
You should use an ArrayList which provides all the logic to automatically extend the array.
List rowValues = new ArrayList();
while (namesList.next()) {
rowValues.add(namesList.getString(1));
}
// You can then put this back into an array if necessary
contactListNames = (String[]) rowValues.toArray(new String[rowValues.size()]);
Did you mean something like:
int i = 0;
ResultSet rs = stmt.executeQuery("select name from users");
while (rs.next()) {
String name = rs.getString("name");
names[i++] = name;
}

Categories