![](/img/trans.png)
[英]I can't figure out the “Java InputMismatchException errors” in my code
[英]InputMismatchException? I still can't figure it out
我试图用Java将txt文件制作成sqlite。
(ID,名称,类别,XCOORDINATE,YCOORDINATE,LENGTH,WIDTH,FLOOR)
类型是有序的INTEGER文本text int int int。 (我通过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。 那么,有人可以帮我吗? 谢谢:)
顺便说一句,我从http://www.tutorialspoint.com/sqlite/sqlite_java.htm下载sqlite-jdbc-3.7.2.jar并将其放入引用的库中。
正如史密斯所说,
我通过将ID数据类型更改为INTEGER并将其设置为AUTOINCREMENT以使其更容易来更正原始数据。
然后
删除
c.setAutoCommit(false);
在主代码中使其工作。
谢谢大家的回答! :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.