Code has been Solved Just Dont know how to delete .
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.
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
Use PreparedStatements! They will deal with the syntax and prevent sql injection.
I am not very good with writing code so. Can you please explain what you mean or give me an example plz
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.
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!
The text field is only accepting numbers.. wft
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); } }
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.
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
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.
I have posted the right code