这是我想要阅读的文件:
20140901|2X-PRG-CZ|PRG|2X|2X-PRG-CZ|||TYPEX||QXTXGXS|QS|QS-PRG-GW|PRG|TYPEB|Y|2|98|0|0
20140901|2X-PRG-CZ|PRG|2X|2X-PRG-CZ|||TYPEX||QXTXGXS|OK|OK-PRG-MQ|PRG|TYPEB|Y|14|672|0|0
20140901|2X-PRG-CZ|PRG|2X|2X-PRG-CZ|||TYPEX||QXTXGXS|EK|EK-PRG-KK|PRG|TYPEB|Y|1|48|0|0
20140901|XS-SIN-OL|MAD|3V|3V-MAD-OO|||STXOL||QXTXGXS|7X|7X-BRU-EP|BRU|TYPEB|Y|1|59|0|0
20140901|XS-SIN-OL|MAD|3V|3V-MAD-OO|||STXOL||QXTXGXS|3V|3V-LGG-SP|LGG|TYPEB|Y|1|193|0|0
20140901|XS-SIN-OL|MAD|3V|3V-MAD-OO|||STXOL||QXTXGXS|7X|7X-PAR-EP|PAR|TYPEB|Y|1|59|0|0
20140901|XS-SIN-OL|MAD|3V|3V-MAD-OO|||STXOL||QXTXGXS|YA|YA-MAD-GS|MAD|TYPEB|Y|4|923|0|0
我的独立程序是这样的..
package com.dbConn;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
public class DbConnection {
public static void main(String[] args) {
int updateQuery = 0;
try {
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
System.out.println("Connecting to the database...");
Connection connection = DriverManager.getConnection(
"jdbc:oracle:thin:@124.18.10.22:1521:INT9", "nitin",
"nitin");
String filePath = "D:/ishan1/R3AP201409_GMSTYXCODG_00740_20140902024214.SND";
InputStream inputStream = new FileInputStream(new File(filePath));
System.out.println("inputStream :" + inputStream.toString());
String name = "Hamilton";
String email = "fred@abc.com";
String str = inputStream.toString();
String sql = "INSERT INTO EXT_SITATEXT_ONLINE_ERROR(SERVICE_ID, USAGE_DATA, FILE_NAME_LINE_NUMBER)values (?, ?, ?)";
PreparedStatement statement = connection
.prepareStatement(sql);
try (BufferedReader br = new BufferedReader(
new FileReader(filePath))) {
for (String line; (line = br.readLine()) != null;) {
System.out.println(line);
}
}
statement.setString(1, name);
statement.setString(2, str);
statement.setString(3, email);
statement.executeUpdate();
updateQuery = statement.executeUpdate(sql);
if (updateQuery != 0) {
System.out.println("table is created successfully and "
+ updateQuery + " row is inserted.");
}
int row = statement.executeUpdate(filePath);
if (row > 0) {
System.out.println("A contact was inserted.");
}
statement.close();
connection.close();
} catch (Exception e) {
System.out.println("The exception raised is:" + e);
}
}}
在这段代码中,我想逐行读取驱动器中的文件并逐行插入oracle db。
答案 0 :(得分:0)
我认为snd是一种二进制格式,如果您打算稍后播放它 - 您应该将整个snd内容存储在blob / securefile或类似的内容中
答案 1 :(得分:0)
音频文件是二进制文件。在逐行阅读它是没有意义的。如果您确实要将音频文件存储在数据库中,可以将整个文件读入字节数组,并将其作为BINARY / BLOB数据存储在数据库中。
从byte []可以轻松创建ByteArrayInputStream(它是InputStream的子类,PreparedStatement.setBinaryStream()和PreparedStatement.setBlob()所期望的参数类型。
请参阅PreparedStatement.setBinaryStream()和PreparedStatement.setBlob(): http://docs.oracle.com/javase/7/docs/api/java/sql/PreparedStatement.html