Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/394.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在Mysql中获取my表的外键字段中的空值_Java_Mysql - Fatal编程技术网

Java 在Mysql中获取my表的外键字段中的空值

Java 在Mysql中获取my表的外键字段中的空值,java,mysql,Java,Mysql,1.我使用java编写的语句将值插入newuser表,如下所示: I have 2 tables in my application: 1.newuser 2.introducer_table Newuser1 table contains *sid**(primary key,auto-generated field)****, fathername,gender,datebirth,occupation,addharnumber as columns 介绍人表: PreparedSta

1.我使用java编写的语句将值插入newuser表,如下所示:

I have 2 tables in my application:
1.newuser
2.introducer_table

Newuser1 table contains *sid**(primary key,auto-generated field)****,  fathername,gender,datebirth,occupation,addharnumber as columns
介绍人表:

PreparedStatement pst =  con.prepareStatement("insert into newuser1 (name,fathername,gender,datebirth,occupation,aadharNumber) values(?,?,?,?,?,?)");

          //pst.setString(1,NULL);
          pst.setString(1,”xyz”);
          pst.setString(2,"ram");
          pst.setString(3,"male");
          pst.setString(4,"oct25");
          pst.setString(5,"emps");
          pst.setString(6,"4564");
data is inserted successfully,but I want the sid value of newuser1 in my  introducer_table so I write the query like this in prepared statement to select the last   insert  id.

My introducer_table contains the columns: 

**sid(foreign key),name,accountno,sign**
PreparedStatement pst =  con.prepareStatement("insert into    introducer_table(sid,name,accountno,sign) values((SELECT LAST_INSERT_ID() from   dual),?,?,?)");

              //nomine details into data base
              //pst.setString(1,NULL);
              pst.setString(1,"ram");
              pst.setString(2,"8945");
              pst.setString(3,"ssss");

 When I execute this,Im getting ‘0’ value in the sid column of introducer_table.I make   sid column as not null while creating intoducer_table,even im getting the ‘zero’ value like this:

请帮帮我,我被这个问题困扰了这么多天。

与其按你的方式做,不如考虑使用

**Sid**   name   accountno     sign
**0**     ram      8945        ssss
然后在插入之后

con.prepareStatement(query, Statement.RETURN_GENERATED_KEYS);
  

pst.executeUpdate();
ResultSet rs = prest.getGeneratedKeys();

但是我编写了准备好的语句代码,用于在另一个java类中插入Introductor_表。我可以在另一个类中使用这个生成的键吗?它可以工作吗?是的,只需使用
last_insert_id
的值来设置
sid
的值意味着要在我的另一个类中使用此last_insert_id变量,我必须为此类创建obj并可以使用它。是否正确?您需要以某种方式将此值从获取它的位置传递到要使用它的位置。这可能是通过使用类变量,也可能只是通过从方法返回的局部变量实现的。嗨,我在发送生成的id时遇到了困难。请给我示例代码,我必须在另一个类中使用最后一个插入id变量
int last_inserted_id = -1;
if(rs.next())
{
  last_inserted_id = rs.getInt(1);
}