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 14 of 14

Thread: Simple Error ( But Hard Solution)

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

    Default Simple Error ( But Hard Solution)

    Code has been Solved Just Dont know how to delete .
    Last edited by tommyacton; August 5th, 2013 at 03:06 PM. Reason: University will think i copied from the net


  2. #2
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: Simple Error ( But Hard Solution)

    Use PreparedStatements! They will deal with the syntax and prevent sql injection.

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

    Default Re: Simple Error ( But Hard Solution)

    I am not very good with writing code so. Can you please explain what you mean or give me an example plz

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Simple Error ( But Hard Solution)


  5. #5
    Member
    Join Date
    Jul 2013
    Posts
    219
    Thanks
    0
    Thanked 18 Times in 17 Posts

    Default Re: Simple Error ( But Hard Solution)

    Hello.
    Actually you have mistake in your insert query.
    You are not putting corresponding ending quote for name and other fields.
    Just try this.
    Should work.

    String sql = "INSERT INTO person (code, name , income , gender) values ("
    + textField.getText() +
    ",'" + textField_1.getText() +
    "','" + textField_2.getText() +
    "','" + textField_3.getText() + "')";


    Syed.

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

    Default Re: Simple Error ( But Hard Solution)

    Quote Originally Posted by syedbhai View Post
    Hello.
    Actually you have mistake in your insert query.
    You are not putting corresponding ending quote for name and other fields.
    Just try this.
    Should work.

    String sql = "INSERT INTO person (code, name , income , gender) values ("
    + textField.getText() +
    ",'" + textField_1.getText() +
    "','" + textField_2.getText() +
    "','" + textField_3.getText() + "')";


    Syed.
    Thank you very very much Syed. You have saved my life. Thanks a lot.
    Any time on becoming a great programmer. ?
    Thnaks again legend.

  7. #7
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Simple Error ( But Hard Solution)

    Wow

    Someone gave you the advice that the correct way to do this was to use a PreparedStatement. But whoa is me learning something new is waaaaaaaaaaay to hard. Much better to whine and wait until someone provides an incorrect solution.
    Improving the world one idiot at a time!

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

    Default Re: Simple Error ( But Hard Solution)

    The text field is only accepting numbers.. wft

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

    Default GUI text field is only accepting numbers. :/

    Hey guys i have been trying to do this project for a couple of days now, i am close to completing it, but the textfield only seem to accept numbers rather than any text.
    I am using eclipse .

    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 {
     
    	String MSAccessDriver = "sun.jdbc.odbc.JdbcOdbc ".trim();
    	String MyDatabase = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=c:\\db\\Database.mdb";
     
    	Connection con = null;
    	Statement stat = null;
    	ResultSet rs;
     
    	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);
    	}
     
    }

  10. #10
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: GUI text field is only accepting numbers. :/

    Have you posted the right code? The main() method is starting CopyOfStartForm, but you've posted CopyOfStartForm2. Further, I don't see any restrictions on the characters that can be entered in the JTextFields you've posted.

    Why are you using a null layout? Even if that is the best design, you'd be better off assigning a variable to frame's getContentPane() so that you don't have to 'fetch' it every time something is added to it.

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

    Default Re: GUI text field is only accepting numbers. :/

    yea i have posted the right code.. I am not such a good programmer. I would like someone to really help me with this. My project is due in soon

  12. #12
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: GUI text field is only accepting numbers. :/

    You may think you've posted the right code, but the fact remains: The class you posted is not the class being run by the main() method. Helping you with code other than that being run is pointless, and we can't help you with code we can't see. Post the class CopyOfStartForm, or if you changed the name by one character to protect the innocent, then let us know.

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

    Default Re: GUI text field is only accepting numbers. :/

    I have posted the right code

  14. #14
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Simple Error ( But Hard Solution)

    Quote Originally Posted by tommyacton View Post
    wft
    Please provide a different question if you still have a problem.

    --- Update ---

    Please do not double post your questions.
    Threads merged

Similar Threads

  1. [SOLVED] Can't find solution for error in my code..
    By adi2609 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 26th, 2013, 11:10 AM
  2. Better solution for moving my rectangle? (simple game)
    By JohnZ in forum Java Theory & Questions
    Replies: 5
    Last Post: May 24th, 2012, 09:54 AM
  3. Simple code apparently too hard for me...
    By Avopeb in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 29th, 2011, 02:35 PM
  4. Solution for error message: Bad operand for binary operator '-'
    By MostinCredible21 in forum AWT / Java Swing
    Replies: 8
    Last Post: September 7th, 2011, 04:28 PM
  5. I am having a hard time fixing this error
    By KuruptingYou in forum What's Wrong With My Code?
    Replies: 7
    Last Post: August 28th, 2011, 10:12 PM