I'm seeking how to execute ActionPerformed on the first combobox in order to filter the next one. This is based on Mysql.
I'm using the following codes to fill the first combo, which is working fine
Vector<String> comboBoxItems = new Vector<String>();
final DefaultComboBoxModel<String> model = new DefaultComboBoxModel<String>(comboBoxItems);
try {
new MconnectionDB();
} catch (SQLException e2) {
e2.printStackTrace();
}
PreparedStatement st1 = null;
ResultSet rs1=null;
String strPro = "";
String sql1 ="select distinct T_AdressePro from t_adresse order by T_AdressePro";
try {
st1= MconnectionDB.con.prepareStatement(sql1);
rs1 = st1.executeQuery();
} catch (SQLException e1) {
e1.printStackTrace();
}
try {
while (rs1.next()){
strPro =rs1.getString("T_AdressePro");
comboBoxItems.add(strPro);
comboBoxPro= new JComboBox<String>(model);
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
try {
rs1.close();
st1.close();
new MdeconnectionDB();
}
catch (SQLException e) {
e.printStackTrace();
}
}
And then, I'm adding another similar code in ActionPerformed on the first combo to filter the second one:
comboBoxPro.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Vector<String> comboBoxItemsC = new Vector<String>();
final DefaultComboBoxModel<String> modelC = new DefaultComboBoxModel<String>(comboBoxItemsC);
String strCir = "";
PreparedStatement st2 = null;
ResultSet rs2=null;
String sql2 ="select distinct T_AdresseCir from t_adresse where T_AdressePro=? order by T_AdresseCir";
try {
new MconnectionDB();
} catch (SQLException e2) {
e2.printStackTrace();
}
comboBoxPro.getSelectedItem();
strProCombo = comboBoxPro.getSelectedItem().toString();
System.out.println(strProCombo);
try {
st2= MconnectionDB.con.prepareStatement(sql2);
st2.setString(1, strProCombo); //strProCombo
rs2 = st2.executeQuery();
}catch (SQLException e1) {
e1.printStackTrace();
}
try {
while (rs2.next()){
System.out.println(rs2.getString("T_AdresseCir"));
strCir =rs2.getString("T_AdresseCir");
comboBoxItemsC.add(strCir);
comboBoxCir= new JComboBox<String>(modelC);
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
try {
rs2.close();
st2.close();
new MdeconnectionDB();
}
catch (SQLException e) {
e.printStackTrace();
}
}
}
});
I'm noticing that the follogwing code "System.out.println(rs2.getString("T_AdresseCir"));" is returning the expected result but not the combobox. Still empty. Your support please, thanks.
In your actionPerformed method you are creating new JComboBox but you don't add it to gui. That is probably why you can't see its contents. You should instead create new ComboBoxModel and set it on existing JComboBox. You should probably use try with resources too to make your code more readable.
Pseudo code (I don't have your database):
// create new model for your comboBox
DefaultComboBoxModel<String> model = new DefaultComboBoxModel<String>();
// fill model with data
try (Connection con = /*get connection to your db by any means necessary*/;
PreparedStatement stmt = con.prepareStatement(/*your query*/);
ResultSet rs = stmt.executeQuery();) {
while (rs.next()) {
model.addElement(rs.getString(/*your column*/));
}
comboBox.setModel(model); // set model for your JComboBox
} /*catch and all that*/
// no need for finally because try-with-resources.
Newbie to JTable. Just got in today.
So I am able to populate my data from database into a JTable.
I am currently stuck in saving the edited cell/s into the database.
I've read many articles. But I haven't found the one
Just a button then save!
Here's my code
public class DBEngine {
Connection con = IQConnection.getConnectionMgrInstance().getConnection();
PreparedStatement pstm = null;
ResultSet rs = null;
public Vector getEmployee() throws Exception{
Vector<Vector<String>> employeeVector = new Vector<Vector<String>>();
try {
if (con != null) {
String query = "SELECT * FROM OGG.TBLSAMPLE";
try {
pstm = con.prepareStatement(query);
rs = pstm.executeQuery();
while(rs.next()) {
Vector<String> employee = new Vector<String>();
employee.add(rs.getString(1)); //Empid
employee.add(rs.getString(2)); //name
employee.add(rs.getString(3)); //position
employee.add(rs.getString(4)); //department
employeeVector.add(employee);
}
} catch (Exception e) {
e.printStackTrace();
}
}else{
//lbl_err1.setText("Error");
}
} catch (Exception e) {
e.printStackTrace();
//flag = false;
//lbl_err1.setText("Failed!");
} finally {
try{
if(rs != null){
rs.close();
}
}catch(Exception e){
}
try{
if(pstm != null){
pstm.close();
}
}catch(Exception e){
}
}
return employeeVector;
}
}
Thanks!
use preparedstatement like this way for inserting into DB
PreparedStatement pt=con.prepareStatement("insert into mytable values(?) ");
pt.setString(1,ID);
int result=pt.executeUpdate();
if you want to update then use this way
PreparedStatement pt=con.prepareStatement("update mytable set column_name=? ");
pt.setString(1,ID);
int result=pt.executeUpdate();
When i run my website it on glassfish server everything works fine but after some time its stop responding and I need to restart glassfish.. I think its cause I not closing the connection. Can someone tell me if this is the problem? if yes how to close it? Here is one of my function.
public Album get_album (String title)
{
try{
//creates a connection to the server
Connection cn = getCon().getConnection();
//prepare my sql string
String sql = "SELECT * FROM albums WHERE Title = ?";
//create prepared statement
PreparedStatement pst = cn.prepareStatement(sql);
//set sql parameters
pst.setString(1, title);
//call the statement and retrieve results
ResultSet rs = pst.executeQuery();
if(rs.next()) {
Album a = new Album();
a.setIdAlbum(rs.getInt("idAlbum"));
a.setTitle(rs.getString("Title"));
a.setYear(rs.getInt("Year"));
a.setIdArtist(rs.getInt("idArtist"));
a.setIdUser(rs.getInt("idUser"));
a.setLike(rs.getInt("Like"));
a.setDislike(rs.getInt("Dislike"));
a.setNeutral(rs.getInt("Neutral"));
a.setViews(rs.getInt("Views"));
return a;
}
}
catch (Exception e) {
String msg = e.getMessage();
}
return null;
}
Assumming the unique error in your application is for not closing the resources after using them, your code should change to:
public Album get_album (String title) {
Connection cn = null;
PreparedStatement pst = null;
ResultSet rs = null;
Album a = null;
try{
//creates a connection to the server
cn = getCon().getConnection();
//prepare my sql string
String sql = "SELECT * FROM albums WHERE Title = ?";
//create prepared statement
pst = cn.prepareStatement(sql);
//set sql parameters
pst.setString(1, title);
//call the statement and retrieve results
rs = pst.executeQuery();
if (rs.next()) {
a = new Album();
a.setIdAlbum(rs.getInt("idAlbum"));
a.setTitle(rs.getString("Title"));
a.setYear(rs.getInt("Year"));
a.setIdArtist(rs.getInt("idArtist"));
a.setIdUser(rs.getInt("idUser"));
a.setLike(rs.getInt("Like"));
a.setDislike(rs.getInt("Dislike"));
a.setNeutral(rs.getInt("Neutral"));
a.setViews(rs.getInt("Views"));
//don't return inside try/catch
//return a;
}
} catch (Exception e) {
String msg = e.getMessage();
//handle your exceptions
//e.g. show them in a logger at least
e.printStacktrace(); //this is not the best way
//this will do it if you have configured a logger for your app
//logger.error("Error when retrieving album.", e);
} finally {
closeResultSet(rs);
closeStatement(pst);
closeConnection(cn);
}
return a;
}
public void closeConnection(Connection con) {
if (con != null) {
try {
con.close();
} catch (SQLException e) {
//handle the exception...
}
}
}
public void closeStatement(Statement st) {
if (st!= null) {
try {
st.close();
} catch (SQLException e) {
//handle the exception...
}
}
}
public void closeResultSet(ResultSet rs) {
if (rs!= null) {
try {
rs.close();
} catch (SQLException e) {
//handle the exception...
}
}
}
I would like to be able to select a data from database and display it using the comboBox.
I have the following code but it does not display the data in the comboBox. I do realize there is codes missing to display the data and my SQL statement is not correct. I am just not sure what I can do any advise is appreciated.
try {
Statement st = db.con.createStatement();
ResultSet rs = st.executeQuery("SELECT Name, Size, Price FROM item WHERE Name=" + comboBox_1.getToolkit());
JOptionPane.showMessageDialog(frame, "displayed");
while (rs.next()) {
String name = rs.getString("Name");
String size = rs.getString("size");
String price = rs.getString("price");
textArea_Name.append(name);
textArea_size.append(size);
textArea_price.append(price);
comboBox_1.addItem(rs.getString("Name"));
comboBox_1.getSelectedItem();
}}
catch (SQLException e ) {
System.out.println("user not added");
e.printStackTrace();
}
}
});
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn= DriverManager.getConnection("jdbc:odbc:driverName");
Statement st = conn.createStatement();
ResultSet r=st.executeQuery("select * from tableName");
while (r.next()) {
JComboBox.addItem(r.getString("Key_Coloumn_Name"));
}
conn.close();
} catch (Exception e) {
JOptionPane.showMessageDialog(null,"Failed to Connect to Database","Error Connection", JOptionPane.WARNING_MESSAGE);
System.exit(0);
}
Try this tutorial. It does exactly that.
http://www.roseindia.net/tutorial/java/swing/comboboxwithdatabaseValues.html
try {
Statement stmt = db.con.createStatement();
ResultSet rs = stmt5.executeQuery("select * from tbl_your_table");
while (rs.next()) {
String pat = rs.getString("name");
jComboBox.addItem(pat);
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
}
con = DriverManager.getConnection("jdbc:mysql://:3306/database","user","password");
stat = con.createStatement();
ResultSet rs = stat.executeQuery("select name from student;");
while(rs.next()){
jComboBox1.addItem(rs.getString("name"));
}
rs.close();
stat.close();
con.close();
comb = new JComboBox();
DefaultComboBoxModel dftm = (DefaultComboBoxModel) comb.getModel();
ArrayList<stock_data> list = userLists();
row = new Object[1];
for (int i = 0; i < list.size(); i++) {
row[0] = list.get(i).get_item();
dftm.addElement(row);
// comb.setModel(dftm);
}
Then create your method and also remember you need a stock_data Class
private ArrayList<stock_data> userLists() {
try {
connection = localhost.dbConnector();
ArrayList<stock_data> usersList = new ArrayList();
try {
String query = "Select * from itemlist";
Statement st = connection.createStatement();
ResultSet rs = st.executeQuery(query);
stock_data users;
while (rs.next()) {
// users = new stock_data(rs.getString("name"));
// usersList.add(users);
comb.addItem(rs.getString("name"));
}
rs.close();
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "blah blah");
e.printStackTrace();
}
return usersList;
} catch (Exception e1) {
e1.printStackTrace();
}
return null;
}
org.apache.commons.dbcp.DelegatingPreparedStatement
is closed
Could i know in which situations this exception will come.
I closed all result sets and prepared statements.
How can i solve this problem.
Code :
public int UpdateMovementLines(List<MaterialRequestIssuanceVO> mlinelist,String projId,String documentno,String user){
int count = 1;
int line = 0;
String uom = null;
String projLocatorId = null;
String projWarehouseId = null;
String warehouseLocatorId = null;
String issuanceId = null;
String movementLineId =null;
String pinstanceId = null;
String sqlQry = null;
String whLocatorId = null;
PreparedStatement ps = null;
PreparedStatement ps1 = null;
PreparedStatement ps2 = null;
PreparedStatement ps3 = null;
PreparedStatement ps4 = null;
PreparedStatement ps5 = null;
ResultSet rs = null;
ResultSet rs1 = null;
ResultSet rs2 = null;
ResultSet rs3 = null;
try{
conn.setAutoCommit(false);
try{
sqlQry="INSERT INTO m_movement (m_movement_id, AD_CLIENT_ID, AD_ORG_ID, CREATED, CREATEDBY, UPDATED, UPDATEDBY," +
"name, movementdate, posted, processing, move_fromto_locator,documentno) VALUES " +
"(?,?,?,NOW(),?,NOW(),?,to_char(now(),'DD-MM-YYYY'),now(),?,?,?,?)";
ps = conn.prepareStatement(sqlQry);
for(MaterialRequestIssuanceVO movementvo:mlinelist){
issuanceId = movementvo.getIssuanceid();
ps.setString(1, issuanceId);
ps.setString(2,movementvo.getClientid());
ps.setString(3,movementvo.getOrgid());
ps.setString(4, movementvo.getCreatedby());
ps.setString(5,movementvo.getUpdatedby());
ps.setString(6,"N");
ps.setString(7,"N");
ps.setString(8,"N");
ps.setString(9, documentno);
count=ps.executeUpdate();
}
}
catch (SQLException e) {
// TODO Auto-generated catch block
log4j.info("Inside DB Line saveMRIssuanceMovementData Exception"+e);
}
finally
{
try
{
ps.close();
log4j.info("Inside Line Finally");
} catch (SQLException e) {
e.printStackTrace();
}
}
ps=conn.prepareStatement("select c_uom_id as uom from m_product where m_product_id = ?");
for(MaterialRequestIssuanceVO movementvo:mlinelist){
line = line +10;
ps.setString(1, movementvo.getMaterialid());
rs = ps.executeQuery();
while(rs.next())
{
uom = rs.getString("uom");
log4j.info("Uom: "+uom);
}
try{
ps2=conn.prepareStatement("select m_locator_id as locatorid from m_locator where m_warehouse_id = ?");
ps2.setString(1, movementvo.getWarehouseId());
rs2 = ps2.executeQuery();
while(rs2.next())
{
warehouseLocatorId = rs2.getString("locatorid");
log4j.info("warehouseLocatorId: "+warehouseLocatorId);
}
}catch(SQLException e){
log4j.info("Warehouse Locator Exception: "+e);
}
finally{
rs2.close();
ps2.close();
}
try{
ps3=conn.prepareStatement("select m_locator_id as locatorid from m_locator where m_warehouse_id=? and value like ?");
ps3.setString(1, movementvo.getWarehouseId());
ps3.setString(2, projId);
rs3 = ps3.executeQuery();
if(rs3.next())
{
projLocatorId = rs3.getString("locatorid");
log4j.info("projLocatorId: "+projLocatorId);
}
else
{
sqlQry="INSERT INTO m_locator (m_locator_id, AD_CLIENT_ID, AD_ORG_ID, CREATED, CREATEDBY, UPDATED, UPDATEDBY," +
"value, m_warehouse_id, priorityno, x,y, z) VALUES " +
"(?,?,?,NOW(),?,NOW(),?,?,?,?,?,?,?)";
ps4 = conn.prepareStatement(sqlQry);
try
{
whLocatorId = SequenceIdData.getUUID();
log4j.info("issueid: "+whLocatorId);
ps4.setString(1, whLocatorId);
log4j.info("Client Id: "+movementvo.getClientid());
ps4.setString(2,movementvo.getClientid());
log4j.info("Orgid: "+movementvo.getOrgid());
ps4.setString(3,movementvo.getOrgid());
ps4.setString(4, movementvo.getCreatedby());
ps4.setString(5,movementvo.getUpdatedby());
ps4.setString(6,projId);
ps4.setString(7,movementvo.getWarehouseId());
ps4.setInt(8,50);
ps4.setString(9,"x");
ps4.setString(10,"y");
ps4.setString(11,"z");
count=ps4.executeUpdate();
if(count == 1)
projLocatorId = whLocatorId;
}
catch(SQLException e)
{
log4j.info("M_Locator Exception: "+e);
}
finally
{
ps4.close();
}
log4j.info("whLocatorId projLocatorId: "+projLocatorId);
}
}catch(SQLException e){
log4j.info("Locator Exception: "+e);
}
finally{
rs3.close();
ps3.close();
}
try{
sqlQry="INSERT INTO m_movementline (m_movementline_id, AD_CLIENT_ID, AD_ORG_ID, CREATED, CREATEDBY, UPDATED, UPDATEDBY," +
"m_movement_id, m_locator_id, m_locatorto_id, m_product_id,line, movementqty,c_uom_id,m_attributesetinstance_id) VALUES " +
"(?,?,?,NOW(),?,NOW(),?,?,?,?,?,?,?,?,?)";
ps1 = conn.prepareStatement(sqlQry);
movementLineId = SequenceIdData.getUUID();
ps1.setString(1, movementLineId);
ps1.setString(2,movementvo.getClientid());
ps1.setString(3,movementvo.getOrgid());
ps1.setString(4, movementvo.getCreatedby());
ps1.setString(5,movementvo.getUpdatedby());
ps1.setString(6,issuanceId);
ps1.setString(7,warehouseLocatorId);
ps1.setString(8,projLocatorId);
ps1.setString(9,movementvo.getMaterialid());
ps1.setInt(10,line);
ps1.setInt(11,Integer.parseInt(movementvo.getIssuedqty()));
ps1.setString(12,uom);
ps1.setString(13,"0");
count=ps1.executeUpdate();
}
catch(SQLException e){
log4j.info("Inside DB MoveLines SQLException"+e.getMessage());
}
finally
{
try
{
ps1.close();
log4j.info("Inside movement Line Finally");
} catch (SQLException e) {
e.printStackTrace();
}
}
}
sqlQry="INSERT INTO ad_pinstance (ad_pinstance_id, AD_CLIENT_ID, AD_ORG_ID, CREATED, CREATEDBY, UPDATED, UPDATEDBY," +
"ad_process_id, record_id, isprocessing, ad_user_id,result) VALUES " +
"(?,?,?,NOW(),?,NOW(),?,?,?,?,?,?)";
ps5 = conn.prepareStatement(sqlQry);
for(MaterialRequestIssuanceVO movementvo:mlinelist){
try{
pinstanceId = SequenceIdData.getUUID();
log4j.info("pinstanceId: "+pinstanceId);
ps5.setString(1, pinstanceId);
log4j.info("Client Id: "+movementvo.getClientid());
ps5.setString(2,movementvo.getClientid());
log4j.info("Orgid: "+movementvo.getOrgid());
ps5.setString(3,movementvo.getOrgid());
ps5.setString(4, movementvo.getCreatedby());
ps5.setString(5,movementvo.getUpdatedby());
ps5.setString(6,"122");
ps5.setString(7,issuanceId);
ps5.setString(8,"N");
ps5.setString(9,user);
ps5.setInt(10, Integer.parseInt("1"));
count=ps5.executeUpdate();
}
catch(SQLException e){
log4j.info("saveMRIssuanceMovementData Line SQLException"+e.getMessage());
}
finally
{
try
{
ps5.close();
log4j.info("Inside movement Line Finally");
} catch (SQLException e) {
e.printStackTrace();
}
}
try{
ps=conn.prepareStatement("select m_movement_post(?)");
ps.setString(1, pinstanceId);
rs = ps.executeQuery();
while(rs.next()){
log4j.info("Result Set: "+rs.getString(1));
}
}catch(SQLException e){
log4j.info("Movement Post Exception: "+e);
}
finally{
ps.close();
}
}
conn.commit();
} catch (Exception e) {
try {
conn.rollback();
count = 0;
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//Above Mensined Exception Catching hear
log4j.info("Inside DB saveMRIssuanceMovementData Line SQLException"+e.getMessage());
}
finally
{
try
{
if(conn != null)
conn.close();
}
catch(SQLException e)
{
}
}
return count;
}
PreparedStatement is closed
You can get this exception when you're trying to (re)use a PreparedStatement while it has been closed. Check the line number of the first line in the stacktrace. It should hint which PreparedStatement it is talking about. Then backtrack its use in the code and fix code accordingly.
Judging the flood of code you've posted, I suspect that it's the ps5 which is been created before a for loop and been closed inside the for loop. Here's an extract of relevance from your code:
ps5 = conn.prepareStatement(sqlQry);
for (MaterialRequestIssuanceVO movementvo : mlinelist) {
try {
ps5.setString(1, string);
ps5.executeUpdate();
} finally {
ps5.close(); // You're closing inside the loop!
}
}
The next iteration in the loop won't be able to reuse the same PreparedStatement anymore. The fix is obvious: close it after completion of the for loop.
try {
ps5 = conn.prepareStatement(sqlQry);
for (MaterialRequestIssuanceVO movementvo : mlinelist) {
ps5.setString(1, string);
ps5.executeUpdate();
}
} finally {
ps5.close();
}
That said, logging all exceptions as Info or doing only e.printStackTrace() and suppressing them and continuing the code flow isn't always a good idea. Log them as Error and then hard-throw thereafter.
} catch (Exception e) {
logger.error("Your message", e);
throw e;
}
Rethrowing isn't needed for exceptions during close, but logging them as Warn is useful.
Last but not least, consider refactoring the exceptionally large method block into separate and sensible methods (tasks) ;)