简体   繁体   中英

SQL INSERT INTO does not work properly (in Java)

I have sql code in java class. The code is just like this below.

private void SummTEkspor(){
        try {
        bln = (String) cmbBln.getSelectedItem();
        thn = (String) cmbThn.getSelectedItem();
        String sql1 ="DELETE FROM a.dbo.t_export";
        String sql2 ="INSERT INTO a.dbo.t_export\n" +
                     "SELECT * FROM b.dbo.export b WHERE b.sk_batch IN \n" +
                     "(SELECT sk_batch from batch_hdr WHERE bln_proses="+bln+
                     "AND thn_proses="+thn; 


            Statement st = kon.conn.createStatement();
            int rs  = st.executeUpdate(sql1);
            int rsl = st.executeUpdate(sql2);


        } catch (Exception x) {
            System.out.println("FAILED");;
        }
    }

when i run the sql1, it works, but when sql2, it did not work properly, and just display FAILED . I guess the query in sql2 didn't take any value from what selected combo box. How can i solve that? Thanks for any reply

You open a bracket in (SELECT sk_batch f and never close it.
Use System.out.println(sql2); in order to see how the second query looks like, it could also be that one of the parameters thn and bln are null for example.

The problem is because you query is not proper :

INSERT INTO a.dbo.t_export\n" +
 "SELECT * FROM b.dbo.export b WHERE b.sk_batch IN \n" +
  "(SELECT sk_batch from batch_hdr WHERE bln_proses="+bln+
    "AND thn_proses="+thn; 

When you are creating a second select sub-query, you have not closed the ) bracket.

Try this :

INSERT INTO a.dbo.t_export" +
 "SELECT * FROM b.dbo.export b WHERE b.sk_batch IN " +
  "(SELECT sk_batch from batch_hdr WHERE bln_proses='"+bln+
    "' AND thn_proses='"+thn + "')"; 

Fix the Query, quote the string values and put proper spaces.

String bln="testing";
    String thn="abc";
     String sql2 ="INSERT INTO a.dbo.t_export\n" +
                 "SELECT * FROM b.dbo.export b WHERE b.sk_batch IN \n" +
                 "(SELECT sk_batch from batch_hdr WHERE bln_proses='"+bln+
                 "' AND thn_proses='"+thn+"')"; 

Try this query

 String sql2 ="INSERT INTO a.dbo.t_export" +
 "SELECT * FROM b.dbo.export b WHERE b.sk_batch IN " +
 "(SELECT sk_batch from batch_hdr WHERE bln_proses="+bln+
 "AND thn_proses="+thn+ ")"; 

bln_proses & thn_proses these are from same table batch_hdr ???

可能有某些原因:-在第二个select语句中关闭-如果bln_proses或thn_proses是字符串,则必须使用'字符来包含值

First of all you have put \\n in your queries which is unnecessary, then Qualifying the table a.dbo.t_export is not needed.

Instead of:

String sql2 ="INSERT INTO a.dbo.t_export\n" +
                 "SELECT * FROM b.dbo.export b WHERE b.sk_batch IN \n" +
                 "(SELECT sk_batch from batch_hdr WHERE bln_proses="+bln+
                 "AND thn_proses="+thn; 

Try:

String sql2 ="INSERT INTO dbo.t_export " +
                 "SELECT * FROM b.dbo.export b WHERE b.sk_batch IN " +
                 "(SELECT sk_batch from batch_hdr WHERE bln_proses="+bln+
                 " AND thn_proses="+thn +")";

If your columns are of Varchar type then you have to put values inside '' (Single Quotes).

Above query will work. But I suggest you to not use this approach because there is a chance of SQL Injection. Use Precompiled statements to avoid SQL Injection.

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