我有一个带有ID列表的文本文件,我必须读取其中的每个ID并在数据库中搜索相关ID并使用新值更新特定列。用于读取文本文件并在数据库中更新的Java代码
-1
A
回答
1
我试图给你一个简要的演示,该怎么做。
下面的代码:
public class Test {
private static final String FILENAME = "D:/Idfile.txt"; // Your Id file
private SessionFactory sessionFactory;
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("resource/spring/applicationContext.xml");
Test test = new Test();
test.readFile();
}
private void readFile() {
BufferedReader br = null;
FileReader fr = null;
try {
fr = new FileReader(FILENAME); // You read the file here
br = new BufferedReader(fr);
String sCurrentLine;
br = new BufferedReader(new FileReader(FILENAME));
while ((sCurrentLine = br.readLine()) != null) {
if (!checkIfIdExists(sCurrentLine)) {
System.out.println("Some problem"); // Handle the exception
// scenario here.
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();
if (fr != null)
fr.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
private boolean checkIfIdExists(String sCurrentLine) {
Session session = null;
Transaction tx = null;
try {
session = sessionFactory.openSession();
session.setFlushMode(FlushMode.AUTO);
tx = session.beginTransaction();
String sql = "SELECT text_file_id, primary_key from demo_table where text_file_id = :text_file_id "; // Check if the ID is present
SQLQuery query = session.createSQLQuery(sql);
query.addEntity(LookUpDemoTable.class);
query.setParameter("text_file_id", sCurrentLine);
List results = query.list();
if (results.size() != 0) {
for (Iterator<LookUpDemoTable> it = results.iterator(); it.hasNext();) {
LookUpDemoTable lookUpDemoTableToUpdate = new LookUpDemoTable();
LookUpDemoTable lookUpDemoTable = it.next();
lookUpDemoTableToUpdate.setPrimaryKey(lookUpDemoTable.getPrimaryKey());
session.saveOrUpdate(lookUpDemoTableToUpdate); // Incase the ID is present
tx.commit();
}
} else {
LookUpDemoTable lookUpDemoTableToInsert = new LookUpDemoTable();
lookUpDemoTableToInsert.setPrimaryKey(new Long(System.currentTimeMillis()).toString());
lookUpDemoTableToInsert.setTextFileId(sCurrentLine);
session.save(lookUpDemoTableToInsert); // Incase the ID is not present
tx.commit();
}
return true;
} catch (Exception e) {
tx.rollback();
} finally {
if (null != session) {
session.close();
}
}
return false;
}
}
的LookUpDemoTable
类
@Entity
@Table(name = "demo_table")
public class LookUpDemoTable {
@Id
@Column(name = "primary_key")
private String primaryKey;
@Column(name = "text_file_id")
private String textFileId;
public String getPrimaryKey() {
return primaryKey;
}
public void setPrimaryKey(String primaryKey) {
this.primaryKey = primaryKey;
}
public String getTextFileId() {
return textFileId;
}
public void setTextFileId(String textFileId) {
this.textFileId = textFileId;
}
}
你会applicationContext.xml
并把它在main()
方法中提到的路径,也就是resource/spring/applicationContext.xml
你applicationContext.xml
应该看起来像:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:batch="http://www.springframework.org/schema/batch" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="http://www.springframework.org/schema/batch
http://www.springframework.org/schema/batch/spring-batch-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
<context:annotation-config />
<import resource="database.xml" />
<bean id="test"
class="Test"> <!-- Your fully qualified class name -->
<property name="sessionFactory" ref="sessionFactory" />
</bean>
</beans>
你database.xml
应该有你的数据库的详细信息,并与LookUpDemoTable
类中定义为annotatedClasses
你IdFile SessionFactory的bean应当是这样的:
IDAB12
IDAC24
IDAD89
希望这有助于。
您也可以参考此链接:
+0
感谢您的指导我在这个问题上的帮助 – Keerthisairam
+0
将提供一次输出我有一个完整的输出 – Keerthisairam
相关问题
- 1. 用于阅读文本的java代码
- 2. 阅读sqlite数据库文件,并从中获取文本C#
- 3. 在Java中使用JSON数据读取文本文件
- 4. Java读取文本文件2列并存储在数组中
- 5. java从数据库读取编码数据并创建pdf文件
- 6. 从文本文件中读取并更新
- 7. 读取文本文件并填充数组中的数据。
- 8. shell脚本在unix中逐行读取文件并更新文件中的行
- 9. 使用java从文本文件中读取数据行
- 10. 读取文本文件并在C++中显示数据
- 11. 在java中读取文本文件
- 12. 在android中读取文本文件到数据库
- 13. 如何从Java中的文本文件读取数据的GRID?
- 14. 从文本文件中读取数据
- 15. 从文本文件中读取数据
- 16. 从文本文件中读取数据
- 17. 无法在Matlab中读取文件数据与文本读取
- 18. 使用java读取文本文件的元数据/属性(.txt)
- 19. 使用java读取数据库中的xls文件
- 20. 从C++中的文本文件中读取数据并更改值
- 21. Java代码读取SGML文件
- 22. 用于更新xml文件内容的Java代码
- 23. 导入文本文件并在Java中逐行读取
- 24. 如何读取Java中的.dat文件并将该dat文件中的数据写入文本文件?
- 25. Java中基于文件的数据库
- 26. 在java中选择并读取文件
- 27. 从文本文件中读取列表并加入并解码
- 28. 使用JavaScript读取JSON文件,更新代码
- 29. Java:读取文件并将其数据存储在ArrayList中?
- 30. 用于读取文件并将内容插入到数据库中的性能
欢迎SO。对你有好处,但你为什么要告诉我们?请看[ask] – AxelH
向我们展示我们的代码,目前为止 – Abhishek
您必须指定一个难度来帮助您 - 您做了什么,您卡在哪里。 – yakobom