java 使用 jdbc 将值插入数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16090006/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 21:53:02  来源:igfitidea点击:

Insert value into database using jdbc

javadatabasems-accessjdbc-odbc

提问by Baba Dass

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

我正在使用 MS-Access 和 Java 连接 JDBC-ODBC 驱动程序。作为下面的代码,我正在尝试创建一个注册文本框,但是当我添加值时,我只在数据库中得到结果“null”。我如何获得我插入的真实价值?谢谢

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

回答by drew moore

From the looks of it, your statement code is fine. The problem is that you're initializing unameand pwdAFTER you execute it.

从它的外观来看,您的语句代码很好。问题是您正在初始化unamepwd在执行之后。

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.

我假设在上面的某个地方您已将这些变量初始化为null. 因此,在执行语句时,它必须插入的值是null.

回答by Pradeep Simha

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:

此外,您的查询容易受到 SQL 注入攻击。始终使用类似于以下的参数化查询:

insert into Login values (?,?)

回答by tbkn23

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.

更新查询不返回插入或修改的值。您可以做的是,不要让 DB 自动生成新条目的 ID,而是在插入之前自己生成它,然后使用该 ID 查询 DB。

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

您应该查看涵盖此主题的堆栈溢出帖子: 如何在 JDBC 中获取插入 ID?

回答by user2119554

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

你只需要先获取值然后输入查询:)

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

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

回答by Ahsan

//before using the variables need to initialize 

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

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

回答by 0x6C38

You need initialize unameand pwdbefore you excecute the update:

你需要初始化 unamepwd你excecute更新之前:

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

回答by patrick.elmquist

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 unameand pwdwill be null (provided they are not given any values pre this code)

否则unamepwd将为空(前提是在此代码之前没有给它们任何值)