Results 1 to 2 of 2
  1. #1
    stamv is offline Member
    Join Date
    Jan 2009
    Posts
    1
    Rep Power
    0

    Smile read txt file,with some records, create objects and store objects in tables of a db.

    hello friends.


    I am new in programming.

    I have an exercise with DB and Java.

    I want to read a textfile with some records ,create odjects and then put the objects in 3 tables ( I create these 3 tables with NETBEANS 6.5, and are empty ). I know how to connect to DB with a driver and DB URL.

    I put the code of DBConnector.java and OldFileReader.java(include main method )


    My application OldFileReader run but my tables donot store anything, still
    empty!!!!!!!!!!!!!!!!!

    I have create 3 class for Customer,Stock and Trans with get and set methods.

    Please help.

    thanks, stamvs.




    OLDFILEREADER.JAVA.
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.sql.Connection;
    import java.sql.SQLException;
    import java.util.NoSuchElementException;
    import java.util.Scanner;

    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */

    /**
    *
    * @author stamv
    */
    public class OldDBFileReader {
    private static Connection conn;

    DBConnector apps=new DBConnector();
    private Scanner input;

    // επιτρέπει στον χρήστη να ανοίξει το αρχείο.
    public void openFile() throws FileNotFoundException {
    try{
    input=new Scanner(new File("oldData.txt"));
    }
    catch (FileNotFoundException fileNotFound){
    System.err.println("error opening file.");
    System.exit(1);
    }
    } // τέλος της μεθόδου openFile.


    // διαβάζει τις εγγραφές από το αρχείο.

    public void readStockRecords() throws NoSuchElementException, SQLException, Exception {

    Stock s=new Stock();

    while (input.hasNext())



    try { // διαβάζει εγγραφές από το αρχείο χρησιμοποιώντας το αντικείμενο Scanner.



    s.setId(input.nextInt());
    s.setSymbol(input.next());
    s.setLast_price(input.nextDouble());
    apps.addStock(s);

    }

    catch (NoSuchElementException elementException) {
    System.err.println("File improperly formed.");


    continue;
    }


    }

    public void readCustomerRecords()throws NoSuchElementException, SQLException, Exception {
    Customer c=new Customer ();


    while(input.hasNext())

    try{
    c.setId(input.nextInt());
    c.setName(input.next());
    c.setSurName(input.next());
    c.setAddress(input.next());
    c.setCash_allowance(input.nextDouble());

    apps.addCustomer(c);}

    catch (NoSuchElementException elementException) {
    System.err.println("File improperly formed.");
    continue;
    }
    }


    public void readTransRecords()throws NoSuchElementException, SQLException, Exception {
    Trans tr=new Trans();

    while(input.hasNext())

    try{
    tr.setId(input.nextInt());
    tr.setCustomer_id(input.nextInt());
    tr.setDate(input.next());
    tr.setStock_Id(input.nextInt());
    tr.setQuantity(input.nextInt());
    tr.setStock_price(input.nextDouble());

    apps.addTrans(tr);



    }
    catch (NoSuchElementException elementException) {
    System.err.println("File improperly formed.");

    continue;

    }

    }



    public void closeFile() {
    if (input!=null)
    input.close();
    }


    public static void main(String[] args) throws NoSuchElementException, SQLException, Exception {

    OldDBFileReader application=new OldDBFileReader();



    }

    }





    DBCONNECTOR.JAVA

    import java.sql.Statement;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    /*
    * @author stamv
    */
    public class DBConnector {

    static final String JDBC_DRIVER="sun.jdbc.odbc.JdbcOdbcDriver";
    static final String DATABASE_URL="jdbc:derby://localhost:1527/contact";
    Connection conn=null;
    Statement stmt=null;
    static {
    try {
    Class.forName(JDBC_DRIVER);

    }
    catch (ClassNotFoundException ex) {
    System.err.println(" not found sun.jdbc.odbc.JdbcOdbcDriver ");
    }
    }








    public void init() throws SQLException {
    try
    {
    conn=DriverManager.getConnection(DATABASE_URL,"nbu ser","nbuser");
    }
    catch(SQLException sql) {
    System.err.println(" not found DATABASE_URL ");
    }
    }





    public void addCustomer(Customer c) throws SQLException, Exception {
    try
    {

    stmt= conn.createStatement();
    String q="INSERT INTO NBUSER.CUSTOMER(ID,NAME,SURNAME,ADDRESS,INIT_CASH_ ALLOWANCE)VALUES("+
    (c.getId()) + ","+ c.getName() +"," +c.getSurname()+ "," + c.getAddress()+ "," +
    c.getCash_allowance()+ ")";
    stmt.execute(q);

    }
    catch (SQLException e) {

    if (stmt!=null)
    stmt.close();

    System.err.println("SQLException:" + e.getMessage());

    }

    catch (Exception e) {
    System.err.println("Exception:" + e.getMessage());
    }
    } // τέλος της μεθόδου addCustomer.




    public void addStock(Stock s) throws SQLException, Exception {
    try
    {
    Statement stmt1;
    stmt1= conn.createStatement();
    String q1="INSERT INTO NBUSER.STOCK(ID,SYMBOL,LAST_PRICE) VALUES ("+
    s.getId() + ","+ s.getSymbol() +"," +s.getLast_price()+ ")";
    stmt1.execute(q1);
    }

    catch (SQLException e) {

    if (stmt!=null)
    stmt.close();

    System.err.println("SQLException:" + e.getMessage());

    }

    catch (Exception e) {
    System.err.println("Exception:" + e.getMessage());
    }
    } // τέλος της μεθόδου addStock.





    public void addTrans(Trans tr) throws SQLException,Exception {
    try
    {
    Statement stmt2;
    stmt2= conn.createStatement();
    String q2="INSERT INTO NBUSER.TRANS (ID,CUSTOMER_ID,DATE,STOCK_ID,QUANTITY,STOCK_PRICE ) VALUES ("+ tr.getId() + ","+ tr.getCustomer_id() +","+ tr.getDate()+","+tr.getStock_Id()+","+tr.getQuanti ty()+ "," +tr.getStock_price()+ ")";
    stmt2.execute(q2);
    }


    catch (SQLException e) {

    if (stmt!=null)
    stmt.close();

    System.err.println("SQLException:" + e.getMessage());

    }



    catch (Exception e) {
    System.err.println("Exception:" + e.getMessage());
    }
    // τέλος της μεθόδου addTrans.

    }

    public void closeConnection(Connection conn) throws Exception {
    try{
    conn.close();
    }

    catch (Exception e) {
    System.err.println("Exception:" + e.getMessage());
    }

    }
    }// τέλος της κλάσης DBConnector.



    OLDDATA.TXT

    STOCK: 1 ΕΤΕ 22.15
    STOCK: 2 ALPHA 13.22
    STOCK: 3 EUROBANK 12.52
    CUSTOMER: 1 Γεώργιος Παπαδόπουλος "Θεμιστοκλή 13, 11234 Αθήνα" 10000.00
    TRANS: 221 1 05/01/2001 1 100 35.11
    TRANS: 321 1 04/05/2002 1 100 28.18
    CUSTOMER: 2 Μάριος Θεριστόπουλος "Αβράμη 15, 18167 Αθήνα" 22000.00
    STOCK: 4 ΚΟΡΡΕΣ 6.52
    TRANS: 504 1 18/07/2002 1 -50 32.19
    STOCK: 5 ΕΛΠΕ 20.96
    STOCK: 6 ΑΑΑΚ 18.01
    TRANS: 742 2 12/09/2002 5 40 16.25
    TRANS: 782 2 12/11/2002 6 550 10.11
    CUSTOMER: 3 Νικόλαος Αβραμίδης "Μητροπόλεως 75-77, 11367 Αθήνα" 6000.00
    TRANS: 799 3 22/11/2002 2 70 22.19
    TRANS: 916 2 20/09/2003 3 40 20.19
    TRANS: 1054 1 18/07/2003 1 -50 32.19
    TRANS: 1214 1 18/08/2003 5 250 21.19
    TRANS: 1311 3 22/01/2004 3 190 17.91
    TRANS: 1320 3 22/01/2004 1 100 32.45

  2. #2
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    You comments are all Greek to me ;-)

    I don't see anything obviously wrong with your code, although using PreparedStatment's with parameters would cause the application to run faster, even with the small number of records.

    Are you sure all the sections of code are executing?

    Are you seeing error messages?

    If you are using an IDE, step through the application in debug.

    If not, add a System.out.println() statement at the beginning of each method and each loop to print a message "In xxx.", which will help you see what the program is doing.

    If you are running this from a command line, be sure to use the java command and not javaw, so you can see the output.

Similar Threads

  1. Need a solution to read and store data from a file
    By sheetalnri in forum New To Java
    Replies: 10
    Last Post: 09-30-2010, 07:43 AM
  2. Replies: 5
    Last Post: 07-11-2008, 04:26 AM
  3. Can I store multiple objects in an array
    By lareauk in forum New To Java
    Replies: 9
    Last Post: 05-29-2008, 04:57 AM
  4. Can I use vectors to store multiple types of objects
    By Nathand in forum Advanced Java
    Replies: 6
    Last Post: 04-28-2008, 08:55 AM

Posting Permissions

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