![](/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.