我有一個帶有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. 用於閱讀文本的java代碼
- 3. 從php文本文件讀取數據並創建數據庫
- 4. 以Java讀取文本文件並使用數據
- 5. 閱讀sqlite數據庫文件,並從中獲取文本C#
- 6. 在Java中使用JSON數據讀取文本文件
- 7. Java讀取文本文件2列並存儲在數組中
- 8. 用於讀取java中的excel文件的代碼的代碼優化
- 9. 從JAVA代碼中讀取和插入SVN庫中的文件
- 10. 使用Java從文件和更新數據庫表中讀取日期
- 11. java從數據庫讀取編碼數據並創建pdf文件
- 12. Java:在更新時讀取文件
- 13. 從數據庫中讀取數據,並在一個txt文件
- 14. 讀取文本文件,並更新到使用LINQ
- 15. 從文本文件中讀取並更新
- 16. 讀取文本文件並填充數組中的數據。
- 17. shell腳本在unix中逐行讀取文件並更新文件中的行
- 18. 從java數據庫讀取docx文件在java中
- 19. 使用java從文本文件中讀取數據行
- 20. 從文本文件中讀取數據並將數據分配給新變量
- 21. 從文本文件中讀取數據,並使用C++語言
- 22. 讀取文本文件並在C++中顯示數據
- 23. FileNotFoundException - 在java中讀取文本文件
- 24. 在java中讀取文本文件
- 25. 在android中讀取文本文件到數據庫
- 26. 在xml文件中讀取數據並將其用於標籤
- 27. 如何從Java中的文本文件讀取數據的GRID?
- 28. 從文件中讀取並在文本區域的java顯示
- 29. 從文本文件中讀取數據
- 30. 從文本文件中讀取數據
歡迎SO。對你有好處,但你爲什麼要告訴我們?請看[ask] – AxelH
向我們展示我們的代碼,目前爲止 – Abhishek
您必須指定一個難度來幫助您 - 您做了什麼,您卡在哪裏。 – yakobom