-5
Connection conn;
Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
conn = DriverManager.getConnection("jdbc:ucanaccess://C:\\Users\\MUHAMMAD SHAHAB\\STUD1.accdb");
String username = tf.getText();
String password = String.valueOf(pf.getPassword());
PreparedStatement pst;
pst = conn.prepareStatement("INSERT INTO SUN (Username,Password)" + "values(?,?)");

pst.setString(1, username);
pst.setString(2, password);

I just want to enter integer data in database instead of string.How should i replace string with int in this code? Which method should i use for getting integer data from TextField.

Paolo Forgia
  • 6,572
  • 8
  • 46
  • 58
Mcolo
  • 149
  • 2
  • 10
  • 8
    It takes very little effort on your own to find the solution yourself. Go to [the javadoc for PreparedStatement](https://docs.oracle.com/javase/8/docs/api/java/sql/PreparedStatement.html) and find the correct one of the `set` methods. – fabian Aug 17 '16 at 08:20
  • would you please show me the format how do i get integer data from textfield@fabian – Mcolo Aug 17 '16 at 08:26
  • consider asking "which method should i use for getting integer data from textfield" in a separate question, and post the appropriate code for it. – c0der Aug 17 '16 at 08:29
  • 1
    http://stackoverflow.com/questions/5585779/converting-string-to-int-in-java – fabian Aug 17 '16 at 08:31
  • I am trying to do this but i didn't{ int username=tf.getInt(); String password=String.valueOf(pf.getPassword()); PreparedStatement pst; pst=conn.prepareStatement("INSERT INTO SUN (Username,Password)"+"values(?,?)"); pst.setInt(1, username); pst.setString(2,password); } – Mcolo Aug 17 '16 at 08:44
  • getText() from textbox then convert string to integer then insert... – Rupsingh Aug 17 '16 at 08:46
  • can you plz show me how should i convert string to integer if you don't mind because i am new in java plzzz @Maharaj – Mcolo Aug 17 '16 at 08:50
  • @Shahabkhan ``Integer.valueOf(tf.getText());``. If this question is really about that conversion and not about access/preparedstatement, it's quality is even worse than what everyone thought. – f1sh Aug 17 '16 at 08:54
  • Query will be like "insert into table_name values ("+ Integer.parseInt(jTextField.getText()) +")" – Rupsingh Aug 17 '16 at 08:56
  • you are not understanding me. I am just going to post the complete code ,so that you can better understand my problem@f1sh – Mcolo Aug 17 '16 at 09:02

1 Answers1

0

Edit: Added conversion from string to int.

Use:

    //if the data is an integer represented as String 
    String intAsString = "5";
    //convert it. catch NumberFormatException exceptions 
    int intValue = Integer.parseInt(intAsString);
    //update 
    pst.setInt(3, intValue);

See documentation.

c0der
  • 18,467
  • 6
  • 33
  • 65
  • No you are not understanding what i am trying to say....You are just showing me a method for setting int data in a textfield.But how should i get integer data.for example if i want to get string data then i will do this 'String u=tf.getText' but what should i do for getting integer data? – Mcolo Aug 17 '16 at 08:39
  • So you are asking how to convert a ``String`` into an ``Integer``? – f1sh Aug 17 '16 at 08:53
  • http://stackoverflow.com/questions/3517686/cannot-convert-string-to-integer-in-java – Ole V.V. Aug 17 '16 at 09:21