Results 1 to 3 of 3
  1. #1
    kiki2009 is offline Member
    Join Date
    Jun 2009
    Posts
    43
    Rep Power
    0

    Question how to select item from Jcombo box and insert it into database access

    kindly help me how to select and item from Jcombo box and insert it into a database access,
    i have try with this code below it show incompatible type and could not find symbol when i
    compile the code below :

    Java Code:
     import java.awt.*;
     import java.awt.event.*;
     import java.awt.Toolkit.*;
     import javax.swing.*;
     import javax.swing.JFrame;
     import javax.swing.border.*;
    
     import java.sql.*;
     
     import javax.swing.table.DefaultTableModel;
     import java.util.Vector;
    
    class StartForm extends JFrame implements ActionListener{
    
    	Connection con;
    	PreparedStatement stat;	
    	
    	private Container cpane;
    	
    	private JPanel jp;
    	JTabbedPane jtp;
    	
    	private JLabel lblserialno;
    	private JLabel lblyear;
    	private JLabel lblmonth;
    	private JLabel lblamount;
    	
    	
    	private JTextField txtserialno;
    	//private JTextField txtyear;
    	private JComboBox comboyear;
    	private JTextField txtmonth;
    	private JTextField txtamount;
    	
    		
    
    	private JButton addbtn;
    	private JButton refreshbtn;
    	
    	private JTable table;
    	private JScrollPane scroll;
    	private DefaultTableModel tmodel;
    	private ResultSet result;
    	
        StartForm() {
        	
        	try 
        	{
        	Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
     	 	 con = DriverManager.getConnection ("jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=masterdata.mdb;DriverID=22}","","");
        	}
        	
        	catch(Exception e)
    		{
    		 System.out.println("there was some error in establishing connection : "+e);
    		}
    		
    		Dimension screen = 	Toolkit.getDefaultToolkit().getScreenSize(); 
            
            			setTitle("Automatation System");  
            			setSize(900,700);
            			setLocationByPlatform(true); 
            			setIconImage(new ImageIcon("logo/logo.jpg").getImage());        
            			setLocation((screen.width - 900)/2,((screen.height-700)/2));
            			//setResizable(false);
            			setVisible(true);
            
    				cpane = getContentPane();
    		
    				jtp = new JTabbedPane();
    				jtp.setFont(new Font("Lucida Grande", Font.BOLD, 11));
    				
    				jp = new JPanel();
            		jp.setBorder(BorderFactory.createTitledBorder(""));
    				jp.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
    				jp.setLayout(null);
    				
    				cpane.add(jtp);
    				jtp.addTab("Test1", jp);
    				
    				lblserialno = new JLabel("Serial No:"); 
            		lblserialno.setBounds(30, 10, 100, 50); 
            		lblserialno.setFont(new Font("Monotype Corsiva", Font.BOLD, 16));      
            		jp.add(lblserialno);
            		
            		lblyear = new JLabel("Year:"); 
            		lblyear.setBounds(30, 42, 110, 50); 
            		lblyear.setFont(new Font("Monotype Corsiva", Font.BOLD, 16));      
            		jp.add(lblyear);  
                    	
            		lblmonth = new JLabel("Month :"); 
            		lblmonth.setBounds(30, 70, 110, 50); 
            		lblmonth.setFont(new Font("Monotype Corsiva", Font.BOLD, 16));      
            		jp.add(lblmonth); 
            	
            		lblamount = new JLabel("Amount :"); 
            		lblamount.setBounds(30, 100, 110, 50); 
            		lblamount.setFont(new Font("Monotype Corsiva", Font.BOLD, 16));      
            		jp.add(lblamount);
            	
    				txtserialno = new JTextField(15);
            		txtserialno.setBounds(160, 22, 180, 25);
            		txtserialno.setFont(new Font("Times Roman",Font.PLAIN,12));
            		jp.add(txtserialno);
            		txtserialno.addKeyListener(new KeyAdapter() 
            							{
            								public void keyTyped(KeyEvent e) 
            								{
              									char c = e.getKeyChar();
              										if (!((c >= '0') && (c <= '9') ||
                 											(c == KeyEvent.VK_BACK_SPACE) ||
                 											(c == KeyEvent.VK_DELETE))) {
                													getToolkit().beep();
                													e.consume(); }
            								}
          								});        	
            
           			/*txtyear= new JTextField(15);
            		txtyear.setBounds(160, 51, 180, 25);
            		txtyear.setFont(new Font("Times Roman",Font.PLAIN,12));
            		jp.add(txtyear);*/
            		
            		String[] yearStrings = { "2000", "2001", "2002", "2003", "2004","2005", "2006", "2007", "2008", "2009","2010","2011" };
            		
            		comboyear = new JComboBox(yearStrings);
            		comboyear.setSelectedIndex(0);
            		comboyear.setBounds(160, 51, 180, 25);
            		comboyear.setFont(new Font("Times Roman",Font.PLAIN,12));
            		jp.add(comboyear);
            		comboyear.addActionListener(this);
                   
            		txtmonth= new JTextField(15);
            		txtmonth.setBounds(160, 80, 180, 25);
            		txtmonth.setFont(new Font("Times Roman",Font.PLAIN,12));
            		jp.add(txtmonth);
            		
            		txtamount= new JTextField(15);
            		txtamount.setBounds(160, 110, 180, 25);
            		txtamount.setFont(new Font("Times Roman",Font.PLAIN,12));
            		jp.add(txtamount); 
            			
            		addbtn = new JButton("ADD");	   		
    	   			addbtn.setBounds(100, 400, 100, 25);
    	   			addbtn.setFont(new Font("Monotype Corsiva", Font.PLAIN, 16));	   				   			
    	   			jp.add(addbtn);
    	   			
    	   			addbtn.addActionListener(this);	   		 		
    			
    	   //Table display
    	   	
    	   	String s1=" ",s2="",s3=" ",s4="";
    		String data[][]= new String[100][4];
    		String col[] = {"sl.no","year","month","amount"};
    				
    		ResultSet result=null;		
        						try 
    		    					{	
    		      						stat = con.prepareStatement("Select * from collection");
    		      								result = stat.executeQuery();  							  
    														int r=0;
    														int c=0;
    										
    										while(result.next())
    			  									{
      													s1=result.getString(1);    
    													s2=result.getString(2);
    													s3=result.getString(3);
    													s4=result.getString(4);    
    																					
    																
    			    											data[r][c++]=s1;
    			    											data[r][c++]=s2;
    			    											data[r][c++]=s3;
    			    											data[r][c++]=s4;			    													    										 											
    			    													    													    
    														r++;
    														c=0;
    	
    			   									}
    			   	result.close();
    			   	stat.close();
    			  					}
    							catch(Exception e) {
    												System.out.println("Could not execute the query"+e);
    												}
    				
       DefaultTableModel tablemodel = new DefaultTableModel();
    		for(int i =0;i<col.length;i++)
    		{	
    			tablemodel.addColumn(col[i]);	
    		}
    			
    		JTable table = new JTable(tablemodel);
    			table.setRowHeight(15);
    			table.setShowGrid(true);
    			table.setEnabled(false);
    		    table.setSelectionForeground(Color.RED);
       		    table.setSelectionBackground(Color.BLUE);   		    
       		       		       		    
    			scroll = new JScrollPane(table);
    			scroll.createHorizontalScrollBar();
    			scroll.setBounds(400,10,450,450);
    		 	jp.add(scroll);
    		
    		int i=0;
    		
    		while(i<data.length)
    		{	
    			Vector data1 = new Vector();
    			int j=0;
    			while(j<col.length)
    			{
    			data1.addElement(data[i][j]);
    			j++;
    			}
    			tablemodel.addRow(data1);
    			i++;
    			
    		}
        }
      
    	public void actionPerformed(ActionEvent e)
    		{		
    			if(e.getSource()==addbtn)			
    	
    			{
    				AccessUser();
    			}	
       		}
      		
     	public void AccessUser()
    		{
           	try
    			{
        				stat = con.prepareStatement("INSERT INTO collection VALUES(?,?,?)");
    			  								
    			  									String	slno  			= txtserialno.getText();    
    												//String	yearcollect    	= txtyear.getText();
    												String yearcollect		=comboyear.getSelectedItem();
    												String	monthcollect    = txtmonth.getText();
    												String	amountcollect   = txtamount.getText();
    												
    												
    													stat.setString(1, slno);
    													//stat.setString(2, yearcollect );
    													stat.setString(2, yearcollect);
    													stat.setString(3, monthcollect);													
    													stat.setString(4, amountcollect);
    																
    													
    													
    											stat.executeUpdate();
    												
    													txtserialno.setText("");
    													//txtyear.setText("");
    													comboyear.setText("");   										
    			   										txtmonth.setText("");
    			   										txtamount.setText("");
    			   										
    												}
    			   								catch(Exception exp)
    			   									{
    												System.out.println("Could not execute the query"+ exp);
    												} 
    													
    			Thread th = new Thread();
    			
    		try{
      for(int i = 1;i <= 10;i++)
        {
      String s1=" ",s2="",s3=" ",s4="";
    		String data[][]= new String[100][4];
    		String col[] = {"sl.no","year","month","amount"};
    				
    		ResultSet result=null;		
        						try 
    		    					{	
    		      						stat = con.prepareStatement("Select * from collection");
    		      								result = stat.executeQuery();  							  
    														int r=0;
    														int c=0;
    										
    										while(result.next())
    			  									{
      													s1=result.getString(1);    
    													s2=result.getString(2);
    													s3=result.getString(3);
    													s4=result.getString(4);    
    																					
    																
    			    											data[r][c++]=s1;
    			    											data[r][c++]=s2;
    			    											data[r][c++]=s3;
    			    											data[r][c++]=s4;			    													    										 											
    			    													    													    
    														r++;
    														c=0;
    	
    			   									}
    			   	result.close();
    			   	stat.close();
    			  					}
    							catch(Exception e) {
    												System.out.println("Could not execute the query"+e);
    												}
    				
       DefaultTableModel tablemodel = new DefaultTableModel();
    		for(int a =0;a<col.length;a++)
    		{	
    			tablemodel.addColumn(col[a]);	
    		}
    			
    		JTable table = new JTable(tablemodel);
    			table.setRowHeight(15);
    			table.setShowGrid(true);
    			table.setEnabled(false);
    		    table.setSelectionForeground(Color.RED);
       		    table.setSelectionBackground(Color.BLUE);   		    
       		       		       		    
    			scroll = new JScrollPane(table);
    			scroll.createHorizontalScrollBar();
    			scroll.setBounds(400,10,450,450);
    		 	jp.add(scroll);
    		
    		int a=0;
    		
    		while(a<data.length)
    		{	
    			Vector data1 = new Vector();
    			int b=0;
    			while(b<col.length)
    			{
    			data1.addElement(data[a][b]);
    			b++;
    			}
    			tablemodel.addRow(data1);
    			a++;
    			
    		}
     					 th.sleep(1);
      						}
     					 }
      						catch(InterruptedException e)
      							{
        								System.out.println("Thread interrupted!");
      									e.printStackTrace();
      							}
      					}	   
       
        
      public static void main(String arg[]) 
      {
      	StartForm st = new StartForm();
      }    
    }
    Last edited by JosAH; 09-25-2011 at 11:29 AM. Reason: Changed [quote] tags to [code] tags.

  2. #2
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    618
    Rep Power
    4

    Default Re: how to select item from Jcombo box and insert it into database access

    Hi.
    First you must break down your code on little parts. It are database layer and user interface. Then you can understand where you have problems.
    So I mean it will look like one layer does not know about another layer. You must pass only data. Now you have "spaghetti" code.
    Skype: petrarsentev
    http://TrackStudio.com

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,344
    Blog Entries
    7
    Rep Power
    20

    Default Re: how to select item from Jcombo box and insert it into database access

    I hope you realize that you're dealing with two completely unrelated problems:

    1) get a value from a JComboBox;
    2) how to insert a value into a database table.

    All the rest of your code you've posted is irrelevant and just obfuscates matters. Please trim down your code as much as possible and separate your two (sub)problems.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Auto select JCombo box Adding to JTable
    By kasiram.p@gmail.com in forum AWT / Swing
    Replies: 0
    Last Post: 01-03-2011, 06:08 AM
  2. Replies: 4
    Last Post: 12-16-2010, 03:47 PM
  3. Appending and item to a Select List
    By Samurai Coder in forum New To Java
    Replies: 1
    Last Post: 12-04-2009, 10:56 PM
  4. Replies: 3
    Last Post: 02-28-2009, 09:17 AM
  5. how to fetch 20,000 records from excel sheet & insert into access database using Java
    By santosh_tamse in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 02-22-2009, 10:24 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
  •