[英]Exception while reading an sql file and doing database insertion in Java
我試圖從一個sql文件中讀取sql查詢,並使用以下java代碼創建mysqlite數據庫:
import java.io.BufferedReader;
import java.io.FileReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.util.ArrayList;
public class ReadMySqlDump {
static String sep = System.getProperty("line.separator");
public ReadMySqlDump(){
try{
String s = "";
String buff="";
ArrayList<String> creates = new ArrayList<String> ();
ArrayList<String> inserts = new ArrayList<String> ();
boolean createStart = false;
String currentCreate = "";
BufferedReader br = new BufferedReader(new FileReader("mydb.sql.sql"));
while((buff=br.readLine()) != null){
buff = buff.trim();
if(buff.length() == 0)continue;
if(buff.startsWith("/*"))continue;
if(buff.startsWith("--"))continue;
if(createStart && buff.startsWith(");")){
// System.out.println("before: " + currentCreate);
currentCreate = currentCreate.trim();
if(currentCreate.endsWith(","))currentCreate = currentCreate.substring(0, currentCreate.length()-1);
//currentCreate = currentCreate.substring(0, currentCreate.length()-2);
// System.out.println("after: " + currentCreate);
currentCreate += " " + buff + sep;
createStart = false;
creates.add(currentCreate);
currentCreate = "";
continue;
}
if(createStart){
currentCreate += " " + buff +sep;
continue;
}
if(!createStart && buff.startsWith("CREATE")){
createStart = true;
currentCreate += buff + sep;
continue;
}
if(buff.startsWith("INSERT")) {
inserts.add(buff);
continue;
}
}
br.close();
for(String ss: creates){
System.out.println(ss);
}
System.out.println("");
System.out.println("");
for(String ss: inserts){
System.out.println(ss);
}
Class.forName("org.sqlite.JDBC");
Connection conn = DriverManager.getConnection("jdbc:sqlite:testNew5.db");
Statement stat = conn.createStatement();
for(String ss: creates){
System.out.println("SQL command: " + ss);
stat.executeUpdate(ss);
}
// ResultSet rs = stat.executeQuery("select * from tbl_passion_attributes");
// while(rs.next()){
// }
for (String ss : inserts){
System.out.println("ss: " + ss);
int n = stat.executeUpdate(ss);
System.out.println("n: " + n);
}
conn.close();
} catch(Exception ex){
ex.printStackTrace();
}
}
public static void main(String[] args) {
new ReadMySqlDump();
}
}
create語句可以完美工作並創建表。 但是當插入語句開始時,我得到了第一個INSERT語句本身的異常。 以下是例外:
ss: INSERT INTO acos (created, modified, id, parent_id, model, foreign_key, alias, lft, rght) VALUES
java.sql.SQLException: near "VALUES": syntax error
at org.sqlite.DB.throwex(DB.java:288)
at org.sqlite.NativeDB.prepare(Native Method)
at org.sqlite.DB.prepare(DB.java:114)
at org.sqlite.Stmt.executeUpdate(Stmt.java:102)
at ReadMySqlDump.<init>(ReadMySqlDump.java:94)
at ReadMySqlDump.main(ReadMySqlDump.java:113)
以下是mydb.sql.sql的內容
begin;
-- phpMyAdmin SQL Dump
-- version 3.5.1
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Jan 30, 2013 at 11:55 AM
-- Server version: 5.5.24-log
-- PHP Version: 5.3.13
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
--
-- Database: abc
--
-- --------------------------------------------------------
--
-- Table structure for table acos
--
CREATE TABLE IF NOT EXISTS acos (
created datetime NOT NULL,
modified datetime NOT NULL,
id integer NOT NULL ,
parent_id integer DEFAULT NULL,
model varchar(255) DEFAULT NULL,
foreign_key integer DEFAULT NULL,
alias varchar(255) DEFAULT NULL,
lft integer DEFAULT NULL,
rght integer DEFAULT NULL,
);
--
-- Dumping data for table acos
--
INSERT INTO acos (created, modified, id, parent_id, model, foreign_key, alias, lft, rght) VALUES
('0000-00-00 00:00:00', '0000-00-00 00:00:00', 1, NULL, NULL, NULL, 'controllers', 1, 1124),
('0000-00-00 00:00:00', '0000-00-00 00:00:00', 2, 1, NULL, NULL, 'Pages', 2, 15);
-- --------------------------------------------------------
--
-- Table structure for table aros
--
CREATE TABLE IF NOT EXISTS aros (
created datetime NOT NULL,
modified datetime NOT NULL,
id integer NOT NULL ,
parent_id integer DEFAULT NULL,
model varchar(255) DEFAULT NULL,
foreign_key integer DEFAULT NULL,
alias varchar(255) DEFAULT NULL,
lft integer DEFAULT NULL,
rght integer DEFAULT NULL
);
--
-- Dumping data for table aros
--
INSERT INTO aros (created, modified, id, parent_id, model, foreign_key, alias, lft, rght) VALUES
('0000-00-00 00:00:00', '0000-00-00 00:00:00', 1, NULL, 'Group', 1, NULL, 1, 12),
('0000-00-00 00:00:00', '0000-00-00 00:00:00', 2, NULL, 'Group', 2, NULL, 13, 18);
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
commit;
我從這里使用jdbc jar
如果有人可以幫助解決此問題,將不勝感激。
發送到服務器的INSERT語句似乎不完整,在VALUES之后被截斷,而它應該包含文件中以下兩行所提供的數據。
您沒有從文件中正確獲取INSERT語句。 對於CREATE來說,它是可以完成的:您正在管理currentCreate
標志並連接語句行。 您未針對INSERT執行此操作:
if(buff.startsWith("INSERT")) {
inserts.add(buff);
continue;
}
在這里,您只需要第一行( buff
是br.readLine()
的結果),其余的將被忽略。
解決方案是對INSERT執行與CREATE相同的操作:當看到INSERT時,將后續行連接起來,直到看到語句完成為止,即該行以);
結尾);
。
好的,我自己解決了此問題,方法是在代碼中包含以下內容:
if(!insertStart && (buff.startsWith("INSERT") && buff.endsWith("VALUES"))) {
insertStart = true;
insertMain = buff;
continue;
}
if(insertStart && (buff.endsWith("),"))){
buff = buff.substring(0, buff.length()-1);
insertQuery = insertMain+" " + buff+";";
inserts.add(insertQuery);
continue;
}
if(insertStart && (buff.endsWith(");"))){
insertQuery = insertMain+" " + buff+";";
inserts.add(insertQuery);
insertStart = false;
insertMain = "";
continue;
}
PS:SQLite不接受我在問題中顯示的形式的INSERT語句。 對於每個INSERT語句,它將僅接受一組值。 因此,我不得不將此多行INSERT轉換為單個INSERT語句
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.