Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Prepared statements - how to set up?

843785Feb 16 2009 — edited Mar 9 2009
Hello,

I'm a new to java, I am having a tough time with a database connection class that also runs a SQL query. I know i need to make it a prepared statement, but I'm not sure how to go about it? any help would be great, here is the full class code.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.JTextField;

	public class ARLinkConnection {

		Connection con;
		Statement stmt;
		ResultSet rs;
		
		public String[] getValues() {
			try {
				Class.forName("oracle.jdbc.driver.OracleDriver");
				con = DriverManager.getConnection("jdbc:oracle:thin:@athena:1581:DEV",
								"user", "password");
				stmt = con.createStatement();
				rs = stmt.executeQuery("SELECT DISTINCT "
								+ " substr(pov.vendor_name,1,35), "
								+ " TO_CHAR(PHA.APPROVED_DATE,'DD-MON-YYYY'), "
								+ " substr(rha.segment1,1,7), "
								+ " substr(requestor.full_name,1,30), "
								+ " substr(buyer.full_name,1,30), "
								+ " (PLA.UNIT_PRICE * PLA.QUANTITY), "
								+ " SUBSTR (ppa.segment1, 1, 5), "
								+ " SUBSTR (GAA.award_number, 1, 5), " 
								+ " pat.task_number, "
								+ " GAA.FUNDING_SOURCE_AWARD_NUMBER, "
								+ " SUBSTR(GLC.SEGMENT4,1,8), "
								+ " SUBSTR(PLA.ATTRIBUTE7,1,20), "  
								+ " hl.location_code, "
								+ " PLA.LINE_NUM, "
								+ " PDA.DISTRIBUTION_NUM "                                    
								+ " FROM "
								+ " po.po_headers_all                  pha, " 
								+ " po.po_vendors                      pov, "
								+ " hr.per_all_people_f                buyer, "
								+ " po.po_lines_all                    pla, "
								+ " po.po_distributions_all            pda, "
								+ " pa.pa_projects_all                 ppa, "
								+ " pa.pa_tasks                        pat, "
								+ " GL.GL_CODE_COMBINATIONS            GLC, "
								+ " gms.gms_award_distributions        GAD, "
								+ " gms.gms_awards_all                 GAA, "
								+ " po.po_req_distributions_all        rda, "
								+ " po.po_requisition_lines_all        rla, "
								+ " po.po_requisition_headers_all      rha, "
								+ " hr.per_all_people_f                requestor, "
								+ " hr.per_all_assignments_f           paa, "
								+ " apps.hr_locations                  hl "  
								+ " WHERE "
								+ " pha.segment1 = '907878'    AND "                          
								+ "  pov.vendor_id                = pha.vendor_id " 
								+ " AND buyer.person_id              = pha.agent_id " 
								+ "AND pla.po_header_id             = pha.po_header_id " 
								+ "AND pla.CANCEL_DATE              IS NULL "  
								+ "AND pda.po_line_id               = pla.po_line_id " 
								+ "AND pda.encumbered_amount        > 0 "                 
								+ "AND ppa.project_id               = pda.project_id " 
								+ "AND pat.project_id               = pda.project_id "
								+ "AND pat.task_id                  = pda.task_id "
								+ "AND glc.code_combination_id      = pda.code_combination_id " 
								+ "AND gad.po_distribution_id       = pda.po_distribution_id "
								+ "AND gad.project_id               = pda.project_id "
								+ "AND gad.task_id                  = pda.task_id "
								+ "AND gaa.award_id                 = gad.award_id "
								+ "AND rda.distribution_id      (+) = pda.req_distribution_id " 
								+ "AND rla.requisition_line_id  (+) = rda.requisition_line_id "
								+ "AND rla.cancel_date          (+) IS NULL "
								+ "AND rha.requisition_header_id(+) = rla.requisition_header_id " 
								+ "AND requestor.person_id      (+) = rla.to_person_id "
								+ "AND paa.person_id            (+) = requestor.person_id "
								+ "AND hl.location_id           (+) = paa.location_id "
								+ "ORDER BY pla.line_num");
				
				ResultSetMetaData rsmd = rs.getMetaData();
				int numberOfColumns = rsmd.getColumnCount();
				String[] values = new String[numberOfColumns];
				int rowCount = 1;
				while (rs.next()) {
					for (int i = 1; i <= numberOfColumns; i++) {
						// System.out.print(" Column " + i + ": ");
						// System.out.println(rs.getString(i));
						values[i - 1] = rs.getString(i);
					}
					rowCount++;
				}
					stmt.close();
					return values;
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				return null;
		}
	}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 6 2009
Added on Feb 16 2009
18 comments
650 views