2015-03-25 34 views
1

我試圖插入新行的B/W現有行,但得到錯誤 我的DB如何在mysql中插入新行b/w現有行?

RoutePoint  lat    lon     type 
    1  12.9061170343075  77.5724397120948  s 
    2  12.9209711117095  77.5601544553814  v 
    3  12.892986142953  77.4662127668475  v 
    4  12.9215410271329  77.5607713634547  s 

查詢 -

String query1 = "UPDATE mytable SET RoutePoint = 
    RoutePoint + 1 where id > 3 "+"insert into rp(lat, lon,type) values (?,?,?)"; 

pstmt = con.prepareStatement(query1); // create a statement 
pstmt.setDouble(1, 12.34234234); 
pstmt.setDouble(2, 77.423423423); // set input parameter 2 
pstmt.setString(3, s); // set input parameter 3 
pstmt.executeUpdate(); // execute insert statement 

但是我卻越來越無差錯

You have an error in your SQL syntax; check the manual that corresponds 
to your MySQL server version for the right syntax to use near 
insert into rp(lat, lon,type) values (12.921343123349597,77.5601659236431,'s') 
at line 1 

請幫忙。

+0

並不清楚到底是什麼你想幹什麼?如果我查詢他們saprately得到這個錯誤 - – 2015-03-25 07:57:43

回答

0

query1你有一個UPDATE和一個INSERT,這些是兩個語句。你應該用一些分隔符將它們分開,或者一個接一個地執行它們(使用兩個PreparedStatement s)。

+0

。 – 2015-03-25 08:12:16

+0

參數索引超出範圍(1>參數的數量,它是0),什麼是邏輯更新mytable SET RoutePoint = RoutePoint + 1其中id> 3' – 2015-03-25 08:12:45

+0

如果語句(您的UPDATE)沒有參數,則在使用它時不設置任何參數。 – 2015-03-25 08:18:16

0

爲什麼您同時使用UpdateInsert命令?分裂您的命令兩大塊:

更新:

String query1 = "UPDATE mytable SET RoutePoint = 
    RoutePoint + 1 where id > 3; 

pstmt = con.prepareStatement(query1); // create a statement 
pstmt.executeUpdate(); // execute update statement 

插入:

query1 = "insert into rp(lat, lon,type) values (?,?,?)" 
pstmt = con.prepareStatement(query1); // create a statement 
pstmt.setDouble(1, 12.34234234); 
pstmt.setDouble(2, 77.423423423); // set input parameter 2 
pstmt.setString(3, s); // set input parameter 3 
pstmt.executeUpdate(); // execute update statement 
相關問題