0
即時通訊嘗試使用java將一個txt文件轉換爲sqlite。InputMismatchException?我仍然無法弄清楚
(ID,名稱,類別,x座標,y座標,長度,寬度,FLOOR)
類型是有序 INTEGER文本文本INT INT INT INT。 (I由AUTOINCREMENT創建ID。)
一個例子是像
maleToilet room -58 0 58 48 9 femaleToilet room -58 -48 58 48 9
這裏是主代碼:
import java.sql.*;
import java.io.*;
import java.util.*;
class Read{
public Scanner input;
public void openFile() {
try {
input = new Scanner(new File("D:\\room.txt"));
} catch (FileNotFoundException fileNotFoundException) {
System.err.println("Error opening file.");
System.exit(1);
}
}
public void closeFile() {
if (input!=null)
input.close();
}
}
public class TxtToSqlite
{
public static void main(String args[])
{
Read r = new Read();
Connection c = null;
Statement stmt = null;
try {
Class.forName("org.sqlite.JDBC");
c = DriverManager.getConnection("jdbc:sqlite:test.db");
c.setAutoCommit(false);
stmt = c.createStatement();
//create the schema
/*String sql = "CREATE TABLE ROOM " +
"(ID INTEGER PRIMARY KEY AUTOINCREMENT," +
" NAME TEXT NOT NULL, "+
" CATEGORY TEXT NOT NULL, "+
" XCOORDINATE REAL NOT NULL, "+
" YCOORDINATE REAL NOT NULL, "+
" LENGTH REAL NOT NULL, "+
" WIDTH REAL NOT NULL, "+
" FLOOR INT NOT NULL)";*/
r.openFile();
String sql = null;
int i = 1;
while(r.input.hasNext()){
sql = "INSERT INTO ROOM (NAME,CATEGORY,XCOORDINATE,YCOORDINATE,LENGTH,WIDTH,FLOOR) " +
"VALUES ("+"'"+r.input.next()+"', '"+r.input.next()+"', "+
r.input.nextInt()+", "+r.input.nextInt()+", "+
r.input.nextInt()+", "+r.input.nextInt()+", "+r.input.nextInt()+");";
stmt.executeUpdate(sql);
i++;
}
r.closeFile();
stmt.close();
c.close();
} catch (InputMismatchException e) {
System.out.print("Input Error!");
} catch (Exception e) {
System.err.println(e.getClass().getName() + ": " + e.getMessage());
System.exit(0);
}
} }
但將一個InputMismatchException。所以,任何人都可以幫助我嗎?謝謝:)
順便說一句,我下載sqlite-jdbc-3.7.2.jar從 http://www.tutorialspoint.com/sqlite/sqlite_java.htm 並使其進入引用的庫。
請添加'stacktrace'你正在''exception'獲得。還要添加'sql schema'關於你如何創建表? – Smit
此[異常](http://docs.oracle.com/javase/7/docs/api/java/util/InputMismatchException.html)來自您的'Scanner'對象在以下情況下:「*檢索到的令牌確實不符合預期類型的模式,或者令牌超出預期類型的範圍*「 –
每個字段的數據類型是主要原因嗎? 像XCOORDINATE for REAL,但我使用nextInt()? – newman