简体   繁体   English

使用jdbc将值插入数据库

[英]Insert value into database using jdbc

I am using MS-Access and Java with connection of JDBC-ODBC driver. 我正在将MS-Access和Java与JDBC-ODBC驱动程序连接使用。 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. 如下面的代码所示,我试图创建一个注册文本框,但是当我添加值时,我在数据库中只得到结果“ null”。 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. 问题是执行后要初始化unamepwd

I'm assuming that somewhere above you have initialized these variables to null . 我假设您在上面的某个地方已将这些变量初始化为null So, at the time the statement is executed, the values it has to insert are null . 因此,在执行该语句时,它必须插入的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. 此外,您的查询还容易受到SQL Injection攻击。 Always use parameterized queries similar to below: 始终使用类似于以下内容的参数化查询:

insert into Login values (?,?)

You need initialize uname and pwd before you excecute the update: 在执行更新之前,需要初始化unamepwd

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) 否则, unamepwd将为null(前提是在此代码之前未给它们提供任何值)

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. 您可以做的是,不是让数据库自动生成新条目的ID,而是在插入之前自己生成它,然后使用该ID查询数据库。

You should check this stack overflow post that covers this subject: How to get the insert ID in JDBC? 您应该检查涉及此主题的堆栈溢出文章: 如何在JDBC中获取插入ID?

You Just Need to Fetch the Values First & then Put the Query :) 您只需要先获取值,然后查询即可:)

String uname = userTextBox.getText(); 字符串uname = userTextBox.getText();
String pwd = passTextBox.getText(); 字符串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+"')"); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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