简体   繁体   English

如何从结果查询中插入查询以在Java NetBeans上插入查询?

[英]how to insert query from the result query to insert query on java netbeans?

I have a problems in java progamming, I want to add primary key from x table to insertion query 我在Java编程中遇到问题,我想将x表的主键添加到插入查询中

String sql = "(select id_transaksi from transaksi where id_transaksi in (SELECT MAX(id_transaksi) FROM transaksi))"

koneksi.stat.executeUpdate("insert into detail_transaksi values(null,'"+sql+"','"+produk.getText()+"','"+tgl+"','"+beli.getText()+"','"+bayar.getText()+"')");

anyone can help me? 有人可以帮助我吗? I want to add the result of query to insertion query? 我想将查询结果添加到插入查询吗? thanks. 谢谢。

thanks for the answer, but I mean, I want to add the first query for the second query, so I put id from table x to insert query to table y, I use it in java programming. 感谢您的回答,但我的意思是,我想为第二个查询添加第一个查询,因此我将表x的id插入到表y中,在Java编程中使用它。

The things which u need to do are: 您需要做的事情是:

Step 1) Select your required value ( primary key) from the database, which can be done as follows 步骤1)从数据库中选择所需的值(主键),可以按照以下步骤进行操作

i) Establish a connection to database i)建立与数据库的连接

eg: Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");             
    Connection con=DriverManager.getConnection("jdbc:odbc:kkk");

ii) Frame a suitable query ii)制定合适的查询

eg: String sql = "select id_transaksi from transaksi where id_transaksi in (SELECT      MAX(id_transaksi) FROM transaksi))"

iii) Prepare a Statement iii)准备一份声明

 eg: PreparedStatement st=con.prepareStatement(sql);

iv) Execute your query and store the result in resultset object iv)执行查询并将结果存储在结果集对象中

eg: ResultSet  rs=st.executeQuery();

v) Retrieve the required value(primary key) from the result set Object to a variable of appropriate datatype. v)从结果集Object中检索所需值(主键)到适当数据类型的变量。

eg: String my_rqd_value=rs.getString(“id_transaksi”);

Step 2) Now u have the value to be inserted in the variable my_rqd_value which can be inserted to any table as you prefer, follow these steps to insert. 步骤2)现在,您已经将值插入变量my_rqd_value中,该变量可以根据需要插入到任何表中,请按照以下步骤进行插入。

i) Frame a suitable query. i)制定合适的查询。

eg: sql = ""insert into detail_transaksi values(null,'"+my_rqd_value+"','"+produk.getText()+"','"+tgl+"','"+beli.getText()+"','"+bayar.getText()+"')");

ii) Prepare a Statement. ii)准备一份声明。

eg: st=con.prepareStatement(sql);

iii) Execute your query. iii)执行查询。

rs=st.executeUpdate();

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

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