0

Good day,

I'm new and glad to be here, I just need a help with the my code can anyone help me find the main error with it? The JList is giving me hell and since I migrated to a new laptop I'm failing to establish a connection with my database.

Any help will be greatly appreciated

Kind Regards,
Mylo92

import java.sql.*;
 import javax.swing.*;
 import java.awt.*;
 import java.awt.event.*;
 import java.io.File;
 import javax.swing.event.ListSelectionEvent;
 import javax.swing.event.ListSelectionListener;

 public class Exoplanet_List{
 	private static final String DB_file_name = "Exoplanets_2007-2011_Szymon_Welgus";
 	private static final String DRIVER = "jdbc:odbc:DRIVER = {Microsoft Access Driver (*.mdb)};" + "DBQ=" + new File(DB_file_name).getAbsolutePath();

 	static{
 		try{
 			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
 		}
 		catch(ClassNotFoundException e){
 			System.out.println("Class not found");
 			e.printStackTrace();
 		}
 	}

 	private Connection dbcon;

 	/*class ListExample extends JFrame{
    	private	JPanel topPanel;
    	private	JList listbox;

    	public ListExample(){
    		setTitle("Make a choice: ");
    		setSize(300, 100);
    		setBackground(Color.gray);
	    	topPanel = new JPanel();
	    	topPanel.setLayout(new BorderLayout());
	    	getContentPane().add(topPanel);
	    	String	listData[] ={"A - Display all", "Q - Exit"};
	    	listbox = new JList(listData);
     		topPanel.add(listbox, BorderLayout.CENTER);
        }
 	}*/

 	/*public class TextArea {
           JFrame f = new JFrame();
           f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
           JTextArea area = new JTextArea();
           area.setLineWrap(true);
           area.setWrapStyleWord(true);
           area.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8));
           f.add(new JScrollPane(area));
           f.setSize(new Dimension(350, 300));
           f.setLocationRelativeTo(null);
           f.setVisible(true);
    }*/

    /*public class DualSample {
    String labels[] = { "A", "B", "C", "D","E", "F", "G", "H","I", "J" };

    JFrame f = new JFrame("Sample Components");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JList list = new JList(labels);
    JScrollPane scrollPane = new JScrollPane(list);

    Container contentPane = f.getContentPane();
    contentPane.add(comboBox, BorderLayout.NORTH);
    contentPane.add(scrollPane, BorderLayout.CENTER);

    f.setSize(300, 200);
    f.setVisible(true);
  }*/

  public class ListDemo extends JFrame {

	JList list;
	String[] listColorNames = {"black", "blue", "green", "yellow", "white"};
	Color[] listColorValues = {Color.BLACK, Color.BLUE, Color.GREEN, Color.YELLOW, Color.WHITE};
	Container contentpane;
	public ListDemo(){
		super("Your choice: ");
		contentpane = getContentPane();
		contentpane.setLayout(new FlowLayout());
		list = new JList(listColorNames);
		list.setSelectedIndex(0);
		list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		contentpane.add(new JScrollPane(list));
		list.addListSelectionListener(new ListSelectionListener() {
        public void valueChanged(ListSelectionEvent e) {
                   contentpane.setBackground(listColorValues[list.getSelectedIndex()]);
		     	}
	    	});
		setSize(200, 200);
		setVisible(true);
    	}
    }

 	public Exoplanet_List(){
 		System.out.println("Exoplanet_List Linking to Database... \n");

 		try{
 			dbcon = DriverManager.getConnection(DRIVER);
 			Statement smt = dbcon.createStatement();
 			JOptionPane.showMessageDialog(null, "Exoplanet_List database connection successful.", "Status", JOptionPane.INFORMATION_MESSAGE);
 			JOptionPane.showMessageDialog(null, "All data that has the following variables: Unknown; 0; 01 January <current year>; that data was unavilable.", "Please note!", JOptionPane.INFORMATION_MESSAGE);
 			char choice = ' ';

 			do{
 				//Look for method at home JFrame

 				String input = JOptionPane.showInputDialog("Your choice: ");
 				choice = input.toUpperCase().charAt(0);
 				switch(choice){
 					case 'A':{
 						selectAllQry();
 						break;
 					}
 				}
 			}
 			while(choice != 'Q');
 			dbcon.close();
 			JOptionPane.showMessageDialog(null, "Status", "Done.", JOptionPane.INFORMATION_MESSAGE);
 			Thread.sleep(1000);
 			System.exit(0);
 		}
 		catch(Exception e){
 		   JOptionPane.showMessageDialog(null, "Cannot connect.", "Status", JOptionPane.ERROR_MESSAGE);
	       e.printStackTrace();
	       System.out.println(e.toString());
 		}
 		System.exit(0);
 	}

 	public void selectAllQry(){
 		try{
     		String choose_number = JOptionPane.showInputDialog("Please enter a number from 1 - 248: ");
    		Statement smt = dbcon.createStatement();
    		String sql = "...";
    		ResultSet rs = smt.executeQuery(sql);
 	    	System.out.println("\nPlanet number Name         Star          Constellation       Distance from Earth(Light-years) Orbital Period(Days) Minimum Mass(MJ) Discovery Date Detection Method");
     		System.out.println("---------------------------------------------------------------------------------------------------------------------------------------------------------------------");
    		while(rs.next()){
     	    	  String pn = rs.getString("Planet number");
	                  pn += "  ";
	              String n = rs.getString("Name");
    	          for (int i = n.length(); i < 13; i++)
	            	  n += " ";
    	          String star = rs.getString("Star");
	              for (int i = star.length(); i < 12 ; i++)
         	    	  star += " ";
	              String con = rs.getString("Constellation");
    	          for (int i = con.length(); i < 26; i++)
        	    	  con += " ";
	              String dfE = rs.getString("Distance from Earth(Light-years)");
	                  dfE = "R" + dfE.substring(0, dfE.length () - 2);
    	          for (int i = dfE.length(); i < 9; i++)
        	    	  dfE = " " + dfE;
	              String op = rs.getString("Orbital Period(Days)");
	                  op = "R" +  op.substring(0, op.length () - 2);
    	          for (int i = op.length(); i < 9; i++)
     	         	  op =  " " + op;
	              String mm = rs.getString("Minimum Mass(MJ)");
	              for (int i = mm.length(); i < 6; i++)
     	        	  mm = " " + mm;
    	          String dd = rs.getString("Discovery Date");
     	          for (int i = dd.length(); i < 10; i++)
	         	      dd = " " + dd;
	              String dm = rs.getString("Detection Method");
    	              dm = "  " + dm.substring(0, 10);

	          System.out.println (pn + n + star + con + dfE + op + mm + dd + dm);
 		}
 	}
 	catch(Exception e){
 		JOptionPane.showMessageDialog(null, "selectAllQry failed.", "Status", JOptionPane.ERROR_MESSAGE);
 		e.printStackTrace();
 	}
 	System.exit(0);
 }
    public static void main(String[] args){
        	//ListExample mainFrame = new ListExample();
	    	//mainFrame.setVisible(true);
	    	//TextArea ta = new TextArea();
 	    	new Exoplanet_List();
 	    	//JComboBox comboBox = new JComboBox(labels);
            //comboBox.setMaximumRowCount(4);
            //ListDemo test = new ListDemo();
		    //test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
 }
2
Contributors
1
Reply
2
Views
5 Years
Discussion Span
Last Post by peter_budo
0

Well you just failed to post exact error. It is unlikely that someone will copy&paste your application and try to compile it. So why don't you post error if any received or explain in details where is JList giving you hell...

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.