java小程序从数据库中(表)中读取数据然后创建文件夹生成txt文件

最近在写文件文件读写的功能对读写操作有了更深一层的理解。

在java中stream代表一种数据源,javaio的底层数据源。

在任何有能力产生数据源的javaio对象就可以看作是一个InputStream对象。他能产生数据源我们便能将数据取出来,java对封装的通用方法就用read()方法。

反之任何有能力接受数据源的javaio对象我们就可以看作是一个outputsteram 对象,能接受数据我们就可以调用它的write方法,

下面是一些常用的流。

基于字节流的stream:

dataOutputstream          dataInputStream

FileOutputStream           FileInputStream

基于字符流的Stream

FileWriter         FileReader

StringWriter     StringReader


输出数据:

void write(int b) 往流中写一个字节b

void write(byte b[])王字节中写一个字节数组b

void write(byte b[],int off,int len)把字节数组b中从下标off开始,长度问len的字节写入流中

最后关闭流。OutputStream是流的形式,具体可以表现为FileOutputStream的形式进行整行的写入。
可以通过“FileOutputStream”创建文件实例,之后过“OutputStreamWriter”流的形式进行存储,举例:
OutputStreamWriter pw = null;//定义一个流
pw = new OutputStreamWriter(new FileOutputStream(“D:/test.txt”),"GBK");//确认流的输出文件和编码格式,此过程创建了“test.txt”实例
pw.write("我是要写入到记事本文件的内容");//将要写入文件的内容,可以多次write
pw.close();//关闭流

outputStream

下面是我写的一个简单的小项目。



一:首先创建一个对象

package com.files.readdata;


import java.math.BigDecimal;
/**
 * 类
 * @author Frank.dai
 *
 */
public class InfoBean {

private BigDecimal seqNo;
private String billNo;
private String policyNo;
private String certiNo;
private BigDecimal amount;
private String returnCode;
private String returnDesc;

public BigDecimal getSeqNo() {
return seqNo;
}
public void setSeqNo(BigDecimal seqNo) {
this.seqNo = seqNo;
}
public String getBillNo() {
return billNo;
}
public void setBillNo(String billNo) {
this.billNo = billNo;
}
public String getPolicyNo() {
return policyNo;
}
public void setPolicyNo(String policyNo) {
this.policyNo = policyNo;
}
public String getCertiNo() {
return certiNo;
}
public void setCertiNo(String certiNo) {
this.certiNo = certiNo;
}
public BigDecimal getAmount() {
return amount;
}
public void setAmount(BigDecimal amount) {
this.amount = amount;
}
public String getReturnCode() {
return returnCode;
}
public void setReturnCode(String returnCode) {
this.returnCode = returnCode;
}
public String getReturnDesc() {
return returnDesc;
}
public void setReturnDesc(String returnDesc) {
this.returnDesc = returnDesc;
}



}

二:写一个来连接数据库的操作

package com.files.readdata;


import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;


public class DbConnection {

public static Connection getConnection(){

Connection con = null;

Properties properties = new Properties();
InputStream in = Thread.currentThread().getClass().getResourceAsStream("/database.properties");
try {
properties.load(in);
String driver = properties.getProperty("driver");
if(driver != null){
System.setProperty("jdbc.drvers", driver);
}

String url = properties.getProperty("jdbc.url");
String username = properties.getProperty("jdbc.username");
String password = properties.getProperty("jdbc.password");
try {
con = DriverManager.getConnection(url,username,password);
} catch (SQLException e) {
e.printStackTrace();
Logger.getLogger(DbConnection.class.getName()).log(Level.SEVERE, null, e);;
}
} catch (IOException e1) {
e1.printStackTrace();
}

return con;
}


}

三:创建对数据库操作的类

package com.files.readdata;


import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.sql.Co

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这个问题可以回答。以下是一个简单的Java程序,可以从文件读取数据并将其批量插入到数据库: ```java import java.io.BufferedReader; import java.io.FileReader; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; public class InsertDataFromTextFile { public static void main(String[] args) { String jdbcUrl = "jdbc:mysql://localhost:3306/mydatabase"; String username = "myusername"; String password = "mypassword"; String filename = "data.txt"; String tableName = "mytable"; try (Connection conn = DriverManager.getConnection(jdbcUrl, username, password); BufferedReader br = new BufferedReader(new FileReader(filename))) { String line; String sql = "INSERT INTO " + tableName + " (col1, col2, col3) VALUES (?, ?, ?)"; PreparedStatement pstmt = conn.prepareStatement(sql); while ((line = br.readLine()) != null) { String[] data = line.split(","); pstmt.setString(1, data[0]); pstmt.setString(2, data[1]); pstmt.setString(3, data[2]); pstmt.addBatch(); } pstmt.executeBatch(); System.out.println("Data inserted successfully."); } catch (Exception e) { e.printStackTrace(); } } } ``` 这个程序假设你已经有一个MySQL数据库,并且已经创建了一个名为"mydatabase"的数据库,以及一个名为"mytable"的,该有三个列(col1, col2, col3)。程序从一个名为"data.txt"的文本文件读取数据,每行数据用逗号分隔,然后将数据批量插入到"mytable"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值