HEy everyone. I've been workign on this for a few days now and it's due tomorrow, so I thought I would ask a developer community for any input. Here is what is supposed to be happening.

The Server starts.
The Client Starts.
The user enters a table name into the client testfield and hits enter.
The client connects to the server, the server connects to the database, the server sends a query to the database and retrieves the table, the server displays the table and sends it to the client at the same time, the client displays the table.

Here is my hang up. When i display the table on the client, all is good as long as i can hard code how many colums are in it.

I'll post both .java source codes so you guys can review.

Here is the FinalServer.java

import java.awt.BorderLayout;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;
import java.util.StringTokenizer;
import javax.swing.*;

public class FinalServer extends JFrame
{
    class ThreadHandler extends Thread
    {

        public void run()
        {
            try
            {
                BufferedReader isFromClient = new BufferedReader(new InputStreamReader(connectToClient.getInputStream()));
                PrintWriter osToClient = new PrintWriter(connectToClient.getOutputStream(), true);
                do
                {
                	StringTokenizer st = new StringTokenizer(isFromClient.readLine());
                    String tableName = st.nextToken();
                    try {
                        String queryString = "select * from " + tableName;

                        ResultSet resultSet = stmt.executeQuery(queryString);

                        ResultSetMetaData rsMetaData = resultSet.getMetaData();
                      //  Integer number = rsMetaData.getColumnCount();
                        //osToClient.println(number + 1);
                        for (int i = 1; i <= rsMetaData.getColumnCount(); i++) {
                          jta.append(rsMetaData.getColumnName(i) + "    ");
                          osToClient.println(rsMetaData.getColumnName(i) + "    ");
                          
                        }
                       // osToClient.println("\n");
                        jta.append("\n");

                        // Iterate through the result and print the student names
                        while (resultSet.next()) {
                        	String response = null;
                          for (int i = 1; i <= rsMetaData.getColumnCount(); i++) {
                            jta.append(resultSet.getObject(i) + "     ");
                            response = (resultSet.getObject(i) + "     ");
                       
                           osToClient.println(response);
                          }
                          //osToClient.println("\n");
                          jta.append("\n");
                        }
                        osToClient.close();
                        

                      }
                      catch (SQLException ex) {
                        ex.printStackTrace();
                        osToClient.println("Error, Table not found.");
                        osToClient.close();
                      }
                } while(osToClient != null );
            }
            catch(IOException e)
            {
                System.err.println(e);
            }
        }

        private Socket connectToClient;
        private int counter;
        final FinalServer this$0;

        public ThreadHandler(Socket c, int i)
        {
            this$0 = FinalServer.this;
            connectToClient = c;
            counter = i;
        }
    }


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

    public FinalServer()
    {
        jta = new JTextArea();
        getContentPane().setLayout(new BorderLayout());
        getContentPane().add(new JScrollPane(jta), "Center");
        setTitle("Exercise30_2Server");
        setSize(500, 300);
        setDefaultCloseOperation(3);
        setLocationRelativeTo(null);
        setVisible(true);
        try
        {
            ServerSocket s = new ServerSocket(8000);
            jta.append((new StringBuilder("Server started at ")).append(new Date()).append('\n').toString());
            int i = 0;
            do
            {
                Socket connectToClient = s.accept();
                jta.append((new StringBuilder("Starting thread ")).append(i).append(" at ").append(new Date()).append('\n').toString());
                ThreadHandler t = new ThreadHandler(connectToClient, i);
                initializeDB();
                t.start();
                i++;
            } while(true);
        }
        catch(IOException ex)
        {
            System.err.println(ex);
        }
    }
    
    private void initializeDB() {
        try {
          // Load the JDBC driver
          Class.forName("com.mysql.jdbc.Driver");
//          Class.forName("oracle.jdbc.driver.OracleDriver");
          jta.append("Database Driver loaded\n");

          // Establish a connection
          Connection connection = DriverManager.getConnection
         ("jdbc:mysql://localhost/javabook", "chris", "password");
     //   ("jdbc:oracle:thin:@liang.armstrong.edu:1521:ora9i",
     //   "", "password");
          jta.append("Database connected\n");

          // Create a statement
          stmt = connection.createStatement();

         
        

        }
        catch (Exception ex) {
          ex.printStackTrace();
        }
      }
    private Statement stmt;
    private JTextArea jta;

}

And Here is the client code:

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.net.Socket;
import javax.swing.*;

public class FinalClient extends JFrame
    implements ActionListener
{

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

    public FinalClient()
    {
        jta = new JTextArea();
        Panel p1 = new Panel();
        p1.add(new Label("Enter radius"));
        p1.add(jtf = new JTextField(10));
        setTitle("Exercise30_2Client");
        setSize(500, 300);
        getContentPane().setLayout(new BorderLayout());
        getContentPane().add(p1, "North");
        getContentPane().add(new JScrollPane(jta), "Center");
        jtf.addActionListener(this);
        setDefaultCloseOperation(3);
        setLocationRelativeTo(null);
        setVisible(true);
        
        
    }

    public void actionPerformed(ActionEvent e)
    {
        String actionCommand = e.getActionCommand();
        if(e.getSource() instanceof JTextField)
            try
          {
            	Socket connectToServer = new Socket("localhost", 8000);
            	isFromServer = new BufferedReader(new InputStreamReader(connectToServer.getInputStream()));
                osToServer = new PrintWriter(connectToServer.getOutputStream(), true);
                String request = jtf.getText().trim();
                osToServer.println(request);
                do{
               
                	for (int i = 1; i <= 4 ; i++) {
                	
                 jta.append((new StringBuilder(isFromServer.readLine())) + "     ");
                
                      }
                	
                jta.append("\n");
                
                }while(true);
                
            }
            catch(IOException ex)
            {
                System.err.println(ex);
            }
    }

    private JTextField jtf;
    private JTextArea jta;
    PrintWriter osToServer;
    BufferedReader isFromServer;
}

Any code that is commented out is just some stuff i was tinkering with.
Any help would be greatly appreciated. Thank you in advance.

I one of the things I tried was making a public static global variable in the server class and calling it as the number of iterations in the for loop of the client class, that didn't work. Any help would be appreciated.

I figured out the problem. I made a columnToClient and stringToClient strings and instead of sending data to the client inside the server for loops, i just added the response string to these ToClientStrings and sent them to the client out side the for loops and got rid of the client for loop. i can post the fixed code if anyone wants to see it.

yes please it would be much appreciated

i have an exibition coming up in two weeks and i was hoping to show off a java created server client program

however of all the source codes i tried over the internet non of them work
i would really love some help getting a program created

If you read, i got this program to work. Have you done any coding yourself, or are you wanting someone to do all the work for you?

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.