텍스트 파일에 ID 목록이 있습니다. 각 ID를 읽고 DB에서 관련 ID를 검색하고 새 값으로 특정 열을 업데이트해야합니다.텍스트 파일을 읽고 데이터베이스에서 업데이트하기위한 Java 코드
답변
나는 무엇을 할 것인지에 대한 간단한 데모를 제공하려고 노력했습니다.
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
처럼 보일 것입니다 귀하의 DB 정보와 annotatedClasses
귀하의 IdFile로 LookUpDemoTable
클래스에 정의 된 SessionFactory에 콩을 가져야한다 :이 도움이
IDAB12
IDAC24
IDAD89
희망.
당신은이 링크를 참조 할 수 있습니다
문제에 대한 안내에 도움을 주신 덕분에 – Keerthisairam
은 완전한 출력 세트를 얻으면 출력을 제공합니다. – Keerthisairam
- 1. 안드로이드 앱 데이터베이스를 읽고 업데이트하기위한 아키텍처
- 2. .txt 파일을 읽고 이진 표현으로 변환하는 Java 코드
- 3. mySQL 데이터베이스에서 읽고 쓰는 온라인 Java 애플릿?
- 4. JAVA : 함께 파일을 읽고 쓰십시오.
- 5. 텍스트 파일을 읽고 목록에 쓰기
- 6. 서버에서 텍스트 파일을 읽고 asp.net
- 7. 텍스트 파일을 읽고 데이터를 해석합니다.
- 8. 텍스트 파일을 읽고 사전에 저장하십시오.
- 9. java : 동시에 파일을 읽고 쓰고 동시성을 파일
- 10. 텍스트 파일을 읽고 무작위로 텍스트 줄을 표시하십시오.
- 11. JavaFX에서 텍스트 파일을 읽고 텍스트 영역에 표시
- 12. 텍스트 파일에서 텍스트 파일을 읽고 텍스트로 출력하십시오.
- 13. 텍스트 파일을 읽고 내가 Expenses.txt라는 파일을 읽고있다
- 14. Java : 텍스트 파일을 읽고 서식을 지정하는 동안 앞에 0을 유지하십시오.
- 15. Java - 텍스트 파일을 읽고 클래스 값으로 행 변환
- 16. java 서블릿에서 외부 텍스트 파일을 읽고 쓰는 응용 프로그램을 실행하십시오.
- 17. Java - BZ2 파일을 읽고 압축을 풀다.
- 18. 자바에서 텍스트 파일을 읽고 문자열에게 그것을
- 19. 업로드에서 텍스트 파일을 읽고 django를 사용하여 텍스트 파일에 다시 저장하십시오.
- 20. 파일을 한 줄씩 읽고 Java 프로그램을 실행하십시오.
- 21. java 디렉토리에있는 파일을 읽고 디렉토리에 오프셋을 저장합니다.
- 22. appengine에서 텍스트 파일을 읽고 쓰는 방법은 무엇입니까?
- 23. 텍스트 파일에서 읽고 텍스트를 비교 - Java
- 24. VB.NET을 사용하여 동시에 텍스트 파일을 읽고 쓰십시오.
- 25. 구성 파일을 업데이트하기위한 쉘 명령
- 26. (filechooser와) 텍스트 파일을 읽고 (각 라인은 ArrayList를
- 27. SGML 파일을 읽는 Java 코드
- 28. 텍스트 파일을 읽고 정렬하는 방법은 무엇입니까?
- 29. C# windowsForm에 저장된 동일한 형식으로 데이터베이스에서 검색된 파일을 읽고 싶습니다.
- 30. 텍스트 파일을 읽고 7 줄마다 'Page'를 만듭니다.
가 SO에 오신 것을 환영합니다. 좋은데 왜 우리에게 말하고있는거야? 제발, [ask]를 보시오. – AxelH
지금까지 저희에게 ur 코드를 보여주십시오. – Abhishek
도와 주실 수없는 어려움을 지정하십시오. 무엇을했는지, 어디서 붙어 있습니까? – yakobom