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