简体   繁体   中英

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

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 .

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

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

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)

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?

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);      
    }    
//before using the variables need to initialize 

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM