Java - Eclipse to Excel from SQL Query - java

Getting the results of a SQL query where I want row 5 from every record that is returned.
I would like to export the data to an Excel spreadsheet in rows of thirteen, starting at the second row (I have headers).
My SQL logic is obviously off right now because I'm getting the first result thirteen times in increasing rows and columns, although it is properly starting on the second row and only going out 13 columns. The second result repeats in this fashion, as does each successive result.
I suspect my troubles start at while (rs.next()) {
for (int i = 0; i < 13; i++) {
package process;
import java.util.Scanner;
import java.io.*;
import java.sql.*;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFCell;
public class Sec_Completions {
public static void main(String[] args) {
Sec_Completions obj_Sec_Completions=new Sec_Completions();
obj_Sec_Completions.Check_Data();
}
public void Check_Data() {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the beginning date of completion YYYY/MM/DD");
String doc = scanner.nextLine();
doc = doc + " 00:00";
System.out.println("Date is" + " " + doc);
final String sql = "SELECT DISTINCT wp_grassblade_completions.user_id, wp_grassblade_completions.status, wp_grassblade_completions.content_id, wp_usermeta.meta_key, CASE WHEN meta_value = 'male' THEN 'M' WHEN meta_value = 'female' THEN 'F' WHEN meta_value = 'Louisiana' THEN 'LA' ELSE meta_value END AS '1', wp_usermeta.meta_value, wp_usermeta.user_id, wp_grassblade_completions.timestamp\r\n" +
"FROM wp_grassblade_completions \r\n" +
"INNER JOIN wp_usermeta ON wp_grassblade_completions.user_id = wp_usermeta.user_id\r\n" +
"WHERE wp_grassblade_completions.timestamp >= ? AND meta_key IN ('mepr_full_name', 'mepr_address', 'mepr_city', 'mepr_state', 'mepr_zip_code', 'mepr_home_phone_with_area_code', \r\n" +
" 'mepr_drivers_license_or_id', 'mepr_id_state', 'mepr_LAst_four_of_social_security_number', 'mepr_date_of_birth_mmddyyyy', 'mepr_sex_mf', 'mepr_height', 'mepr_weight') AND content_id IN ('1575, 642, 1580') \r\n" +
"ORDER BY wp_grassblade_completions.timestamp, content_id, wp_usermeta.user_id";
Class.forName("com.mysql.jdbc.Driver");
Connection connection=DriverManager.getConnection("jdbc:mysql://wpengine.com:3306/wp_database","user", "passsword");
PreparedStatement ps =null;
ps=connection.prepareStatement(sql);
ps.setString(1, doc);
Statement st = connection.createStatement();
ResultSet rs = ps.executeQuery();
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sheet = wb.createSheet("Security");
XSSFRow rowhead = sheet.createRow((short) 0);
int index = 1;
while (rs.next()) {
for (int i = 0; i < 13; i++) {
XSSFRow row = sheet.createRow((short) index);
row.createCell((short) i).setCellValue(rs.getString(5));
index++;
}
}
FileOutputStream fileOut = new FileOutputStream("D://OneDrive//ABSEC//ATC_Reporting//expdata1.xlsx");
wb.write(fileOut);
fileOut.close();
System.out.println("Data is saved in excel file.");
rs.close();
connection.close();
} catch (Exception e) {
System.out.println(e);
}

Edited, again
Try something like this:
// META_KEY values, in the order that the columns should be. This should be a
// private static final outside of in the class, along with the query's text...
String[] keys = { "mepr_full_name", "mepr_address", "mepr_city", "mepr_state",
"mepr_zip_code", "mepr_home_phone_with_area_code",
"mepr_drivers_license_or_id", "mepr_id_state",
"mepr_LAst_four_of_social_security_number", "mepr_date_of_birth_mmddyyyy",
"mepr_sex_mf", "mepr_height", "mepr_weight" };
// Running thru the ResultSet
short index = 1;
while ( rs.next() )
{
XSSFRow row = sheet.createRow( index );
String key = rs.getString(4); // meta_key
String value = rs.getString( 5 ); // meta_value
for ( short i = 0; i < keys.length; ++i )
{
if ( keys[i].equals( key))
{
// Retrieving cell, creating if not exists
XSSFCell cell = row.getCell( i, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK );
// if the cell is used, increment index o get to the next row
if ( cell.getCellTypeEnum() != CellType.BLANK )
++index;
// Set the value
cell.setCellValue( value );
// No need to walk thru the rest of the array...
break;
}
}
}
This only works, tho, if any "next row" does not start with a column that has no value in the previous row...
For this reason you should also add meta_key to the ORDER BY clause...
With some additional cleanup, something like this:
SELECT DISTINCT wpgc.user_id, wpgc.status, wpgc.content_id, wpu.meta_key,
CASE wpu.meta_value WHEN 'male' THEN 'M' WHEN 'female' THEN 'F'
WHEN 'Louisiana' THEN 'LA' ELSE wpu.meta_value END AS '1',
wpu.user_id, wpgc.timestamp
FROM wp_grassblade_completions AS wpgc
JOIN wp_usermeta AS wpu ON wpgc.user_id = wpu.user_id
WHERE wpgc.timestamp >= ?
AND wpu.meta_key IN ('mepr_full_name', 'mepr_address', 'mepr_city', 'mepr_state',
'mepr_zip_code', 'mepr_home_phone_with_area_code',
'mepr_drivers_license_or_id', 'mepr_id_state',
'mepr_LAst_four_of_social_security_number',
'mepr_date_of_birth_mmddyyyy', 'mepr_sex_mf', 'mepr_height',
'mepr_weight')
AND content_id IN ('1575, 642, 1580')
ORDER BY wpgc.timestamp, wpgc.content_id, wpu.user_id, wpu.meta_key

Related

Unable to delete particular rows and empty string rows in xlsx spreadsheet using Apache POI

I have this spreadsheet
I would like to iterate all the rows containing "USED" keywords and empty strings (spaces) , save all row numbers that match those criteria to an ArrayList , then loop my spreadsheet and remove all the row numbers listed in my ArrayList.
I tried and modified some solutions from Stackoverflow (one of them from here) . My codes look like this
// Remove "USED" and empty string from spreadsheet
private static void cleanUpSheet(String sheetName, String pathToExcel) throws IOException, InvalidFormatException {
FileInputStream fileInput = new FileInputStream(pathToExcel);
Workbook workbook = WorkbookFactory.create(fileInput);
List<Integer> toRemove = new ArrayList<>();
int index = workbook.getSheetIndex(sheetName);
if (index == -1)
System.out.println("No sheet Found");
else {
System.out.println("Index " + index);
Sheet sheet = workbook.getSheetAt(index);
Iterator<Row> rowItr = sheet.iterator();
DataFormatter formatter = new DataFormatter();
System.out.println("Total ROW " + sheet.getLastRowNum());
while (rowItr.hasNext()) {
Row row = rowItr.next();
if (containsValue(row, row.getFirstCellNum(), row.getLastCellNum())) {
//Row contains value
System.out.println("Value Cell 0 " + formatter.formatCellValue(row.getCell(0)));
if (formatter.formatCellValue(row.getCell(0)).equals("USED")) {
System.out.println("Value cell 0 should contain USED " + formatter.formatCellValue(row.getCell(0)));
toRemove.add(row.getRowNum());
System.out.println("What I am removing "+ row.getCell(4).getRichStringCellValue());
}
continue;
} else {
//Row does not contain value
System.out.println("Empty row " + formatter.formatCellValue(row.getCell(0)));
toRemove.add(row.getRowNum());
System.out.println("What I am removing "+ row.getCell(4).getRichStringCellValue());
}
}
for (int i = 0 ; i<toRemove.size() ; i++) {
System.out.println("To Remove " + toRemove.get(i));
removeRow(pathToExcel,toRemove.get(i),false,sheet);
}
System.out.println("New Total Row " + sheet.getLastRowNum());
}
fileInput.close();
FileOutputStream outFile = new FileOutputStream(new File(pathToExcel));
workbook.write(outFile);
outFile.close();
}
private static void removeRow(String pathToExcel, int rowToBeRemoved, boolean removeIndexOneOnly, Sheet sheet) throws IOException, InvalidFormatException {
if (removeIndexOneOnly == true) rowToBeRemoved = 1;
int lastRowNum = sheet.getLastRowNum();
Row row = sheet.getRow(rowToBeRemoved);
if (row != null && rowToBeRemoved != lastRowNum) {
//System.out.println(row.getCell(3).getRichStringCellValue()); //For Testing
//System.out.println(row.getCell(4).getRichStringCellValue()); //For Testing
System.out.println("row to be removed " + rowToBeRemoved);
System.out.println("last row " + lastRowNum);
sheet.removeRow(row);
sheet.shiftRows(rowToBeRemoved + 1, lastRowNum, -1);
}
if (rowToBeRemoved == lastRowNum) {
System.out.println("Very Last Row");
Row removingRow = sheet.getRow(rowToBeRemoved);
if (removingRow != null) {
sheet.removeRow(removingRow);
}
}
}
The result looks like this
When I run in debug mode , I can see that my List<Integer> toRemove does list the correct row numbers I want to remove (i.e. containing "USED" and empty spaces). So far so good.
But when I execute this part of codes
for (int i = 0 ; i<toRemove.size() ; i++) {
System.out.println("To Remove " + toRemove.get(i));
removeRow(pathToExcel,toRemove.get(i),false,sheet);
}
It just doesn't remove all rows listed in List<Integer> toRemove.
I am using POI 3.17.
I am wondering where could I do wrongly.
Thanks.

ERRORS while writing in excel - Java poi

I've excel sheet of only two columns. The user add y himself and the third one is for checking if the other two columns are valid to insert in he database or not.
While I'm trying to write in the third column after checking the processing going good for couple of rows but if there are more rows, the results for the third column for each row is not correct...
I think the problem here in the for loop if anyone can help it would be really appreciated. Thanks.
here's my code:- ...
#CheckMethodAuthority("PFV2300&limt_print")
public static void importExcel(Upload upload_data) throws IOException,
NoSuchFieldException, SecurityException, IllegalArgumentException,
IllegalAccessException,
ParseException {
String fileName = upload_data.getFileName();
logger.info("H1:" + fileName);
final int expectedSheetIndex = 0; //
Sheet sheet = ExcelUtil.openWorkSheet(new ByteArrayInputStream(upload_data.asBytes()), fileName, expectedSheetIndex);
SYS4000 account = (SYS4000)renderArgs.get("account");
StringBuffer fileUploadErrorMsg = new StringBuffer();
final int expectedTitleRowNum = 1; //
List<POS20083> pos20083List = readAllCellsOfSheet(sheet, expectedTitleRowNum, account.user_no, fileUploadErrorMsg);
int addItem =0;
int upItem =0;
int failItem =0;
File filePath = new File("D:\\play framework workspace\\Coupon_Platform\\public\\sample\\excel\\PFV2300-sample.xlsx");
InputStream file = new FileInputStream(filePath);
XSSFWorkbook WB = new XSSFWorkbook(file);
sheet = WB.getSheetAt(0);
for (POS20083 POS20083: pos20083List)
{
for (int i=0; i<=pos20083List.size(); i++)
{
Row row = sheet.getRow(i);
Cell cell_store_no= (sheet.getRow(i).getCell(0));
Cell cell_control_qty = (sheet.getRow(i).getCell(1));
Cell cell_error_msg = sheet.getRow(i).getCell(2);
sheet.autoSizeColumn(2);
for (int j=0; j<=sheet.getFirstRowNum(); j++)
{
if (row == null)
{
row = sheet.createRow(i);
continue;
}
if (cell_error_msg == null)
{
cell_error_msg = sheet.getRow(i).createCell(2);
continue;
}
if (row.getRowNum()==0)
{
continue;
}
POS20083 pos20083 = POS20083.find("merchant_no= ? and store_no= ?",POS20083.merchant_no, POS20083.store_no).first();
String sql = insert into pos20083 (pos20081_id, merchant_no, store_no, control_qty, id)select 60, ?1, ?2, ?3, pos20083_seq.nextval from dual;
String sql2 = " Select count(store_no) from twc_store"
+ " Where exists (select store_no"
+ " from pos20083"
+ " where twc_store.store_no = ?4)";
String sql3 = " Select count(store_no) from pos20083"
+ " Where exists (select store_no"
+ " from twc_store"
+ " where pos20083.store_no = ?5)";
EntityManager entityManager = JPA.em();
EntityTransaction transaction = JPA.em().getTransaction();
Query query = entityManager.createNativeQuery(sql);
Query query2 = entityManager.createNativeQuery(sql2);
Query query3 = entityManager.createNativeQuery(sql3);
query2.setParameter(4, POS20083.store_no);
query3.setParameter(5, POS20083.store_no);
if (!transaction.isActive())
{
transaction.begin();
}
int insertCount2 = query2.executeUpdate();
int insertCount3 = query3.executeUpdate();
int storeCounts = ObjectUtil.getInteger(query2.getSingleResult());
int storeCounts_pos20083 = ObjectUtil.getInteger(query3.getSingleResult());
if (storeCounts == 0 && ObjectUtil.getInteger(cell_control_qty.getNumericCellValue()) >0)
{
cell_error_msg.setCellValue("Store Number ID doesn't exists in [twc_store]");
sheet.autoSizeColumn(2);
}
if (storeCounts == 0 && ObjectUtil.getInteger(cell_control_qty.getNumericCellValue()) <=0)
{
cell_error_msg.setCellValue("Store Number ID doesn't exists in [twc_store] and and control Quantity coudn't be less or equal ZERO");
sheet.autoSizeColumn(2);
}
if (storeCounts == 1 && storeCounts_pos20083 ==0 && ObjectUtil.getInteger(cell_control_qty.getNumericCellValue()) > 0)
{
query.setParameter(1, POS20083.merchant_no);
query.setParameter(2, POS20083.store_no);
query.setParameter(3, POS20083.control_qty);
int insertCount = query.executeUpdate();
addItem++;
cell_error_msg.setCellValue(" ");
sheet.autoSizeColumn(2);
}
if (transaction.isActive())
{
transaction.commit();
}
file.close();
if (storeCounts == 1 && storeCounts_pos20083 ==0 && ObjectUtil.getInteger(cell_control_qty.getNumericCellValue()) <=0)
{
cell_error_msg.setCellValue("control Quantity coudnot be less or equal ZERO");
sheet.autoSizeColumn(2);
//failItem++;
//file.close();
}
if (storeCounts == 1 && storeCounts_pos20083 ==1 && ObjectUtil.getInteger(cell_control_qty.getNumericCellValue()) <=0)
{
cell_error_msg.setCellValue("Store Number ID already exists and control Quantity coudn't be less or equal ZERO");
sheet.autoSizeColumn(2);
}
if (storeCounts == 1 && storeCounts_pos20083 ==1 && ObjectUtil.getInteger(cell_control_qty.getNumericCellValue()) > 0)
{
cell_error_msg.setCellValue("Store Number ID already exists");
sheet.autoSizeColumn(2);
}
}
}
failItem++;
}
file.close();
FileOutputStream fileout = new FileOutputStream(filePath);
WB.write(fileout);
fileout.flush();
fileout.close();
I've found out the bug and i fixed it
there are was three loops
1- for (POS20083 POS20083: pos20083List)
2- for (int i=0; i<=pos20083List.size(); i++)
3- for (int j=0; j<=sheet.getFirstRowNum(); j++)
so if we remove the first one which isn't useful here because we are getting the data always from the excel sheet so it will works good ...
also the same for the third one too as well its not very useful here in my task too as well ...
so what i need is only the this loop
for (int i=0; i<=pos20083List.size(); i++)
and we can replace the pos20083list which was return the real number of row with this line
int rows = sheet.getPhisicalNumberofRows() - 1
Thanks so much Layne Bernardo for your efforts

Insert to mysql with undefined number of column

I have a question about insert query of java.
I have read data from excel file, and I want to use that data to import to MySQL, if excel file has 3 columns, I can use
String sql="INSERT INTO tablename(column1,column2,colum3) value(...)
But if excel file doesn't have defined the number of column (for example excel_1.xlsx have 3 columns, excel_2.xlsx have 4 columns).
How can I use insert query? Can anybody give me any suggestion?
You can do something like this:
FileInputStream excelFile = new FileInputStream(new File("yourExcelsheet.xsls"));
Workbook workbook = new XSSFWorkbook(excelFile);
Sheet datatypeSheet = workbook.getSheetAt(0);
Iterator<Row> iterator = datatypeSheet.iterator();
//int columnCount = 4;
String columnArray[] = new String[]{"column1","column2","column3","column4","column5","column6"};
//iterate row-wise
while (iterator.hasNext()) {
String sql="INSERT INTO tablename";
String columnNames = "";
String valueString = "";
String value = "";
int i=0;
Row currentRow = iterator.next();
Iterator<Cell> cellIterator = currentRow.iterator();
//iterate column-wise
while (cellIterator.hasNext()) {
Cell currentCell = cellIterator.next();
value = currentCell.getStringCellValue();
if(i != 0)
{
columnNames += ",";
valueString += ",";
}
columnNames += columnArray[i] ;
valueString += value;
i++;
}
sql += "(" + columnNames + ") values (" + valueString + ");";
System.out.println(sql);
}
OUTPUT:
INSERT INTO tablename(column1,column2,column3,column4) values
(5,5,5,5);

Saving ResultSet to use as input for writing file

I'm trying to save the ResultSet of a DB query into an ArrayList to be used as an input to write to a file but I run out of memory. There is about 116k rows of data in the table I'm reading. I've tried writing directly to the file inside the readDB() method and it creates a CSV-file of about 3.3 MB. This is my code.
public class Main {
public static void main(String[] args) {
DBAccess dbAccess = new DBAccess();
}
}
The class and method to read the DB.
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class DBAccess {
public List<String> readDB() {
String url = "jdbc:Cobol://Dev/Project Files/DatAndCpyFiles";
try (Connection con = DriverManager.getConnection(url, "", "");
Statement stmt = con.createStatement())
{
stmt.setFetchSize(10);
Class.forName("com.hxtt.sql.cobol.CobolDriver").newInstance();
String sql = "select * from PROFS";
ResultSet rs = stmt.executeQuery(sql);
List<String> result = new ArrayList<>();
ResultSetMetaData resultSetMetaData = rs.getMetaData();
int iNumCols = resultSetMetaData.getColumnCount();
for (int i = 1; i <= iNumCols; i++) {
result.add(resultSetMetaData.getColumnLabel(i) + ";");
}
String row;
while (rs.next()) {
for (int i = 1; i <= iNumCols; i++) {
row = rs.getString(i);
row = row == null ? " " : row.replaceAll("\u0086", "å");
row = row == null ? " " : row.replaceAll("\u008F", "Å");
row = row == null ? " " : row.replaceAll("\u0084", "ä");
row = row == null ? " " : row.replaceAll("\u008E", "Ä");
row = row == null ? " " : row.replaceAll("\u0094", "ö");
row = row == null ? " " : row.replaceAll("\u0099", "Ö");
row = row == null ? " " : row.replaceAll("\u0081", "ü");
row = row == null ? " " : row.replaceAll("\u009A", "Ü");
row = row == null ? " " : row.replaceAll("\u0082", "é");
row = row == null ? " " : row.replaceAll("\u0090", "É");
result.add(result + row.trim() + ";");
}
}
rs.close();
System.out.println("Returning result");
return result;
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
return null;
}
}
}
Error message
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Arrays.java:3332)
at java.lang.AbstractStringBuilder.ensureCapacityInternal(AbstractStringBuilder.java:124)
at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:448)
at java.lang.StringBuilder.append(StringBuilder.java:136)
at java.lang.StringBuilder.append(StringBuilder.java:131)
at java.util.AbstractCollection.toString(AbstractCollection.java:462)
at java.lang.String.valueOf(String.java:2994)
at java.lang.StringBuilder.append(StringBuilder.java:131)
at se.spedweb.DBAccess.readDB(DBAccess.java:56)
at se.spedweb.Main.main(Main.java:15)
What other data structure can I for this? Am I using the wrong approach for this? I could just append to a file instead of add to list in the method but I thought it would be cleaner to have writing to file and reading the database in separate classes, maybe this is wrong.
If you run java with the command line option try using -Xmx, you can manually set the size of the heap. Try setting it to -Xmx2048m.
Note: there's ways to do it if your using an IDE, a quick google will show you how to change it

Can't print output through loop, don't know how to print second table

Edit: anything would help. If anyone can tell me the process at least I'd appreciate it.
I'm trying to get my code to read for an access database that I made. In that database are 2 tables, one is Soccer_Team and the other is Soccer_Players. I'm having an issue figuring out how to have the field name show up in front of the actual data. When I run the code now I get this:
Name: Location: Home Stadium: FC Barcelona Spain Camp Nou
Name: Location: Home Stadium: FC Bayern Munich Germany Allianz
I want the "Name:" to be followed by the club name, and so on... I am not familiar with Java so I am a little confused here.
Also, I want to print the second table from my database through an SQL query, that has it display the above but with the player information printed under each row... I don't even know where to begin doing that. I'm not sure I'm explaining this well, so sorry if I'm confusing people.
Sorry if this is asking too much but I am pretty lost... Thanks for any help guys.
package msjavaaccessdb;
import java.sql.*;
import java.util.*;
public class MSjavaAccessDB {
/** Creates a new instance of databaseApplication */
public MSjavaAccessDB() {
}
/**
* #param args the command line arguments
*/
static String nameOfJdbcOdbcDriver =
"sun.jdbc.odbc.JdbcOdbcDriver";
// static String dataBaseNameDSN = "jdbc:odbc:myDataSource";
static String dataBaseNameDSN = "jdbc:odbc:myDataSource";
static String userName = "";
static String passwordForUser = "";
static Connection myConnectionRequest = null;
static Statement myStatementObject = null;
static ResultSet myResultTuples = null;
static ResultSetMetaData myResultTuplesMetaData = null;
static String queryToBeExecuted = "select * from Soccer_Team";
public static void main(String args[])
throws ClassNotFoundException {
try {
//Identify the driver to use
Class.forName(nameOfJdbcOdbcDriver);
//Attempt a connection to database...
Connection myConnectionRequest =
DriverManager.getConnection(
dataBaseNameDSN, userName, passwordForUser);
//Create a statement object, use its method to execute query
Statement myStatementObject =
myConnectionRequest.createStatement();
//Use statement object method to execute a query.
//Hold results in a resutl set...like a cursor
ResultSet myResultTuples = myStatementObject.executeQuery
(queryToBeExecuted);
//Call metadata to get the number of attributes
myResultTuplesMetaData = myResultTuples.getMetaData();
int numberOfAttributes =
myResultTuplesMetaData.getColumnCount();
System.out.println(Integer.toString(numberOfAttributes));
//For each row in result set, print ALL columns
for(int rowNum = 1; myResultTuples.next(); rowNum++) {
for (int i = 1; i <= numberOfAttributes; i++) {
if ( (i != 1) ) System.out.print (
myResultTuples.getString(i) + "\t");
else {//String xyz = myResultTuples.getString(1);
int xyz = Integer.parseInt( myResultTuples.getString(1));
System.out.print ("Name: ");
System.out.print ("Location: ");
System.out.print ("Home Stadium: ");
}
}
System.out.println("\n\n");
}
} // end of try block
//handle ALL exceptions to above database calls
catch (SQLException sqlError) {
System.out.println("Unexpected exception : " +
sqlError.toString() + ", sqlstate = " +
sqlError.getSQLState());
sqlError.printStackTrace();
}
} // end of main method of this class
} // end of the class
So I don't know how your datatable looks, but what i guess you want to do is print the column label and then the corresponding data value of the row.
for(int rowNum = 1; myResultTuples.next(); rowNum++) {
for (int i = 1; i <= numberOfAttributes; i++) {
//print column label
System.out.print(myResultTuplesMetaData.getColumnLabel(i) + ": ");
//print data value
System.out.print(myResultTuples.getString(i) + "\t");
}
System.out.println("\n\n");
//process SQl-Query about players and print out results in another for loop HERE
}
So for the second table you would have to do sth like SELECT * FROM table2 WHERE team IS (?) as a PreparedStatement and print out the results in another loop.
I can't get the error right now because I don't have access to NetBeans at this moment. I can update later when I am on my home computer. I'm not sure if what I did makes sense, because I don't exactly know what the rules of Java are... Hope this isn't stroke inducing.
package msjavaaccessdb;
import java.sql.*;
import java.util.*;
public class MSjavaAccessDB {
/** Creates a new instance of databaseApplication */
public MSjavaAccessDB() {
}
/**
* #param args the command line arguments
*/
static String nameOfJdbcOdbcDriver =
"sun.jdbc.odbc.JdbcOdbcDriver";
static String dataBaseNameDSN = "jdbc:odbc:myDataSource";
static String dataBaseNameDSN = "jdbc:odbc:myDataSource";
static String userName = "";
static String passwordForUser = "";
static Connection myConnectionRequest = null;
static Statement myStatementObject = null;
static ResultSet myResultTuples = null;
static ResultSetMetaData myResultTuplesMetaData = null;
static ResultSet ResultTuples = null;
static ResultSetMetaData ResultTuplesMetaData = null;
static String queryToBeExecuted = "select * from Soccer_Team";
static string secondQuery = "select * from Soccer_Player where team is (?)";
public static void main(String args[])
throws ClassNotFoundException {
try {
//Identify the driver to use
Class.forName(nameOfJdbcOdbcDriver);
//Attempt a connection to database...
Connection myConnectionRequest =
DriverManager.getConnection(
dataBaseNameDSN, userName, passwordForUser);
//Create a statement object, use its method to execute query
Statement myStatementObject =
myConnectionRequest.createStatement();
//Use statement object method to execute a query.
//Hold results in a resutl set...like a cursor
ResultSet myResultTuples = myStatementObject.executeQuery
(queryToBeExecuted);
ResultSet ResultTuples = myStatementObject.executeQuery
(secondQuery);
//Call metadata to get the number of attributes
myResultTuplesMetaData = myResultTuples.getMetaData();
int numberOfAttributes =
myResultTuplesMetaData.getColumnCount();
System.out.println(Integer.toString(numberOfAttributes));
ResultTuplesMetaData = ResultTuples.getMetaData();
int numOfAttributes =
ResultTuplesMetaData.getColumnCount();
System.out.println(Integer.toString(numOfAttributes));
//For each row in result set, print ALL columns
for(int rowNum = 1; myResultTuples.next(); rowNum++) {
for (int i = 1; i <= numberOfAttributes; i++) {
//print column label
System.out.print(myResultTuplesMetaData.getColumnLabel(i) + ": ");
//print data value
System.out.print(myResultTuples.getString(i) + "\t");
}
System.out.println("\n\n");
}
for(int rowNum = 1; ResultTuples.next(); rowNum++) {
for (int i = 1; i <= numOfAttributes; i++) {
//print column label
System.out.print(ResultTuplesMetaData.getColumnLabel(i) + ": ");
//print data value
System.out.print(ResultTuples.getString(i) + "\t");
}
System.out.println("\n\n");
}
} // end of try block
//handle ALL exceptions to above database calls
catch (SQLException sqlError) {
System.out.println("Unexpected exception : " +
sqlError.toString() + ", sqlstate = " +
sqlError.getSQLState());
sqlError.printStackTrace();
}
} // end of main method of this class
} // end of the class

Categories