我想将.dat文件转换为人类可读的.txt文件,然后应将其插入数据库中。 我尝试转换.dat文件,但到目前为止没有成功。
示例:
[original .dat file][1]
输出应如下所示:
612,*,Familienpension Sonnenhof,Fesser Noana,Panhans Anna-Irina,Sonnenwg 3,Lackenhof,3295,7728963,4,18
SQL类如下:
public static void addMasterData() {
//ObservableList Klasse damit der DatenbankListener darauf Veraenderungen registriert
ObservableList<Hotel> hotelsData = null;
try {
Statement st = Main.connection.createStatement();
hotelsData = FXCollections.observableArrayList(); // Hier auch Observable fuer Datenbanklistener
//Set in der die Abfrageergebnisse der Tabelle gespeichert werden
ResultSet rs = st.executeQuery("SELECT * FROM hotel");
// 4. Process the ResultSet by scrolling the cursor forward via next().
while (rs.next()) {
Hotel hotel = new Hotel(
rs.getInt(1),
rs.getString(3),
rs.getString(2),
rs.getString(4),
rs.getString(5),
rs.getString(6),
rs.getString(7),
rs.getString(8),
rs.getString(9),
rs.getInt(10),
rs.getInt(11)
);
hotelsData.add(hotel);
}
} catch (SQLException ex) {
// 5. Connection und Statement schließen, automatisch mit try-with-resources (JDK 7)
JOptionPane.showMessageDialog(null, ex);
}
try {
//Hotel in the hotel Tabelle einfuegen
String query = "insert into hotel ("
+ "hotel_id, "
+ "name, "
+ "category, "
+ "owner, "
+ "contact, "
+ "address, "
+ "city, "
+ "city_code, "
+ "phone, "
+ "rooms, "
+ "beds)"
+ " values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
File file = new File("src/touristoffice/ui/hotel.txt");
BufferedReader br = new BufferedReader(new FileReader(file));
String line = "";
// Ursprungsdatei Einlesen
while ((line = br.readLine()) != null) { //solange Datenbankeinträge vorhanden sind
String data[] = line.split(",");
Statement statement = Main.connection.createStatement();
PreparedStatement preparedStmt = Main.connection.prepareStatement(query);
preparedStmt.setInt(1, Integer.parseInt(data[0]));
preparedStmt.setString(2, data[2]);
preparedStmt.setString(3, data[1]);
preparedStmt.setString(4, data[3]);
preparedStmt.setString(5, data[4]);
preparedStmt.setString(6, data[5]);
preparedStmt.setString(7, data[6]);
preparedStmt.setString(8, data[7]);
preparedStmt.setString(9, data[8]);
preparedStmt.setInt(10, Integer.parseInt(data[9]));
preparedStmt.setInt(11, Integer.parseInt(data[10]));
// preparedstatement ausführen
preparedStmt.execute();
}````
If you have an idea, please let me know, thanks!
Best Regards,
Thomas