Results 1 to 4 of 4
  1. #1
    montesjm1242 is offline Member
    Join Date
    May 2013
    Posts
    1
    Rep Power
    0

    Default need help with Database class

    I've created a database class to be used for assignment after assignment. I am supposed to take import.sql out of this class completely. So I need to get rid of the Result Sets and Connections in this method. I also need to figure out a way to call rs.next in the database class as well as the rs.getstring in the database class. Any ideas. I am awful at java.


    // Writes employees in array to input7
    public static void writeEmployee() {

    String sql = "Select EmployeeId, GenderAbb, JobTypeId from Employee";
    int index = 0;
    String outputLine;
    String outputp;
    String employID = "EmployeeID";
    String genderA = "GenderAbb";
    String jobT = "JobTypeId";

    String genderJob[][] = new String[maxEmployee][2];


    for (int i = 0; i < maxEmployee; i++) {
    for (int j = 0; j < 2; j++) {
    genderJob[i][j] = " ";
    }
    }
    try {

    if (!isConnected()) {
    Connect();
    }

    Statement stmt = connection.createStatement();
    rs = stmt.executeQuery(sql);


    try {

    File outputFile = new File("C:/temp/report7.txt");
    PrintWriter PW = new PrintWriter(outputFile);

    PW.println("Employee Gender and Job Description Report");
    PW.println(" ");
    PW.println("Employee" + " " + "ID" + "\t" + "Gender" + "\t\t" + "Job" + " " + "Type");
    PW.println(" ");

    //needs work

    while (rs.next()) {

    index = Integer.parseInt(rs.getString("EmployeeID"));
    genderJob[index][0] = rs.getString("GenderAbb");
    genderJob[index][1] = rs.getString("JobTypeId");
    }

    for (int p = 1; p < maxEmployee; p++) {
    if (p <= 0000 || p >= 9999) {
    outputp = "0000";
    } else if (p <= 9 && p >= 1) {
    outputp = "000" + p;
    } else if (p <= 99 && p >= 10) {
    outputp = "00" + p;
    } else if (p <= 999 && p >= 100) {
    outputp = "0" + p;
    } else {
    outputp = Integer.toString(p);
    }
    outputLine = outputp;
    for (int n = 0; n < 2; n++) {
    outputLine = outputLine + "\t\t" + genderJob[p][n];
    }
    PW.println(outputLine);
    }
    PW.close();

    } catch (Exception e) {

    JOptionPane.showMessageDialog(null, "2nd try Error");
    System.exit(-1);
    }
    }

    catch (Exception z) {
    }
    }

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: need help with Database class

    I am supposed to take import.sql out of this class completely. So I need to get rid of the Result Sets and Connections in this method
    What is 'import.sql'? Do you mean 'import java.sql.*? How would you communicate without using JDBC?

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default Re: need help with Database class

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: need help with Database class

    Move all the database interaction out of this method/class.
    Write a DataAccessObject class (DAO) that has a method that returns your genderJob[][].
    That method will hold all the database interaction stuff.
    Call that from this code to get the array you want to print out.

    Essentially, whoever told you to do this is saying "you are doing too much in this method".
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Create registration form | insert into database | call the database
    By FamilyGuy in forum JavaServer Pages (JSP) and JSTL
    Replies: 3
    Last Post: 04-23-2012, 11:08 AM
  2. Embedded Database : SQLite or Derby Database
    By vishnubrett in forum NetBeans
    Replies: 3
    Last Post: 03-26-2012, 10:50 AM
  3. Give Value to other class to check database
    By dread_arisawa in forum New To Java
    Replies: 1
    Last Post: 06-23-2010, 09:52 PM
  4. Replies: 0
    Last Post: 04-27-2010, 12:15 PM
  5. Replies: 2
    Last Post: 02-11-2009, 04:43 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •