使用java读取文本文件以插入特定表
我有这个代码。这个程序是读取文本文件,但无法插入。请建议我 这是我的文本文件使用java读取文本文件以插入特定表,java,file,Java,File,我有这个代码。这个程序是读取文本文件,但无法插入。请建议我 这是我的文本文件 1 2016-07-13 14:51:53 1 255 1 0 1 2016-07-13 14:52:42 1 255 1 0 1 2016-07-13 14:52:51 1 255 1 0 1 2016-07-13 14:53:06 1 255 1 0 1 2016-07-13 14:53:10 1 255 1 0 3 2016-07-16 16:07:
1 2016-07-13 14:51:53 1 255 1 0
1 2016-07-13 14:52:42 1 255 1 0
1 2016-07-13 14:52:51 1 255 1 0
1 2016-07-13 14:53:06 1 255 1 0
1 2016-07-13 14:53:10 1 255 1 0
3 2016-07-16 16:07:34 1 255 1 0
4 2016-07-16 16:08:50 1 255 1 0
5 2016-07-16 16:09:33 1 255 1 0
4 2016-07-16 16:09:57 1 255 1 0
我只想插入一列和第二列,其余的列我不需要
package com.om.whms.examples;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOExcep`
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.Scanner;
public class Hello {
public static void main(String[] args) {
BufferedReader br = null;
String strLine = "";
try {
br = new BufferedReader( new
FileReader("N:/Attendance/BiometricAttendance.txt"));
Connection connection = DBConnection.getConnection();
System.out.println("connection...");
while( (strLine = br.readLine()) != null){
strLine = strLine.trim().replaceAll("( )+", ",");
strLine += "\n";
System.out.println(strLine);
if(!(strLine.startsWith("0") || strLine.startsWith("1") ||
strLine.startsWith("2") || strLine.startsWith("3") ||
strLine.startsWith("4") || strLine.startsWith("5")
|| strLine.startsWith("6") || strLine.startsWith("7") ||
strLine.startsWith("8") || strLine.startsWith("9") )
){ // Lines starting with number 0.
continue;
}
System.out.println("connection..."+strLine);
/*String query = "Insert into attendence values
("+strLine+");";*/
String query ="insert into attendence "
+ " (biometric_id, date, log_time)" + " values ( 1,
2017-07-13, 14:51:53)";
PreparedStatement pstmt =
connection.prepareStatement(query);
pstmt.executeUpdate();
br.close();
}
} catch (FileNotFoundException e) {
System.err.println("Unable to find the file: fileName");
} catch (IOException e) {
System.err.println("Unable to read the file: fileName");
}
catch(Exception e){System.err.println(e.getMessage());}
}
}
你解决了使用此代码…感谢支持你的家伙,包 com.om.whms.examples
import java.io.BufferedReader;
import java.io.FileReader;
import java.sql.Connection;
import java.sql.PreparedStatement;
public class BiometricLogs {
public static void readBiometricFile(String fileName){
BufferedReader br = null;
try{
br = new BufferedReader( new FileReader(fileName));
Connection connection = DBConnection.getConnection();
String strLine = "";
while((strLine = br.readLine()) != null){
String dateStr = strLine.substring(10, 20);
String timeStr = strLine.substring(21, 30).trim();
String idStr = (strLine.substring(0, 9)).trim();
//System.out.println(idStr + "$" + dateStr + "$" + timeStr);
String sql ="insert into attendence (biometric_id, date,
log_time)"
+ " values ( "+idStr+", '"+dateStr+"',
'"+timeStr+"');";
System.out.println(sql);
try{
PreparedStatement pstmt = connection.prepareStatement(sql);
pstmt.executeUpdate();
}catch(Exception e){
System.out.println(e.getMessage());
}
}
}catch(Exception e){
System.out.println(e.getMessage());
e.printStackTrace();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
readBiometricFile("N:\\Attendance\\BiometricAttendance.txt");
}
}
嗯……看看您的SQL语句,您似乎希望利用文件中的三列数据(生物识别id、日期和时间),而不是您所说的两列。它是哪一个?我想像示例一样插入第一列bometric id='1',date=“2017-05-01”,time='09:30:00'就像我已经使用了它所应用的代码…只要看看它我的输出1 2016-07-13,14:51:53 12551 1 0 2 1 2016-07-13 14:51:53 1 255 1 0..它没有插入到数据库更改变量私有静态字符串DB_DRIVER=“oracle.jdbc.driver.OracleDriver”私有静态最终字符串DB_CONNECTION=“jdbc:oracle:thin:@localhost:1521:MKYONG”私有静态最终字符串DB_USER=“USER”私有静态最终字符串DB_PASSWORD=“PASSWORD”更改数据类型preparedStatement.setString(1,split1[0]);preparedStatement.setString(2,split1[1]);preparedStatement.setString(3,split1[2]);文件读取分割值输出生物识别1日期:2016-07-13日志时间14:51:53生物识别1日期:2016-07-13日志时间14:52:42生物识别1日期:2016-07-13日志时间14:52:51生物识别1日期:2016-07-13日志时间14:53:06生物识别1日期:2016-07-13日志时间14:53:10生物识别3日期:2016-07-16g_时间16:07:34生物特征识别码4日期:2016-07-16日志时间16:08:50生物特征识别码5日期:2016-07-16日志时间16:09:33生物特征识别码4日期:2016-07-16日志时间16:09:57
import java.io.BufferedReader;
import java.io.FileReader;
import java.sql.Connection;
import java.sql.PreparedStatement;
public class BiometricLogs {
public static void readBiometricFile(String fileName){
BufferedReader br = null;
try{
br = new BufferedReader( new FileReader(fileName));
Connection connection = DBConnection.getConnection();
String strLine = "";
while((strLine = br.readLine()) != null){
String dateStr = strLine.substring(10, 20);
String timeStr = strLine.substring(21, 30).trim();
String idStr = (strLine.substring(0, 9)).trim();
//System.out.println(idStr + "$" + dateStr + "$" + timeStr);
String sql ="insert into attendence (biometric_id, date,
log_time)"
+ " values ( "+idStr+", '"+dateStr+"',
'"+timeStr+"');";
System.out.println(sql);
try{
PreparedStatement pstmt = connection.prepareStatement(sql);
pstmt.executeUpdate();
}catch(Exception e){
System.out.println(e.getMessage());
}
}
}catch(Exception e){
System.out.println(e.getMessage());
e.printStackTrace();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
readBiometricFile("N:\\Attendance\\BiometricAttendance.txt");
}
}