Java字符串用拉丁字符随机替换字符
我正在用Java创建一个程序,将PostgreSQL表中的一些数据移动到另一个表中。每次程序读取一行时,它都会将数据存储在一个SQL查询中,该查询存储在一个字符串对象中 数据将在每读取500行后插入。我遇到的问题是,有时字符串中的字符会被随机替换为拉丁字符,如ȶ、Ȭ、ǎ等,这在查询到数据库时会导致错误。有人知道如何解决这个问题吗 读+写查询:Java字符串用拉丁字符随机替换字符,java,postgresql,unicode,Java,Postgresql,Unicode,我正在用Java创建一个程序,将PostgreSQL表中的一些数据移动到另一个表中。每次程序读取一行时,它都会将数据存储在一个SQL查询中,该查询存储在一个字符串对象中 数据将在每读取500行后插入。我遇到的问题是,有时字符串中的字符会被随机替换为拉丁字符,如ȶ、Ȭ、ǎ等,这在查询到数据库时会导致错误。有人知道如何解决这个问题吗 读+写查询: 可能是编码问题。请检查数据库中的编码设置以及更改环境的任何地方,例如客户端/服务器通信、数据库连接检索等。转换发生在某个地方,而不是JVM本身。@Ray我
可能是编码问题。请检查数据库中的编码设置以及更改环境的任何地方,例如客户端/服务器通信、数据库连接检索等。转换发生在某个地方,而不是JVM本身。@Ray我检查了数据库的编码,它们是UTF-8。这种查询是一个非常糟糕的主意。您应该将准备好的语句与addBatch一起使用,而不是玩弄字符串。更好的是,如果您不在Java中处理数据,请使用直接插入。。。仅使用数据源id进行选择,是Java的已删除标志。我认为您正在对结果字符串进行的字符串替换之一就是原因,或者数据库中已经存在非ASCII字符。很难说。
dat = statement.getResultSet(); //grabbing the data
int index = 0;
while(dat.next())
{
if(index == 0 || index%500 == 0) //check if it's the start of data or already 500 rows read
{
if(index > 0)
{
stm = stm.substring(0, stm.length()-2); // removing the space and comma at the end of final data
sql = new String(stm.getBytes("UTF-8"), "UTF-8"); // this is my attempt at trying to remove the latin char..
statementUnified.execute(sql);
}
stm = "INSERT INTO table (data_source_id, is_deleted_flag,"+columnlist+") values ");
}
stm = stm.concat(" ("+sourceid+",\'0\'");
for(int i = 0;i<columnsize;i++)
{
stm = stm.concat(",");
temp = dat.getString(columnlist.get(i)); //grabbing the data based on the column name
if(temp != null) //replacing escaped char in java and postgresql and removing leading / trailing whitespaces
{
temp = temp.replace("\\", "\\\\\\\\");
temp = temp.replaceAll("\"", "\\\\\\\"");
temp = temp.replaceAll("\'", "\'\'");
temp = temp.replaceAll("%", "%%");
temp = temp.trim();
}
if(temp == null) stm = stm.concat("NULL"); // if data from table or after trimming the whitespaces is NULL, then append NULL to query
// after this there are some validation for email, gender, date of birth, etc.
else //if validation goes well, then the program will go into this ELSE
{
stm = stm.concat("\'"+temp+"\'"); //inserting the data into query
}
}
stm = stm.concat(") , ");
index++;
}
if(stm.endsWith(" , ")) //if the final query didn't reach 500 rows
{
stm = stm.substring(0, stm.length()-2);
sql = new String(stm.getBytes("UTF-8"), "UTF-8");
statementUnified.execute(sql);
}