I have developed a Java GUI App.I want to connect that with mysql.I have already connected a seperate java file with mysql.That java class is also in the same package in my Netbeans project.I want to know is it possible connect DB by using that seperate java class?

I think that question needs to be clarified. Does the existing java file handle data from mysql that you now want to use in your GUI?

Yes of course.You have got it...:)

You just need public methods in your existing java file that the GUI can call to access the data it needs. The existing code shields the GUI from all the complexity of the SQL and separates your application into layers that make it much simpler to understand and support.

I'm not sure if this is what you want. Here is an example. We may have different implementation on fetching data but the point here is the abstraction so that your view does not know how the data are fetched. For the sake of completeness and clarity I added a few classes.

//Sample Person class

public class Person {
    private int personId;
    private String name;
    private String address;
    //default constructor
    public Person(){};
    //
    public Person(int personId, String name, String address){
        this.personId = personId;
        this.name = name;
        this.address = address;
    }

    //setters and getters
}

//an interface that defines all operations in your Person database table

public interface PersonDao{
    //a function that gets all Person in your database
    public List<Person> selectAllPerson();
    // add more methods you need
}

implementation on how you want your data fetched from database

public class PersonDaoImpl implements PersonDao{
    Connection connection;
    PreparedStatement statement;

    public List<Person> selectAllPerson() {
        List<Person> persons = new ArrayList<Person>();
        try {
            //change this part depending on your database
            Class.forName("jdbc:sqlite:database/database.db");
            connection = DriverManager.getConnection("org.sqlite.JDBC");
            statement = connection.prepareStatement("SELECT * FROM Person");
            ResultSet rs = statement.executeQuery();
            while(rs.next()){
                persons.add(new Person(rs.getInt("personId"), rs.getString("name"), 
                        rs.getString("address")));
            }
        } catch (Exception ex) {
            Logger.getLogger(UserDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        }finally{
            try{
                if(connection != null){connection.close();}
                if(statement != null){statement.close();}
            }catch(Exception ex){}
        }
        
        return groups;
    }

    //more methods
}

This is in the view part. Assume I have already define my TableModel.

//get the model of  JTable called table
    DefaultTableModel model = (DefaultTableModel)table.getModel();
    //create the object which calls a function to fetch data from DB.
    PersonDaoImpl personDao = new PersonDaoImpl();
    List<Person> persons = personDao.selectAllPerson();
    for(Person person:persons){
        //add each person to your table. 
        model.addRow(new Object[]{person.getName(),person.getAddress()});
    }

I hope this helps.

Thanks for your help.I'll try your solutions.If i have further problem i'll post it here.Thank you again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.