mercredi 8 mai 2019

How to Connect on a different database in Try-Catch in Java?

I'll try my best to make this as clear as possible since I'm new to Java. If there are parts which are unclear to you, please let me know. I apologize beforehand if the structure of my coding is messy.

I'm making a combobox event wherein if an item is selected (or changed), it will display items of that database and if the Update button is clicked, the program will connect to the database based on the item selected in the combobox.

The problem is that the variable cn after the if-else statement, has an error of "variable may have not been initialized".

Please note that I would like to focus on the condition since it is where I'm having difficulties and that the Update part is not a concern at this time.

   private void btn_UpdateActionPerformed(java.awt.event.ActionEvent evt) 
   {   
   String value2 = (String) combobox_location.getSelectedItem();
   String value4 = (String) combobox_category.getSelectedItem();
   try 
   {
    if(value2.equals(value4))
    {
    Connection cn = db.itemconnector.getConnection();
    }
    String sql = "UPDATE items set location = '"+value2+"'  where id = '"+value4+"' " ; //Please ignore this line for the now
    PreparedStatement ps1 = cn.prepareStatement(sql);
    ps1.execute();
    JOptionPane.showMessageDialog(null, "Update Successful");

    PreparedStatement ps2 = cn.prepareStatement(ref);
    ResultSet rs = ps2.executeQuery();
    DefaultTableModel tm = (DefaultTableModel)itemTable.getModel();
    tm.setRowCount(0);


    while(rs.next())
        {
            Object o[] = {rs.getInt("id"), rs.getString("location"), rs.getString("product_name"),rs.getString("product_category"),rs.getString("product_description"),rs.getInt("product_stock"), rs.getFloat("product_price"), rs.getString("product_status")};
            tm.addRow(o);

        } 




}


catch (Exception e)
{
    System.out.println("Update Error!");
}

}

I have a package db that has itemconnector class for database connection and here is my code I've written in it. I hope I have provided all necessary details for your assistance. If you need more info, please let me know. Thank you.

   package db;

   import java.sql.*;

   public class itemconnector {

/**
 *
 * @return
 * @throws Exception
 */
public static Connection getConnection() throws Exception
{
    Class.forName("com.mysql.jdbc.Driver");
    Connection cn = (Connection)
            DriverManager.getConnection("jdbc:mysql://192.168.1.50:3306/sales","root","");

    return cn;
}

public static Connection getConnection1() throws Exception
{
    Class.forName("com.mysql.jdbc.Driver");
    Connection cn = (Connection)
            DriverManager.getConnection("jdbc:mysql://192.168.1.50:3306/sales1","root","");

    return cn;
}

public static Connection getConnection2() throws Exception
{
    Class.forName("com.mysql.jdbc.Driver");
    Connection cn = (Connection)
            DriverManager.getConnection("jdbc:mysql://192.168.1.50:3306/sales2","root","");

    return cn;
}

}

Aucun commentaire:

Enregistrer un commentaire