0

I am using MS-Access and Java with connection of JDBC-ODBC driver. As the code below, I'm trying to create a registration textbox but when I add the values, I only get the result "null" in the database. How do I get the real value I'm inserting ? Thanks

    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

    Connection con = DriverManager.getConnection("jdbc:odbc:ADB");

    Statement statement = con.createStatement();

    statement.executeUpdate("insert into Login " + "values  ('"+uname+"','"+pwd+"')"); 

    uname = userTextBox.getText();
    pwd = passTextBox.getText();
Baba Dass
  • 1
  • 1
  • 1
  • 2

7 Answers7

3

From the looks of it, your statement code is fine. The problem is that you're initializing uname and pwd AFTER you execute it.

I'm assuming that somewhere above you have initialized these variables to null. So, at the time the statement is executed, the values it has to insert are null.

drew moore
  • 31,565
  • 17
  • 75
  • 112
1

With the amount of information you provided, If you show your complete code, I can update my answer accordingly, I assume you need to change this:

statement.executeUpdate("insert into Login " + "values  ('"+uname+"','"+pwd+"')"); 

uname = userTextBox.getText();
pwd = passTextBox.getText();

To:

uname = userTextBox.getText();
pwd = passTextBox.getText();

statement.executeUpdate("insert into Login " + "values  ('"+uname+"','"+pwd+"'")"); 

Also your query is prone to SQL Injection attacks. Always use parameterized queries similar to below:

insert into Login values (?,?)
Pradeep Simha
  • 17,683
  • 18
  • 56
  • 107
0

You need initialize uname and pwd before you excecute the update:

uname = userTextBox.getText();
pwd = passTextBox.getText();
statement.executeUpdate("insert into Login " + "values  ('"+uname+"','"+pwd+"')"); 
0x6C38
  • 6,796
  • 4
  • 35
  • 47
0

You need to switch the line orders from

statement.executeUpdate("insert into Login " + "values  ('"+uname+"','"+pwd+"')"); 
uname = userTextBox.getText();
pwd = passTextBox.getText();

to

uname = userTextBox.getText();
pwd = passTextBox.getText();
statement.executeUpdate("insert into Login " + "values  ('"+uname+"','"+pwd+"')"); 

otherwise uname and pwd will be null (provided they are not given any values pre this code)

patrick.elmquist
  • 2,113
  • 2
  • 21
  • 35
0

Update queries don't return the inserted or modified value. What you can do, is instead of letting the DB automatically generate the ID of the new entry, generate it yourself before inserting, then query the DB using that ID.

You should check this stack overflow post that covers this subject: How to get the insert ID in JDBC?

Community
  • 1
  • 1
tbkn23
  • 5,205
  • 8
  • 26
  • 46
0

You Just Need to Fetch the Values First & then Put the Query :)

String uname = userTextBox.getText();
String pwd = passTextBox.getText();

try          
    {     

    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");    

    Connection con = DriverManager.getConnection("jdbc:odbc:ADB");    

    Statement statement = con.createStatement();     

    statement.executeUpdate("insert into Login " + "values  ('"+uname+"','"+pwd+"')");     
     }
   catch(Exception e)        
    {      
       System.out.println(e);      
    }    
user2119554
  • 320
  • 1
  • 5
  • 13
0
//before using the variables need to initialize 

uname = userTextBox.getText();
pwd = passTextBox.getText();

statement.executeUpdate("insert into Login " + "values  ('"+uname+"','"+pwd+"')"); 
Ahsan
  • 29
  • 1