Java 读取文本文件并在数据库中更新的代码
Java code to read a text file and update in database
我有一个包含 ID 列表的文本文件,我必须读取其中的每个 ID 并在数据库中搜索相关 ID 并使用新值更新特定列。
我已尝试向您简要演示如何操作。
代码如下:
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
class
@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
class 定义的 SessionFactory bean 作为其 annotatedClasses
您的 IdFile 应如下所示:
IDAB12
IDAC24
IDAD89
希望这对您有所帮助。
你也可以参考这个link :
我有一个包含 ID 列表的文本文件,我必须读取其中的每个 ID 并在数据库中搜索相关 ID 并使用新值更新特定列。
我已尝试向您简要演示如何操作。
代码如下:
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
class
@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
class 定义的 SessionFactory bean 作为其 annotatedClasses
您的 IdFile 应如下所示:
IDAB12
IDAC24
IDAD89
希望这对您有所帮助。
你也可以参考这个link :