How to achieve multiple combinations of JCombobox in java - java

I have 4 comboboxes...client.sengg,month,product and 1 JTable...so when I select any item from client want to display row in Jtable which contains these selected item..if I select sengg and product then values against this items want to display...if select client,month,product then these values should be displays...so currently only client is displays, So how to achieve it?
try
{
String query="Select * from enquiry where `client` =? or `Sales`=? or `month`=? or `Product`= ? " ;
PreparedStatement pst=conn.prepareStatement(query);
pst.setString(1,(String) client.getSelectedItem());
pst.setString(2,(String) sengg.getSelectedItem());
pst.setString(3,(String) product.getSelectedItem());
pst.setString(4,(String) month.getSelectedItem());
ResultSet rs=pst.executeQuery();
table.setModel(DbUtils.resultSetToTableModel(rs));
//return;
}
catch(Exception e)
{
e.printStackTrace();
}
Or query is right for it?

Related

Populate JCombobox values depending on another JCombobox

I have two JComboboxes A and B, so if I select any item form A then values related to the item selected in A should fill in JCombobox B. I tried this but get an error:
java.lang.ArrayIndexOutOfBoundsException: 0
at pst.setString(1, client.getSelectedItem().toString());
try
{
String query="select `User_Name` from Client where `Client_Name`='?' ";
PreparedStatement pst=conn.prepareStatement(query);
pst.setString(1, client.getSelectedItem().toString());
ResultSet rs=pst.executeQuery();
user.addItem("--Select--");
while(rs.next())
{
user.addItem(rs.getString("User_Name"));
}
// return;
System.out.println(query);
}
catch(Exception g)
{
g.printStackTrace();
}
The PresparedStatement takes care of quoting the parameter, when you use setString()
Try
String query="select User_Name from Client where Client_Name = ?";
PreparedStatement pst=conn.prepareStatement(query);
pst.setString(1, String.valueOf(client.getSelectedItem()));
I guess that when you use '?' the prepared statement does not count this as parameter and this is why you get the IndexOutOfBounce
you have done a mistake ,
no need to use '?' , it should be just Client_Name=?

Having Difficulty In Searching on My JAva Program

I want to search some data from database and set it to jtable. So far I have written this code:
try
{
String sql="select * from hotelinfo where Hotel_Name=?;
pst=conn.prepareStatement(sql);
pst.setString(1,searchtxt.getText());
rs=pst.executeQuery();
hotelinfo.setModel(DbUtils.resultSetToTableModel(rs));
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(null, ex);
}
this code is working but gives only one row i want whole rows which is related to my search
As to your question, you should use like keyword in your select query instead of = sign.
Try with below code:
String sql = "select * from hotelinfo where Hotel_Name LIKE ?";
pst= connection.prepareStatement(sql);
pst.setString(1, "%" + searchtxt.getText() + "%");
rs= preparedStatement.executeQuery();

Jtable selected row in netbeans that is bounded to sql server

I fill a jtable(tbl_student) in netbeans by this code:
String[][] result;
result = stu.Search(txt_search.getText());
String hdr[] = {"code", "name", "family"};
tbl_student = new JTable(result, hdr);
jScrollPane1.setViewportView(tbl_student);
tbl_student.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
That Search() function is a select all query from a table in sql server database.
Now, I want to find selected row in this table. How can I do it?
My code in mouse click event of this jtable is not work!
What can I do?
If you need to get some data when you clicked a row in a JTable use the following code.
int row=tbl_student.getSelectedRow();
String Table_data=(tbl_student.getModel().getValueAt(row, 0).toString()); // In here 0 means the column number.
//If you have a JTable with 5 columns; 0 is the 1st column and 4 is the last (5th) column.
If you want to get data in a database table to a JTable, their is a library called rs2xml.jar with the help of that library you can simply fill a JTable with database data.
You can download that library from HERE.
After downloading library use the following code to fill your JTable.
Connection conn=null;
ResultSet rs=null;
PreparedStatement pst=null;
try{
String sql="SELECT * FROM table_name"
conn=java_connect.ConnecrDb(); //Database connecting class
pst=conn.prepareStatement(sql);
rs=pst.executeQuery();
tbl_student.setModel(DbUtils.resultSetToTableModel(rs));
}
catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}
finally{
try{
rs.close();
pst.close();
}
catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}
}

Displaying multiple query results in a single jTable returning unusual results and missing info

What I want to do is execute multiple two queries and the show the results in a jTable. I tried using the UNION but weird results came out in the table like this:
[B#58f67fc 3
[B#9f3d43e 1
[B#66f3378c 3
[B#69c3fd21 4
[B#421fb7c6 3
instead of actual usernames
I already looked at: mysql insert multi row query result into table?
This is what I used:
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection CC = DriverManager.getConnection("jdbc:mysql://localhost:3306/pulsedb", "root", "carrizo");
String getID = "SELECT stuff_id,med_specialty FROM mstuffinfo UNION ALL SELECT username,user_level FROM mstufflogin";
PreparedStatement PS = CC.prepareStatement(getID);
ResultSet RS = PS.executeQuery();
jTable1.setModel(DbUtils.resultSetToTableModel(RS));
}
catch(SQLException SQ)
{
SQ.printStackTrace();
SQ.getErrorCode();
}
catch (ClassNotFoundException ex) {
Logger.getLogger(PulseSTUFFLIST.class.getName()).log(Level.SEVERE, null, ex);
}
}

count a value in database

How I can count how many times a value appear in a table using jdbc? I have 200 possible values and 4 columns for records in the table.
May be by
Select count(*) from table where item = 'value';
If you want count for all 200 values then you can try:
select item,count(*) from table group by item;
Demo code:--
try {
java.sql.Statement s = conn.createStatement();
java.sql.ResultSet r = s.executeQuery("select item,count(*) from table group by item;");
while (r.next()) {
System.out.println(r.getString(1) + " "
+ r.getString(2));
}
} catch (Exception e) {
System.out.println(e);
System.exit(0);
}

Categories