线程“main”中的异常java.sql.SQLException:near" s":语法错误 [英] Exception in thread "main" java.sql.SQLException: near "s": syntax error
本文介绍了线程“main”中的异常java.sql.SQLException:near" s":语法错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我编写了一个程序,用一个单词列表填充数据库。问题是,每当我尝试运行代码时,它会抛出线程中的异常主java.sql.SQLException:nears:语法错误。我意识到在这个论坛上已经提出了类似的问题,但是在我的代码中我无法纠正错误。
I wrote a program that populates a database with a list of words. Problem is, it throws up the "Exception in thread "main" java.sql.SQLException: near "s": syntax error" every time I try to run the code. I realize similar questions have been asked on this forum, but in the context of MY code I am unable to rectify the error.
因此我求助于你。
这是代码
import java.io.*;
import java.sql.*;
import java.util.Scanner;
public class db_populate {
public static void main(String[] args) throws SQLException, IOException,
ClassNotFoundException {
Connection c = null;
Statement stmt = null;
Scanner in = null;
String word;
String wordcat;
String j, sql;
int i = 0;
Class.forName("org.sqlite.JDBC");
c = DriverManager.getConnection("jdbc:sqlite:dictionary.db");
c.setAutoCommit(false);
System.out.println("Opened database successfully");
stmt = c.createStatement();
in = new Scanner(new File("wordsEn.txt"));
while (in.hasNext()) {
word = in.nextLine();
i++;
j = Integer.toString(i);
wordcat = word.substring(0, 2);
sql = "INSERT INTO WORDS (ID,CAT,WORD) " + "VALUES(" + j + ",'"
+ wordcat + "','" + word + "');";
stmt.executeUpdate(sql);
}
stmt.close();
c.commit();
c.close();
in.close();
System.out.println("Records created successfully");
}
}
这些是我得到的错误当我运行时。
and these are the errors I get when I run.
Opened database successfully
Exception in thread "main" java.sql.SQLException: near "s": syntax error
at org.sqlite.core.NativeDB.throwex(NativeDB.java:397)
at org.sqlite.core.NativeDB._exec(Native Method)
at org.sqlite.jdbc3.JDBC3Statement.executeUpdate(JDBC3Statement.java:116)
at db_populate.main(db_populate.java:34)
推荐答案
使用 PreparedStatement 以避免错误输入的问题:
Use PreparedStatement to avoid problems with erroneous input:
PreparedStatement p = c.prepare("INSERT INTO WORDS (ID,CAT,WORD) VALUES(?, ?, ?)");
p.setInt(1, i);
p.setString(2, wordcat);
p.setString(3, word);
p.execute();
//commit results if using InnoDB, etc
这篇关于线程“main”中的异常java.sql.SQLException:near" s":语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文