Results 1 to 3 of 3
  1. #1
    mitty is offline Member
    Join Date
    Jan 2010
    Posts
    16
    Rep Power
    0

    Default Output from a Database

    I have a program which takes user input, searches keywords, if the keywords are found a specific SQL query is sent to the database and the results are output.

    I'm having problems with the output because there are obviously a few tables in the database each with different numbers of fields in there.

    The current program I have outputs the first 4 fields from the table. If there are less that 4 fields then an error occurs and I can't retrieve more than 4 for those tables with more than 4 fields. (I hope this makes sense)

    I know why this is happening, because I have a rs.getString 4 times, I was wondering if there was a way to have a sort of loop which keeps looking and outputtng to the screen until there are no more fields in the table to display.

    My current code is below.

    Java Code:
    import java.io.*;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.*;
    import javax.swing.JOptionPane;
    
    
    
    public class SQL  {
    	
    	
    	public static void main(String[] args)throws IOException{
    
    		
    	String text =  JOptionPane.showInputDialog("Please enter your sentence here");
        String text2= null;
        String text3 = "";
        StringTokenizer st;
        String[] keyword = {"employees", "managers"};   
        String[] keyword2 = {"employees", "admins"};
        String[] keyword3 = {"employees", "analysts"};
        String[] keyword4 = {"employees", "programmers"};
        String firstString;
        String secondString;
        String thirdString;
        String fourthString;
        String Url = "jdbc:mysql://localhost:3306/NLP?user=root&password=m1t35h";
        String dbClass = "com.mysql.jdbc.Driver";
        String query = null;
        int key = 0;
    	st = new StringTokenizer (text, " ");
    	int count;
    
    		
    		while (st.hasMoreElements()){
    	    	  text2 = (st.nextToken());
    	    	  System.out.println (text2);
    	    	 
    	    	  text3 = text3 + " " + text2;
    	    	 }
    		String[] words = text3.split (" ");
    	
    	
    	for (int i=0; i < words.length; i++){
    				
    	  System.out.println (words[i]);
    		  
    	  
    	  
    	count = words.length;  
    	
    
    	
    	  while (i < count){
    		  String Word = words[i];
    		  if (Word.equals ("managers")){
    			  key ++;
    		     }
    		  	else if (Word.equals ("employees")){
    			  key ++;
    		            }
    		       i++;
    		  }
    	   if (key == 2){
    		   System.out.println("SELECT * FROM employees WHERE Position = 'Manager'");
    		   query ="SELECT * FROM employees WHERE Position = 'Manager'";
    		   key = 0;
    		   i = 0;
    	       }
    	   else {
    			key= 0; 
    			i = 0;
    	        }
    	   
    	   while (i < count){
    			  String Word = words[i];
    			  if (Word.equals ("programmers")){
    				  key ++;
    			     }
    			  	else if (Word.equals ("employees")){
    				  key ++;
    			            }
    			       i++;
    			  }
    		   if (key == 2){
    			   System.out.println("SELECT * FROM employees WHERE Position = 'programmer'");
    			   query ="SELECT * FROM employees WHERE Position = 'programmer'";
    			   key = 0;
    			   i = 0;
    		       }
    		   else {
    				key= 0; 
    				i = 0;
    		        }
    	    
    	   
    	   while (i < count){
    			  String Word = words[i];
    		  if (Word.equals ("admins")){
    				  key ++;
    			     }
    		  else if (Word.equals ("employees")){
    				  key ++;
    			  }
    			       i++;
    			  }
    	  if  (key == 2){
    	       System.out.println("SELECT * FROM employees WHERE Position = 'admin'");
    	       query = "SELECT * FROM employees WHERE Position = 'admin'";
    		   key = 0; 
    		   i=0;
    	   }
    	   else {
    			key= 0;    
    			i=0;
    	   }
    	  
    	  while (i < count){
    		  String Word = words[i];
    	  if (Word.equals ("analysts")){
    			  key ++;
    			 		     }
    	  else if (Word.equals ("employees")){
    			  key ++;
    			 		  }
    		       i++;
    		  }
    	  if  (key == 2){
    	       System.out.println("SELECT * FROM employees WHERE Position = 'analyst'");
    	       query = "SELECT * FROM employees WHERE Position = 'analyst'";
    		 
    		   key = 0;
    		  }
    	   else {
    			
    	   }
    
    
    	}
    	
    
    	
    	if (key > 0){
    	System.out.println();
    	try {
    
    		Class.forName("com.mysql.jdbc.Driver");
    		Connection con = DriverManager.getConnection (Url);
    		Statement stmt = con.createStatement();
    		ResultSet rs = stmt.executeQuery(query);
    
    		while (rs.next()) {
    		class ConsoleOutput {
    			    
    			    public void main(String[] args) {
    			        
    			    }
    			}
    
    			firstString = rs.getString(1);
    			secondString = rs.getString(2);
    			thirdString = rs.getString(3);
    			fourthString = rs.getString(4);
    			System.out.println(firstString + " " + secondString + " " + thirdString + " " + fourthString);
    			} //end while
    
    
    
    		con.close();
    		} //end try
    
    		catch(ClassNotFoundException e) {
    		e.printStackTrace();
    		}
    
    		catch(SQLException e) {
    		e.printStackTrace();
    		}
    	}
    	 else {
    		   System.err.println("Not Found");
    			
    	   }
    
    	}
    
    }

    Thank you in advance

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

Similar Threads

  1. Need help: output is not what i want
    By Joshsmith in forum New To Java
    Replies: 8
    Last Post: 09-28-2009, 10:09 AM
  2. Replies: 4
    Last Post: 03-12-2009, 11:25 AM
  3. Java, output string, getting correct output? HELP!
    By computerboyo in forum New To Java
    Replies: 2
    Last Post: 02-25-2009, 11:44 PM
  4. Replies: 2
    Last Post: 02-11-2009, 04:43 AM
  5. Output
    By Twiggy in forum New To Java
    Replies: 14
    Last Post: 12-31-2008, 10:03 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •