我的任务是通过 jdbc 代码将制表符分隔的文件加载到 psql 数据库中。我使用PreparedStatement 来执行查询。但我无法将数据加载到数据库,我遇到这样的异常“INSERT 的表达式多于目标列”。这里我附上了我的代码,请帮助我。
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class process {
static Connection connection = null;
public static void instertable(String line, Connection connection) {
String values[] = null;
PreparedStatement pst = null;
int i = 0;
try {
pst = connection.prepareStatement("insert into tablename values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
values = line.split("\t");
System.out.println("len" + values.length);
for (i = 0; i <= values.length; i++) {
System.out.println(values[i]);
pst.setString(i + 1, values[i]);
}
System.out.println("ii " + i);
System.out.println("pst " + pst);
pst.executeUpdate();
System.out.println("load data database");
} catch (Exception ex) {
System.out.println("load error:" + ex.getMessage());
}
}
public static void readfile(String FileName, Connection connection) {
BufferedReader bufferedreader = null;
String line = null;
try {
bufferedreader = new BufferedReader(new FileReader(FileName));
while ((line = bufferedreader.readLine()) != null) {
// System.out.println(line);
instertable(line, connection);
}
} catch (Exception ex) {
ex.getMessage();
}
}
public static void main(String[] args) throws IOException {
String FileName = "i.txt";
//String LoadOutput=args[3];
try {
Class.forName("org.postgresql.Driver");
connection = DriverManager.getConnection(
"jdbc:postgresql://127.0.0.1:5432/dbname", "username",
"password");
readfile(FileName, connection);
} catch (SQLException e) {
System.out.println("Connection Failed! Check output console");
e.printStackTrace();
} catch (Exception e) {
System.out.println("Exception message : " + e.getMessage());
}
}
}
最佳答案
这里有一个错误:
for (i = 0; i <= values.length; i++) {
<=
应该是<
:当i
是 values.length
, values[i]
将导致ArrayIndexOutOfBoundsException
被扔掉。您对循环的迭代次数太多了。
当 ArrayIndexOutOfBoundsException
被抛出时,异常消息可能只包含错误的索引。这可以解释为什么你得到 load error: 32
作为您自己的消息。
顺便说一句,您在一定程度上是自己错误处理不当的受害者:
catch (Exception ex) {
System.out.println("load error:"+ex.getMessage());
}
你正在捕捉Exception
,这很一般,只显示错误消息。
相反,我建议捕获 SQLException
(通常确实有有用的错误消息)而不是 Exception
.
最后,您还应该关闭 PreparedStatement
。在 finally
中执行此操作 block :
finally {
if (pst != null) {
try {
pst.close();
}
catch (SQLException e) {
System.out.println("Error closing prepared statement: " + e.getMessage());
}
}
}
正如您编辑的评论所证明的,下一个问题在这里:
pst = connection.prepareStatement("insert into tablename values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
您收到的消息表明您的表少于 32 列,因此无法插入所有数据。我建议将列名称添加到 INSERT
声明:
pst = connection.prepareStatement("insert into tablename (column1, column2, ..., column32) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
通过指定列名,您可以修复列的顺序,并且还可以确保将来向表中添加任何列时不会突然收到错误消息。
关于java - 将制表符分隔的文件加载到 jdbc 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23195918/