我想通过使用java将sqlite db导出/保存到另一个空间(例如:C、D、anywhere)......我希望有人能帮我解决这个问题..视图可能是使用java swing做一个界面这个程序有一个 sqlite 数据库,我们有一个按钮(另存为)可以按下并且可以将此数据库保存到另一个空间我找不到很多资源来查找,所以我希望有人可以帮助我完成这个程序。我会很高兴并谢谢你。
问问题
1649 次
2 回答
0
SQLite 允许您访问ATTACH
另一个数据库文件(可以位于任何地方)。然后,您只需使用 SQL 语句将内容复制到. 这是你想要的东西。
ATTACH DATABASE '/the/path/to/the/other/db.sqlite' AS otherdb;
CREATE TABLE otherdb.theTableName (...);
INSERT INTO otherdb.theTableName SELECT * FROM main.theTableName;
或者,在没有打开连接的情况下,只需复制数据库文件。
于 2013-04-08T10:39:25.543 回答
0
try{
filename = null;
HSSFWorkbook hwb=new HSSFWorkbook();
HSSFSheet sheet = hwb.createSheet("new sheet");
HSSFRow rowhead = sheet.createRow((short)0);
rowhead.createCell((short) 0).setCellValue("Invoice No");
rowhead.createCell((short) 1).setCellValue("Date");
rowhead.createCell((short) 2).setCellValue("Customer");
rowhead.createCell((short) 3).setCellValue("Reference No.");
rowhead.createCell((short) 4).setCellValue("Qty");
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/animal1", "root", "315921");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("Select * from few1");
int i=1;
while(rs.next()){
HSSFRow row= sheet.createRow((short)i);
row.createCell((short) 0).setCellValue(rs.getString("Invoice"));
row.createCell((short) 1).setCellValue(rs.getString("Date"));
row.createCell((short) 2).setCellValue(rs.getString("Customer"));
row.createCell((short) 3).setCellValue(rs.getString("Reference"));
row.createCell((short) 4).setCellValue(rs.getString("Qty"));
i++;
}
JFileChooser fileChooser = new JFileChooser();
int retval = fileChooser.showSaveDialog(null);
if (retval == JFileChooser.APPROVE_OPTION) {
filename = fileChooser.getSelectedFile();
if (!filename.getName().toLowerCase().endsWith(".xls")) {
filename = new File(filename.getParentFile(), filename.getName() + ".xls");
}
}
FileOutputStream fileOut = new FileOutputStream(filename);
hwb.write(fileOut);
fileOut.close();
JOptionPane.showMessageDialog(this, "Your excel file has been generated!");
} catch ( IOException | ClassNotFoundException | SQLException ex ) {
System.out.println(ex);
}
它用于在 Microsoft excel 中 Java 导出 MySQL 数据。
于 2018-09-08T13:22:36.813 回答