一个简单的从txt文件中读取数据插入数据库
来源:互联网 发布:ipowerl手指充电软件 编辑:程序博客网 时间:2024/04/27 22:50
一: 首先我们应该先·建一个Dynamic web project 工程。
二: 定义一个读文件操作 package com.what.files;
在这里我们要用到用于读取本地文件中的字节数据的FileInputStream,FileInputStream继承了InputStream。
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.math.BigDecimal;
public class ReadFile {
public static void readTxtFile(String filePath){
InfoBean infoBean = new InfoBean();
try {
String encoding = "utf-8";
File file = new File("D:\\数据.txt");
if(file.isFile() && file.exists()){//判断该文件是否存在
InputStreamReader read = new InputStreamReader(new FileInputStream(file) , encoding);
BufferedReader buffereReader = new BufferedReader(read);
String lineTxt = null;
OperationDB operationDB = new OperationDB();
lineTxt = buffereReader.readLine();
while((lineTxt = buffereReader.readLine()) != null){
String[] s = lineTxt.split("\\|");
infoBean.setSeqNo(new BigDecimal(s[0]));
//(s[0].toString());
infoBean.setBillNo(s[1].toString());
infoBean.setPolicyNo(s[2].toString());
infoBean.setCertiNo(s[3].toString());
infoBean.setAmount(new BigDecimal(s[4]));
operationDB.addRcorder(infoBean);
System.out.println(lineTxt);
}
read.close();
} else {
System.out.println("找不到指定的文件");
}
} catch (Exception e) {
System.out.println("读取文件内容出错");
e.printStackTrace();
}
}
}
三:写一个链接数据库的类
创建jdbc数据库的一般步骤为:
@1.注册驱动(加载驱动,此次加载只做一次),在链接数据库之前首先要加载链接数据库的驱动JVM(即java虚拟机)
通过Java.lang.Class中的forName(String ClassName)方法实现.
如:【
try{
Class.forName("com.mySql.jdbc.Driver");
}catch(ClassNotFoundException e){
System.out.println("加载失败,找不到驱动程序");
e.printStackTrace();
}
】
@2.提供链接数据库jdbc的URL
链接URL定义了连接数据库时的协议,子协议,数据源标识。
格式:协议,子协议,数据源标识。
协议:以jdbc开始。
子协议:是桥连接的数据库程序或数据库管理系统的名称
数据源标识:标记找到数据库来源的地址与链接端口;
如:
jdbc : mySql;
/localhost:3306/test?useUnicode=true&characterEncoding=gbk ; useUnicode=true:表示使用Unicode字符集。如果characterEncoding设置为 gb2312或GBK,本参数必须设置为true 。characterEncoding=gbk:字符编码方式。@3.
创建数据库的来链接。
首先向javav.sql.DriverManag获得Connection链接对象,该对象表示一个数据库的链接。
使用DriverManage的一个getConnection(String url, String username,String password)的方法传入指定要链接的数据库的地址,用户名,密码。
【
String url = "jdbc:mysql://localhost:3306/test" ; String username = "root" ; String password = "root" ; try{ Connection con = DriverManager.getConnection(url , username , password ) ; }catch(SQLException se){ System.out.println("数据库连接失败!"); se.printStackTrace() ; }
】
@4.
创建一个Statement
要执行SQL 语句,必须获得java.sal.Statement实例。Statement实例分为三种类型(1)执行静态SQL,通过Statement实例实现。
(2)执行动态SQL,通过PareStatement实例实现。
(3)执行数据库存储过程,通过CallableStatement实例实现。
Statement stmt = con.createStatement();
PareparedStatement pstmt = con.prepareStatement(sql);
CallableStatement cstmt = con.prepareCall("{CALL demoSp(?,?)}");
@5.执行Sql语句
statement提供了三种执行SQL语句的方法:excute,excuteQuery,excuteUpdate,
(1).ResultSet excuteQuery(String sqlString),执行查询数据库的SQL语句,返回一个ResultSet对象。
(2).int excuteUpdate(String sqlString),用于执行InSERT,UPDATE,DELETE,以及SQL DDL语句如CREATE TABLE,DROP TABLE。
(3).excute(String sqlString)用于执行返回多个结果集,多个更新计数或者二者组合的语句。
【
- ResultSet rs = stmt.executeQuery("SELECT * FROM ...") ;
- int rows = stmt.executeUpdate("INSERT INTO ...") ;
- boolean flag = stmt.execute(String sql) ;
】
@6.处理结果
(1)执行本次更新影响到的记录数
(2)执行返回的结果是一个ResultSet对象。
- 【 ResultSet包含符合SQL语句中条件的所有行,并且它通过一套get方法提供了对这些
- 行中数据的访问。
- • 使用结果集(ResultSet)对象的访问方法获取数据:
- while(rs.next()){
- String name = rs.getString("name") ;
- String pass = rs.getString(1) ; // 此方法比较高效
- }
- (列是从左到右编号的,并且从列1开始)
@7关闭数据库连接
操作完成之后要把使用过的jdbc对象全部关闭。以释放jdbc链接资源,关闭顺序和声明顺序相反。
【
- 1、关闭记录集
- 2、关闭声明
- 3、关闭连接对象
- if(rs != null){ // 关闭记录集
- try{
- rs.close() ;
- }catch(SQLException e){
- e.printStackTrace() ;
- }
- }
- if(stmt != null){ // 关闭声明
- try{
- stmt.close() ;
- }catch(SQLException e){
- e.printStackTrace() ;
- }
- }
- if(conn != null){ // 关闭连接对象
- try{
- conn.close() ;
}catch(SQLException e){ e.printStackTrace() ; } }
】
package com.files.insert;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class BeyondbConnection {
public static Connection getConnection() {
Connection con = null;
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://10.8.9.11:3306/trshdev_dw_aml";
String user = "trshdev_dw_aml";
String password = "123456";
try {
con = DriverManager.getConnection(url, user, password);
} catch (SQLException ex) {
Logger.getLogger(BeyondbConnection.class.getName()).log(Level.SEVERE, null, ex);
}
return con;
}
}
package com.files.insert;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class OperationDB {
private Connection con = null;
public void addRcorder(InfoBean infoBean) throws SQLException {
if(con ==null){
con = BeyondbConnection.getConnection();
}
String sql = "insert into t_pa_nb_list (seq_no,bill_no,policy_no,certi_no,amount) values(?,?,?,?,?)";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setBigDecimal(1, infoBean.getSeqNo());
//setString(1, infoBean.getSeqNo());
pstmt.setString(2, infoBean.getBillNo());
pstmt.setString(3, infoBean.getPolicyNo());
pstmt.setString(4, infoBean.getCertiNo());
pstmt.setBigDecimal(5, infoBean.getAmount());
pstmt.executeUpdate();
}
}
四:定义一个类用于将读取的内容存到数据库中
package com.files.insert;
import java.math.BigDecimal;
public class InfoBean {
private BigDecimal seqNo;
private String billNo;
private String policyNo;
private String certiNo;
private BigDecimal amount;
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;
}
}
五: 主函数:
package com.files.insert;
public class Text {
public static void main(String[] args){
String filePath = "D:\\数据.txt";
ReadFile readFile=new ReadFile();
readFile.readTxtFile(filePath);
}
}
- 一个简单的从txt文件中读取数据插入数据库
- 最近写了一个从数据库中读取文件并写在txt文件中(当数据过多的时候写在多个txt文件中)
- 从一个txt文件中读取矩阵
- 从一个txt文件中读取矩阵
- vc++从txt文件中读取数据
- VTK从TXT文件中读取数据
- c++从txt文件中读取数据
- Ajax的一个简单例子--从一个文件文件中读取数据显示到页面中
- MATLAB中批量从txt文件中读取指定行的数据保存为txt文件
- php读取txt文件并将数据插入到数据库
- php读取txt文件并将数据插入到数据库
- php读取txt文件并将数据插入到数据库
- 从数据库中读取表中数据然后写到txt文件中
- 【示例】Hibernate中读取txt的文件内容并插入到数据库
- 简单的从数据库中读取数据自动生成XML
- 读取从a.txt中读取一个字符串,排序后写入b.txt文件中
- 简洁的从txt文件中读取数据并且修改数据重新存入
- C++利用CStdioFile从txt文件中读取数据
- win 10 vmware 虚拟网卡无法安装解决方法
- Error connecting to database [No such file or directory]
- 循环队列 作为消息队列
- hdu 3631 Shortest Path【Floyd】
- 【GDOI2014模拟】网格
- 一个简单的从txt文件中读取数据插入数据库
- 对‘pthread_create’未定义的引用 对‘pthread_join’未定义的引用
- 计算机组成原理运算器的编程实现
- 用Kibana和logstash快速搭建实时日志查询、收集与分析系统
- IOS开发支持64位
- God Chang's interview——异或
- 1001 ProblemA
- java--------接口
- 服务器的相关配置