This document contains code to connect to a database, execute a SQL query, and write the results to a CSV file. It connects to a DBF database using JDBC, executes a query to get cell IDs, builds a SQL statement to aggregate KPI data by date and cell IDs, executes the statement, writes the column names and row data to a CSV file, and closes all connections.
This document contains code to connect to a database, execute a SQL query, and write the results to a CSV file. It connects to a DBF database using JDBC, executes a query to get cell IDs, builds a SQL statement to aggregate KPI data by date and cell IDs, executes the statement, writes the column names and row data to a CSV file, and closes all connections.
{ Integer i, columnas; BufferedWriter bw = new BufferedWriter(new FileWriter("KPI_result.csv")); Class.forName("com.caigen.sql.dbf.DBFDriver"); String connString = "jdbc:dbf:/D:/2014/Tools3G/JAVA"; String l = ""; String query; Connection connection = DriverManager.getConnection(connString); String sql = "SELECT Utrancell FROM Celdas.dbf";// usual sql query Statement stmt = connection.createStatement(); ResultSet resultSet = stmt.executeQuery(sql); resultSet.next(); l = "'" + resultSet.getString(1); while (resultSet.next()) { l = l + "','" + resultSet.getString(1); } l = l + "'"; stmt.close(); connection.close(); Connection connection2 = DriverManager.getConnection(connString); Statement stmt2 = connection2.createStatement(); query = "Select DATE_ID,avg(EUL_USER_P) as EUL_User,avg(HS_USER_PE) as HS_User,sum(CORTES_PRO)/sum(INTENTOS_C) as DropCS, " + "sum(CORTES_HS_)/sum(INTENTOS_H) as DropHS,sum(CORTES_EUL)/sum(INTENTOS_E) as DropEUL,sum(CORTES_PS_)/sum(INTENTOS_P) as DropPS, " + "sum(CORTES_FAC)/sum(INTENTOS_F) as DropFACH, avg(MHT_HS_SEG) as MHT_HS, avg(MHT_EUL_SE) as MHT_Eul, avg(ACC_CS_PRO) as ACC_CS, " + "avg(ACC_HS_PRO) as ACC_HS, avg(ACC_EUL_PR) as ACC_EUL from kpi_consol.dbf " + "where Utrancell in (" + l + ") group by DATE_ID"; ResultSet rs = stmt2.executeQuery(query); columnas = rs.getMetaData().getColumnCount(); l = rs.getMetaData().getColumnName(1); for (i = 2; i <= columnas; i++) { l = l + "," + rs.getMetaData().getColumnName(i); } bw.write(l + "\n"); while (rs.next()) { l = rs.getString(1); for (i = 2; i <= columnas; i++) { l = l + "," + rs.getString(i); } bw.write(l + "\n"); } rs.close(); stmt2.close(); bw.close(); connection2.close(); }
Name: Dipak Munde Roll No: TE21261 A4: Write A Program To Insert and Retrieve The Data From The Database Using JDBC. Program: // Program To Insert Data Into Database