Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Inputting data on a slq database ( MS access) with java GUI b-) b-)

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    17
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Inputting data on a slq database ( MS access) with java GUI b-) b-)

    When i run the code, that type some text on the fields. It only sees to accept numbers. ()
     
    package sa00366_surrey_ac_uk;
     
     
     
    import java.awt.EventQueue;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JTextField;
     
    /**
     *
     * @author I am CS
     */
    public class CopyOfStartForm {
     
    	private static final String MSAccessDriver = "sun.jdbc.odbc.JdbcOdbc ".trim();
    	public static final String MyDatabase = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=c:\\db\\Database.mdb";
    	private static Connection con = null;
    	private static Statement stat = null;
    	ResultSet rs;
     
    	private JFrame frame;
    	private JTextField textField;
    	private JTextField textField_1;
    	private JTextField textField_2;
    	private JTextField textField_3;
    	public JButton btnInsert;
     
    	/**
    	 * Launch the application.
    	 */
    	public static void main(String[] args) {
    		EventQueue.invokeLater(new Runnable() {
                            @Override
    			public void run() {
    				try {
    					CopyOfStartForm window = new CopyOfStartForm();
    					window.frame.setVisible(true);
    				} catch (Exception e) {
    				}
    			}
    		});
    	}
     
    	/**
    	 * Create the application.
    	 */
    	public CopyOfStartForm() {
    		initialize();
    	}
     
    	/**
    	 * Initialize the contents of the frame.
    	 */
     
    	public void insertperson() throws Exception {
     
    		String sql = "INSERT INTO person (code, name , income , gender) values ("
    						+ textField.getText() +
    				",'"	+ textField_1.getText() +
    				",'"	+ textField_2.getText() +
    				",'"	+ textField_3.getText() + "')";
     
    		System.out.print(sql);
     
    		try {
    			Class.forName(MSAccessDriver);
    			String db = "jdbc:odbc:MyDatabase"; // db = database string stored in the database 
    			con = DriverManager.getConnection(db);
    			stat = con.createStatement();
    			stat.execute(sql);
     
    		}
     
    		catch (Exception ex) {
    			ex.printStackTrace();
    		}
     
    		finally {
    			try {
    				stat.close();
    			} catch (SQLException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    			try {
    				con.close();
    			} catch (SQLException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
     
     
    			textField.setText("");
    			textField_1.setText("");
    			textField_2.setText("");
    			textField_3.setText("");
     
    			JOptionPane.showMessageDialog(null, "Save Complete Successfully");
    		}
     
    	}
     
    	private void initialize() {
    		frame = new JFrame();
    		frame.setBounds(100, 100, 450, 300);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.getContentPane().setLayout(null);
     
    		JLabel lblNewLabel = new JLabel("Person Code");
    		lblNewLabel.setBounds(30, 30, 61, 14);
    		lblNewLabel.setToolTipText("Person Code");
    		frame.getContentPane().add(lblNewLabel);
     
    		JLabel lblFullName = new JLabel("Full Name");
    		lblFullName.setBounds(30, 67, 46, 14);
    		frame.getContentPane().add(lblFullName);
     
    		JLabel lblNewLabel_1 = new JLabel("Income");
    		lblNewLabel_1.setBounds(30, 102, 46, 14);
    		frame.getContentPane().add(lblNewLabel_1);
     
    		JLabel lblGender = new JLabel("Gender");
    		lblGender.setBounds(30, 139, 46, 14);
    		frame.getContentPane().add(lblGender);
     
    		textField = new JTextField();
    		textField.setBounds(122, 27, 124, 20);
    		frame.getContentPane().add(textField);
    		textField.setColumns(10);
     
    		textField_1 = new JTextField();
    		textField_1.setBounds(122, 64, 124, 20);
    		frame.getContentPane().add(textField_1);
    		textField_1.setColumns(10);
     
    		textField_2 = new JTextField();
    		textField_2.setBounds(122, 99, 124, 20);
    		frame.getContentPane().add(textField_2);
    		textField_2.setColumns(10);
     
    		textField_3 = new JTextField();
    		textField_3.setBounds(122, 136, 124, 20);
    		frame.getContentPane().add(textField_3);
    		textField_3.setColumns(10);
     
    		JButton btnInsert = new JButton("Insert");
    		btnInsert.addActionListener(new ActionListener() {
                            @Override
    			public void actionPerformed(ActionEvent arg0) {
     
    				try {
    					insertperson();
    				} catch (Exception e) {
    				}
    			}
    		});
    		btnInsert.setBounds(63, 189, 89, 23);
    		frame.getContentPane().add(btnInsert);
    	}
     
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Inputting data on a slq database ( MS access) with java GUI b-) b-)

    Designated as duplicate of
    http://www.javaprogrammingforums.com...-solution.html
    Please keep your discussion within that thread. I am locking this thread.

Similar Threads

  1. Need help inputting and writing to a external data file.
    By Rain_Maker in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 23rd, 2013, 11:52 PM
  2. Replies: 21
    Last Post: November 27th, 2012, 10:58 PM
  3. multiple access to same data in database
    By the light in forum JDBC & Databases
    Replies: 1
    Last Post: August 23rd, 2011, 10:19 AM