JDK8读取access数据库的数据并写到.txt文件中
程序员文章站
2022-05-23 10:52:46
...
根据公司领导要求,要把门禁系统里的.mdb文件的部分数据读写成.txt文件,所以简单写了一个小demo。
需要的jar包
jar包下载官网地址: https://sourceforge.net/projects/ucanaccess/
maven:
<dependency>
<groupId>net.sf.ucanaccess</groupId>
<artifactId>ucanaccess</artifactId>
<version>4.0.1</version>
</dependency>
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
/**
* 功能描述:获取access数据
*
* @author levono
* @date 2020/2/24
*/
public class GetAccessData{
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
GetAccess();
}
/**
* 连接access数据库,并将数据输出到指定的位置
* @throws Exception
*/
public static void GetAccess() throws Exception{
String password="168168";
try{
//创建数据库的连接
//加载驱动
Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
//根据数据库文件路径建立连接
Connection conn = DriverManager.getConnection("jdbc:ucanaccess://D:\\iCC.mdb",null,password);
Statement stmt = conn.createStatement();
//执行SQL语句
ResultSet rs = stmt.executeQuery("select gro.f_GroupName,con.f_ConsumerNO,con.f_ConsumerName,att.f_ShiftDate" +
",DateDiff('m',att.f_ShiftDate,now()) <=1,att.f_OnDuty1,att.f_OffDuty1 " +
"from (t_b_Consumer con left join t_d_shift_AttReport att on con.f_ConsumerID = att.f_ConsumerID) " +
"left join t_b_Group gro on con.f_GroupID = gro.f_GroupID");
File writename = new File("D:\\RegistrationForm.txt");
//创建新文件
writename.createNewFile();
BufferedWriter bufferedWriter = new BufferedWriter(
new OutputStreamWriter(new FileOutputStream(writename), "UTF-8"));
StringBuilder builder = new StringBuilder();
while(rs.next()){
builder.append('"'+rs.getString(1)+'"'+","
+'"'+rs.getString(2)+'"'+","
+'"'+rs.getString(3)+'"'+","
+'"'+rs.getString(4)+'"'+","
+'"'+rs.getString(5)+'"'+","
+'"'+rs.getString(6)+'"'+","
+'"'+rs.getString(7)+'"'
+"\r\n");
}
bufferedWriter.write(builder.toString());
//把缓存区内容压入文件
bufferedWriter.flush();
//关闭连接
bufferedWriter.close();
rs.close();
stmt.close();
conn.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
文件样式:
"项目部","1","小明","2020-01-21","TRUE","2020-01-21","2020-01-21"
推荐阅读
-
php读取txt文件组成SQL并插入数据库的代码(原创自Zjmainstay)
-
Python实现读取txt文件中的数据并绘制出图形操作示例
-
php读取txt文件组成SQL并插入数据库的代码(原创自Zjmainstay)_PHP教程
-
php读取txt文件组成SQL并插入数据库的代码(原创自Zjmainstay)_php技巧
-
JDK8读取access数据库的数据并写到.txt文件中
-
php读取txt文件组成SQL并插入数据库的代码(原创自Zjmainstay)_php技巧
-
php读取txt文件组成SQL并插入数据库的代码(原创自Zjmainstay)_PHP教程
-
php读取txt文件组成SQL并插入数据库的代码
-
php读取txt文件组成SQL并插入数据库的方法
-
php读取txt文件组成SQL并插入数据库的代码