简体   繁体   中英

Executing multiple queries, characters found after end of sql statement

I am trying to execute two queries as given below but its giving me characters found after end of sql statement exception. I intend to add some data in table2 from table1 and some from the user. The database used is ms access database.

try{
     String ad=  ((JTextField)jDateChooser1.getDateEditor().getUiComponent()).getText();
     String dd= ((JTextField)jDateChooser2.getDateEditor().getUiComponent()).getText();
     String rt= jTextField8.getText();
     int rn= Integer.parseInt(jTextField2.getText());
     int rr= Integer.parseInt(jTextField3.getText());
     String sr= jTextField7.getText();
     int nn= Integer.parseInt(jTextField4.getText());
     int na= Integer.parseInt(jTextField5.getText());
     int nc= Integer.parseInt(jTextField6.getText());
     int gc= Integer.parseInt(jTextField1.getText());

     stmt= conn.createStatement();
     String squery="insert into Room(GuestCode, Title, FirstName, LastName, Country, State, City, ContactNo) select GuestCode, Title, FirstName, LastName, Country, State, City, ContactNo from GuestDetails where GuestCode=gc;insert into Room(ArrivalDate, DepartureDate, RoomType, RoomNo, RoomRate, SpecialRequirements, NoOfNights, NoOfAdults, NoOfChildren) values(' "+ad+" ',' "+dd+" ',' "+rt+" ',' "+rn+" ',' "+rr+" ',' "+sr+" ',' "+nn+" ',' "+na+" ',' "+nc+" ');";

     rs=stmt.executeQuery(squery);
 }catch(Exception ex){
     JOptionPane.showMessageDialog(null, ex);
 }

You're not allowed to put more than one statement into stmt.executeQuery, so everything after the ; (and you can leave the ; out too) should be put in a separate query and executed separately.

However on reading your problem description again (and when starting to edit to add prepared statements) I've figured out that what you try to do is retrieve some data from 1 source and the remainder from the variables in your scope.

You can only use 1 insert to insert data. if you use multiple, they will end up as multiple rows. If you insist on using 2 statements, 1 should be an insert, the other an update. (still run 2 separate executeQuery's to run them.)

If you want to do it in 1 statement then you will have to write your query in such a way that it will work like that.

this then looks like a duplicate of: Using INSERT INTO with 'SELECT' to supply some values but not others (Access 2010)

lastly: try using prepared statements to avoid sql injection (and making much tidier code)

PreparedStatement statement = conn.prepareStatement("select abc from xyz where x = ?");
statement.setString(1, "value you want to match x with";
statement.executeQuery();

// this will replace the value of the first ? with "value you want to match x with"

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