gpt4 book ai didi

java - 输入不匹配异常?我还是想不通

转载 作者:行者123 更新时间:2023-12-01 14:02:37 25 4
gpt4 key购买 nike

我正在尝试使用java将txt文件制作成sqlite。

(ID,NAME,CATEGORY,XCOORDINATE,YCOORDINATE,LENGTH,WIDTH,FLOOR)

类型是有序的INTEGER 文本 文本 int int int int。 (我通过自动增量创建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并将其放入引用的库中。

最佳答案

正如斯密特所说,

I correct the original one by changing the ID datatype to INTEGER and set it AUTOINCREMENT to make it easier.

然后

Remove the c.setAutoCommit(false); in the main code to make it work.

谢谢大家的回答! :)

关于java - 输入不匹配异常?我还是想不通,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19231315/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com