is it possible to use information input into one screen, then displayed into another screen using sql commands?

this is how i am trying to get the information required:

String sql = "";
            if (cname.getText ().length () != 0)
            {
                sql = "SELECT CommonName, ScientificName,Type FROM Animals WHERE CommonName LIKE '" + cname.getText () + "%'";
            }

            else if (sname.getText ().length () != 0)
            {
                sql = "SELECT CommonName, ScientificName,Type FROM Animals WHERE ScientificName LIKE '" + sname.getText () + "%'";
            }

            ResultSet rs = stmt.executeQuery (sql);

            while (rs.next ())
            {
                String cnameInput = rs.getString ("CommonName");
                String snameInput = rs.getString ("ScientificName");
                String type = rs.getString ("Type");

                Search search = new Search (cnameInput,snameInput,type);
            }

Recommended Answers

All 14 Replies

you don't get information in a screen, you get it through code, the gui might be coded in the same file, but they're not related to one another.
and yes, you can pass information retrieved from a DB from one file containing a gui, to another file containing a gui, just to display it there, just like you can with any other data.

how would i go about doing this?

eg, if i have 3 files:
1. SearchScreen to input the data into the GUI
2. ConnectionClass to connect to the DB and get the information
3. Display to display the information retrieved

Best practice is to have a "controller" class that has the public main method, and manages all the others. It can open the first window, run the SQL query class, and open the results window, passing the necessary data.
The other classes need to accept an instance of the controller class in their constructors. The controller passes "this" when it calls their constructors, so then they all have access to the controller to call its methods. eg when the search screen has got its input data it calles a method in the controller, passing that data. The method in the controller then calls the SQL class, passing in the search data.
The point of all this is that each class just needs to know about the controller, rather than needing to access all the other classes, so it scales very well. It also separates the functionality very well - each class just does its own job, and only the controller knows how to fit it all together, but doesn't care how those parts work internally.

ok so far I have tried to code it and i have made it very simple(i have left out many other features) i am just trying to get it to diplay in my display screen but nothing happens. this is my Select screen(to search by name):

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Select extends JFrame implements ActionListener
{
    JMenu menu = new JMenu ("Menu");
    JMenuItem exitMenu = new JMenuItem ("Exit");
    JMenuItem homeMenu = new JMenuItem ("Home");

    JPanel texts = new JPanel ();
    JPanel labels = new JPanel ();
    JPanel buttons = new JPanel ();

    JLabel cName = new JLabel ("Common Name: ");
    JLabel sName = new JLabel ("Scientific Name: ");

    JTextField cname = new JTextField ();
    JTextField sname = new JTextField ();

    JButton back = new JButton ("Back");
    JButton searchButton = new JButton ("Search");

    Container cont;

    TwoOceans first = null;
    Display dis = null;

    SelectConnection db;

    public Select ()
    {
        super ("Search");
        setSize (640, 465);
        setResizable (true);
        setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

        cont = getContentPane ();
        cont.setLayout (new BorderLayout ());

        JMenuBar mb = new JMenuBar ();
        setJMenuBar (mb);

        mb.add (menu, new Integer (1));

        menu.add (homeMenu);
        menu.add (exitMenu);

        texts.setLayout (new GridLayout (2, 1));
        texts.add (cname);
        texts.add (sname);

        labels.setLayout (new GridLayout (2, 1));
        labels.add (cName);
        labels.add (sName);

        buttons.setLayout (new GridLayout (1, 2));
        buttons.add (back);
        buttons.add (searchButton);

        cont.add (labels, BorderLayout.WEST);
        cont.add (texts);
        cont.add (buttons, BorderLayout.SOUTH);

        back.addActionListener (this);
        searchButton.addActionListener (this);
        homeMenu.addActionListener (this);
        exitMenu.addActionListener (this);

        setVisible (true);
        db = new SelectConnection ();
    }


    public void actionPerformed (ActionEvent event)
    {
        Object source = event.getSource ();
        if (source == exitMenu)
        {
            System.exit (0);
        }

        if (source == homeMenu)
        {
            first = new TwoOceans ();
            this.dispose ();
        }
        if (source == back)
        {
            first = new TwoOceans ();
            this.dispose ();
        }

        if (source == searchButton)
        {
            dis = new Display ();
            db.option1 (cname, sname);
        }
    }


    public static void main (String[] args)
    {
        new Select ();
    }
}

this is the code for my SelectConnection(to get information from database):

import java.sql.*;
import java.util.*;
import javax.swing.*;

public class SelectConnection
{
    private Connection dbcon;
    private Statement stmt;

    Display dis = null;


    public SelectConnection ()
    {
        try
        {
            Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
        }
        catch (ClassNotFoundException c)
        {
            System.out.println ("Unable to load database driver");
        }

        try
        {
            String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=";
            database += "PAT.mdb;DriverID=22;READONLY=true}";
            dbcon = DriverManager.getConnection (database, "", "");

            stmt = dbcon.createStatement ();
        }

        catch (Exception e)
        {
            System.out.println ("Unable to connect to the database");
        }
    }


    public void option1 (JTextField cname, JTextField sname)
    {
        try
        {
            String sql = "";
            if (cname.getText ().length () != 0)
            {
                sql = "SELECT CommonName, ScientificName FROM Animals WHERE CommonName LIKE '" + cname.getText () + "%'";
            }

            else if (sname.getText ().length () != 0)
            {
                sql = "SELECT CommonName, ScientificName FROM Animals WHERE ScientificName LIKE '" + sname.getText () + "%'";
            }

            ResultSet rs = stmt.executeQuery (sql);

            while (rs.next ())
            {
                String cnameInput = rs.getString ("CommonName");
                String snameInput = rs.getString ("ScientificName");

                Search search = new Search (cnameInput);


            }
        }

        catch (Exception e)
        {
            System.out.println ("Error: " + e.toString ());
        }
    }
}

