Results 1 to 2 of 2

Thread: Need some help!

  1. #1
    Join Date
    Apr 2012
    Posts
    1

    Unanswered: Need some help!

    Ok so I have been having some problems with some coding and have attempted several different sites, professors, and friends, but I can't seem to ever get in the same room with my Professor and coding, and my friends are a little lost. I'm trying to make a test database, a very simple one, that I will use as the base for a much larger project. But now, I am just getting some really dumb errors (please ignore the spelling on one of my classes names). The GUI works fine, the SQLs work fine to the best of my knowledge, but when I try to combine them, it fails miserably. Here is the coding for the Database Class. I have currently two different methods that seem the most promising... one involving apache and entitled the reader method, the other is getConnection. Please and thank you!

    import org.apache.poi.hssf.usermodel.HSSFCell;
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    import org.apache.poi.hssf.usermodel.HSSFSheet;
    import org.apache.poi.hssf.usermodel.HSSFRow;
    import org.apache.poi.hssf.usermodel.HSSFRow;

    import java.sql.*;
    import java.io.*;
    import java.util.*;


    public class DatabaseCreater {
    Statement stat;
    Connection conn;


    public DatabaseCreater() {
    conn = dbConnect(null, null, null);
    try {
    stat = conn.createStatement();

    }
    catch (SQLException e) {
    e.printStackTrace();
    }
    }

    // 3rd attempt at getting a connection.
    public Connection dbConnect(String dbConnectString,String dbUserid, String dbPassword)
    {
    try
    {
    Properties props = new Properties();
    FileInputStream in = new FileInputStream("//files1.depauw.edu/student/2013/Database_Test/StudentInfoExcel.xlsx");
    props.load(in);
    in.close();
    System.out.println("connected");
    return DriverManager.getConnection(dbConnectString, dbUserid, dbPassword);
    }
    catch (Exception e)
    {
    e.printStackTrace();
    return null;
    }
    }

    // trying to create my table in a seperate method to see where I am screwing up
    public void createTables(Connection conn)
    {
    try
    {
    stat.execute("CREATE TABLE STUDENT (" +
    " StudentNumber Int NOT NULL IDENTITY (1,1)" +
    " FirstName String NULL" +
    " LastName String NULL" +
    " Gender String NULL" +
    " Housing String NULL)");
    }
    catch (SQLException e) {
    e.printStackTrace();
    }
    }

    // this method will print everything in the database
    public void printAll() throws SQLException{
    stat.execute("SELECT * FROM STUDENT");
    }

    // this method will print just one column from the database
    public void printSpec(int x) throws SQLException{
    if(x == 1)
    stat.execute("SELECT FirstName FROM STUDENT");
    else if(x == 2)
    stat.execute("SELECT LastName FROM STUDENT");
    else if(x == 3)
    stat.execute("SELECT Gender FROM STUDENT");
    else
    stat.execute("SELECT Housing FROM STUDENT");
    }


    // this method should be able to take in information and put it into the database
    public void insertRow(String firstName, String lastName, String gender,
    String housing) throws SQLException {
    stat.execute("INSERT INTO Student VALUES (firstName, lastName, gender, housing)");

    }

    /*public static void reader() {
    //String filename = "//files1.depauw.edu/student/2013/Database Test/StudentInfoExcel.xlsx";
    List sheetData = new ArrayList();
    FileInputStream fis = null;
    try {
    fis = new FileInputStream(filename);
    HSSFWorkbook workbook = new HSSFWorkbook(fis);
    HSSFSheet sheet = workbook.getSheetAt(0);
    Iterator rows = sheet.rowIterator();
    while (rows.hasNext()) {
    HSSFRow row = (HSSFRow) rows.next();
    Iterator cells = row.cellIterator();
    List data = new ArrayList();
    while (cells.hasNext()) {
    HSSFCell cell = (HSSFCell) cells.next();
    data.add(cell);
    }
    sheetData.add(data);
    }
    }
    catch (IOException e) {
    e.printStackTrace();
    }
    finally {
    if (fis != null) {
    fis.close();
    }

    showExelData(sheetData);
    }
    }

    private static void showExelData(List sheetData) {
    for (int i = 0; i < sheetData.size(); i++) {
    List list = (List) sheetData.get(i);
    for (int j = 0; j < list.size(); j++) {
    HSSFCell cell = (HSSFCell) list.get(j);
    System.out.print(
    cell.getRichStringCellValue().getString());
    if (j < list.size() - 1) {
    System.out.print(", ");
    }
    }
    System.out.println("");
    }
    }
    */
    // this method SHOULD be able to edit current information in the database and change it
    public void editSpec(int x, int stID, String editedItem) throws SQLException{
    if(x == 1)
    stat.execute("UPDATE STUDENT SET FirstName = editedItem WHERE StudentNumber = stID");
    else if(x == 2)
    stat.execute("UPDATE STUDENT SET LastName = editedItem WHERE StudentNumber = stID");
    else if(x == 3)
    stat.execute("UPDATE STUDENT SET Gender = editedItem WHERE StudentNumber = stID");
    else
    stat.execute("UPDATE STUDENT SET Housing = editedItem WHERE StudentNumber = stID");
    }
    }

  2. #2
    Join Date
    Apr 2006
    Location
    Belgium
    Posts
    2,514
    Provided Answers: 11
    so can you publish the error you get ..
    Best Regards, Guy Przytula
    Database Software Consultant
    Good DBAs are not formed in a week or a month. They are created little by little, day by day. Protracted and patient effort is needed to develop good DBAs.
    Spoon feeding : To treat (another) in a way that discourages independent thought or action, as by overindulgence.
    DB2 UDB LUW Certified V7-V8-V9-V9.7-V10.1-V10.5 DB Admin - Advanced DBA -Dprop..
    Information Server Datastage Certified
    http://www.infocura.be

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
  •