我有一個 JAVA 代碼可以將 SQL Server 表輸出到 CSV。打印在中間停止,我只得到部分數據作為 CSV 輸出。在我的代碼下面。public class SQLServerConnection { public static void main(String[] args) throws IOException, SQLException { // TODO Auto-generated method stub Connection conn = null; SQLConnection cnn = new SQLConnection(); FileWriter fw = new FileWriter(cnn.fileName); try {conn = DriverManager.getConnection(cnn.dbURL); if (conn != null) { DatabaseMetaData dm = (DatabaseMetaData) conn.getMetaData(); } } catch (SQLException ex) { ex.printStackTrace(); } String sql = String.format(cnn.Query); assert conn != null; PreparedStatement preStatement; preStatement = conn.prepareStatement(sql); ResultSet result = preStatement.executeQuery(); ResultSetMetaData rmsd = result.getMetaData(); int Columncount = rmsd.getColumnCount(); //Get the column name and print the column name for (int iterator=1; iterator<= Columncount; iterator++) { fw.append(rmsd.getColumnName(iterator)+","); } fw.append('\n'); while(result.next()){ try { for (int jterator=1; jterator<=Columncount; jterator++){ fw.append(result.getString(jterator)); fw.append(','); } fw.append('\n'); } catch (SQLException e) { e.printStackTrace(); }} conn.close(); }}DB 和 Excel 參數的類public class SQLConnection { public String ServerName = "Server1"; public String DBName = "DB1"; public String FileLoc = "Location of the file"; public String dbURL = "jdbc:sqlserver://"+ServerName+";databaseName="+DBName+";integratedSecurity=true"; public String Query = "SELECT * from [scdHSBCHK]"; public String fileName = (FileLoc + DBName +"_QueryResult.csv"); }實際的 db 返回 57 條記錄,但 csv 僅返回 29 條。我嘗試使用不同的 db 名稱以及相同的問題。當我在程序窗口中輸出結果時,數據顯示正確。
添加回答
舉報
0/150
提交
取消