and this is my Display screen:

import java.awt.*; //dis
import java.awt.event.*;
import javax.swing.*;

public class Display extends JFrame implements ActionListener
{
    JMenu menu = new JMenu ("Menu");
    JMenuItem exitMenu = new JMenuItem ("Exit");
    JMenuItem homeMenu = new JMenuItem ("Home");

    JPanel texts = new JPanel ();
    JPanel labels = new JPanel ();
    JPanel buttons = new JPanel ();

    JLabel cName = new JLabel ("Common Name: ");
    JLabel sName = new JLabel ("Scientific Name: ");

    JTextField cname = new JTextField ();
    JTextField sname = new JTextField ();

    JButton back = new JButton ("Back");

    Container cont;

    TwoOceans first = null;

    SelectConnection db;

    public Display ()
    {
        super ("Display");
        setSize (640, 465);
        setResizable (true);
        setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

        cont = getContentPane ();
        cont.setLayout (new BorderLayout ());

        JMenuBar mb = new JMenuBar ();
        setJMenuBar (mb);

        mb.add (menu, new Integer (1));

        menu.add (homeMenu);
        menu.add (exitMenu);

        texts.setLayout (new GridLayout (2, 1));
        texts.add (cname);
        texts.add (sname);

        labels.setLayout (new GridLayout (2, 1));
        labels.add (cName);
        labels.add (sName);

        buttons.setLayout (new GridLayout (1, 2));
        buttons.add (back);

        cont.add (labels, BorderLayout.WEST);
        cont.add (texts);
        cont.add (buttons, BorderLayout.SOUTH);

        back.addActionListener (this);
        homeMenu.addActionListener (this);
        exitMenu.addActionListener (this);

        setVisible (true);
        db = new SelectConnection();
    }


    public void actionPerformed (ActionEvent event)
    {
        Object source = event.getSource ();
        if (source == exitMenu)
        {
            System.exit (0);
        }


        if (source == homeMenu)
        {
            first = new TwoOceans ();
            this.dispose ();
        }


        if (source == back)
        {
            first = new TwoOceans ();
            this.dispose ();
        }
    }


    public static void main (String[] args)
    {
        new Display ();
    }
}

i have also created this class:

public class Search
{
    private String cnameInput;
    private String snameInput;
    private String type;

    public Search (String cnameInputIn, String snameInputIn, String typeIn)
    {
        cnameInput = cnameInputIn;
        snameInput = snameInputIn;
        type = typeIn;
    }

    public Search (String cnameInputIn, String snameInputIn)
    {
        cnameInput = cnameInputIn;
    }



    public String getcnameInput ()
    {
        return cnameInput;
    }


    public String getsnameInput ()
    {
        return snameInput;
    }


    public String gettype ()
    {
        return type;
    }

    //nevermind about this section as I tried to use it for a text area
    public String SearchOutput1()
    {
        return "Common name: "+ cnameInput + "/nScientific name: " + snameInput + "/nType: " + type; 
    }
}

my problem is that if i type "Stingray" into the cname textfield it doesnt show the common name in the cname textfield and scientific name on the sname textfield in my Display screen. do you know if there is a way in which i can edit my code to display that information?

I doubt that anyone has the time go through all that code for you, so just start putting print statements into your code to confirm (a) that all the right methods are being called at the right time and (b) that important variables have the values you expect. With a bit of thought and a few more prints you will soon narrow the problem doen to one statement.

can you let me know if i have coded this part righy? i tried to set the textfields to the names recieved but I still dont have any output displayed:

public void option1 (JTextField cname, JTextField sname)
    {
        try
        {
            String sql = "";
            if (cname.getText ().length () != 0)
            {
                sql = "SELECT CommonName, ScientificName FROM Animals WHERE CommonName LIKE '" + cname.getText () + "%'";
            }

            else if (sname.getText ().length () != 0)
            {
                sql = "SELECT CommonName, ScientificName FROM Animals WHERE ScientificName LIKE '" + sname.getText () + "%'";
            }

            ResultSet rs = stmt.executeQuery (sql);

            while (rs.next ())
            {
                String cnameInput = rs.getString ("CommonName");
                String snameInput = rs.getString ("ScientificName");

                Search search = new Search (cnameInput, snameInput);
                dis = new Display();
                cname.setText (cnameInput);
                sname.setText (snameInput);
            }
        }

        catch (Exception e)
        {
            System.out.println ("Error: " + e.toString ());
        }
    }

do you know how to call methods from other classes and passing parameters to them?
do you know how to create an instance, or to call a method from an instance of a class from within a class, and passing parameters to that?

that, in the end, is all you are looking for. if you don't know how to do that, don't try to write gui's yet, and go back to the basics.

i have created a program before that uses a gui interface. my information was input into a screen, the data was fetched from the database and was displayed on the same screen. right now i am trying to do a similar thing, but the only difference is that i want it to be diplayed on another screen.

so far i have been fiddling around with my code and i see that this part of the code is wrong:

String cnameInput = rs.getString ("CommonName");
String snameInput = rs.getString ("ScientificName");
Search search = new Search (cnameInput, snameInput);
dis = new Display();
cname.setText (cnameInput);
sname.setText (snameInput);

as all it does is hide the display window i already have.

what error message does it give?
and what do you think that cname.setText does? it sets the value of cname, but not of cname that is an instance member of the dis screen.

I think you should rethink your logic. also, I see you have a 'dis' variable in several classes, why is that?

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.