Baba Dass
Baba Dass

Reputation: 1

Insert value into database using jdbc

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();

Upvotes: 0

Views: 15137

Answers (7)

Pradeep Simha
Pradeep Simha

Reputation: 18123

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 (?,?)

Upvotes: 1

Ahsan
Ahsan

Reputation: 29

//before using the variables need to initialize 

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

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

Upvotes: 0

user2119554
user2119554

Reputation: 320

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);      
    }    

Upvotes: 0

Tomer B
Tomer B

Reputation: 5475

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?

Upvotes: 0

patrick.elmquist
patrick.elmquist

Reputation: 2140

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)

Upvotes: 0

0x6C38
0x6C38

Reputation: 7076

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+"')"); 

Upvotes: 0

drew moore
drew moore

Reputation: 32680

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.

Upvotes: 3

Related Questions