Results 1 to 3 of 3
  1. #1
    tudorH is offline Member
    Join Date
    Feb 2010
    Posts
    6
    Rep Power
    0

    Default Cant print the contents of a table

    Hi guys

    So a few weeks ago i posted on here about converting a text file to a sql table and i received some great help so i thought i would post again with a different problem.

    Im still adding the same text file to the table but this time i need to split the title into three parts, the title, the year and the subtitle. This is where my problem arises, when one of the records doesnt have subtitle it will not print out that record. Any idea how i can fix this. Below is my code and a fraction of my text file for reference to see the format of the file.

    Thanks in advance

    TEXTFILE EXAMPLE:
    MOVIE RATINGS REPORT

    Distribution Votes Rank Title
    0.00001223 574 8.2 Zübük (1980)
    0000000025 2956 8.6 Zügürt Aga (1982)
    1...213..1 8 5.9 Zünd an, es kommt die Feuerwehr (1978)
    ......3123 9 8.4 Züri West - Am Blues vorus (2002)
    ..2.2...24 5 3.2 [Insert Title Here] (2001) The
    0000001221 29479 7.7 [Rec] (2007)
    0000011102 979 7.0 [Rec] 2 (2001)
    .....1.511 54887 9.9 [Ybi-173] (2000)
    0..0020203 38 6.5 [eM] -eNCHANT arM- (2006) (VG)
    .0...0.511 12 6.6 _grau (2005)
    0000021210 5336 9.7 alaska.de (2009)
    1.1.110.03 2568 9.8 bgFATLdy (2000)

    CODE:
    Java Code:
    try {
          ResultSet rs;
          Statement stmt;
          String data = null;
    
          //FileInputStream fstream = new FileInputStream("C:/Users/Tudor/Documents/CMPT-354/rating.txt");
          FileInputStream fstream = new FileInputStream("C:/Users/Tudor/Documents/CMPT-354/employees.txt");
          DataInputStream dstream = new DataInputStream(fstream);
          BufferedReader bf = new BufferedReader(new InputStreamReader(dstream));
    
          //Register the JDBC driver for MySQL.
          Class.forName("com.mysql.jdbc.Driver");
    
          //Define URL of database server for database named mytable on the localhost with the default port number 3306.
          String url = "jdbc:mysql://localhost:3306/newData";
    
          //Get a connection to the database
          Connection con = DriverManager.getConnection(url,"root", "turf238");
    
          //Display URL and connection information
          //System.out.println("URL: " + url);
          //System.out.println("Connection: " + con);
    
          stmt = con.createStatement();
    
          try{
            stmt.executeUpdate("DROP TABLE myTable");
          }catch(Exception e){
            System.out.print(e);
            System.out.println("No existing table to delete");
          }
    
          //Create a table in the database named mytable
          stmt.executeUpdate("CREATE TABLE myTable(distribution char(20)," + "votes integer," + "rank float," + "title char(250)," + "year integer," + "sub char(250));");
    
    String regexp ="([0-9\\.]+)[\\s]+([0-9]+)[\\s]+([0-9]\\.[0-9])[\\s]+(.*)\\s+\\(([0-9]{4})\\)[\\s]+(.*)";
          
          Pattern pattern = Pattern.compile(regexp);
          String line;
          data= "";
    
          while ((line = bf.readLine()) != null) {
            data = line.replaceAll("'", "");
            //data = line.replaceAll("(", "");
            //data = line.replaceAll(")", "");
            Matcher matcher = pattern.matcher(data);
            if (matcher.find()) {
                String distribution = matcher.group(1);
                String votes = matcher.group(2);
                String rank = matcher.group(3);
                String title = matcher.group(4);
                String year = matcher.group(5);
                String sub = matcher.group(6);
                if(sub==null){
                    sub +="none";
                }
                String todo = ("INSERT into mytable " +
                    "(Distribution, Votes, Rank, Title, Year, Sub) "+
                    "values ('"+distribution+"', '"+votes+"', '"+rank+"', '"+title+"', "+year+", '"+sub+"');");
                int r = stmt.executeUpdate(todo);
    
    stmt.close();
          con.close();  //close the connection to the database
        }catch( Exception e ) {
          e.printStackTrace();
        }//end catch
    And the method for printing:
    Java Code:
    public static void partF(){
          try{
          ResultSet rs;
          Statement stmt;
          String data = null;
          Class.forName("com.mysql.jdbc.Driver");
          String url = "jdbc:mysql://localhost:3306/newData";
    
          Connection con = DriverManager.getConnection(url,"root", "turf238");
          stmt = con.createStatement();
          rs= stmt.executeQuery("SELECT * FROM mytable");
    
         //Use the methods of class ResultSet in a loop to display all of the data in the database
         System.out.println("The new table..");
         while(rs.next()){
            String d = rs.getString("distribution");
            String v = rs.getString("votes");
            String f = rs.getString("rank");
            String t = rs.getString("title");
            String y = rs.getString("year");
            String s = rs.getString("sub");
            System.out.println("Rank: " + f + "\tTitle: " + t+ "\tYear: " + y + "\tSub: " + s);
            }
          }catch( Exception e ) {
            e.printStackTrace();
          }
        }

  2. #2
    tudorH is offline Member
    Join Date
    Feb 2010
    Posts
    6
    Rep Power
    0

    Default

    any ideas??

  3. #3
    tugalsan is offline Member
    Join Date
    Mar 2010
    Posts
    4
    Rep Power
    0

    Default

    What is the output of the program.

    try to put an counter and print it too. lets find out whether your table has any row or not; like:
    int counter=0;
    while(rs.next()){
    System.out.println(counter++);
    String d = rs.getString("distribution");
    String v = rs.getString("votes");
    String f = rs.getString("rank");
    String t = rs.getString("title");
    String y = rs.getString("year");
    String s = rs.getString("sub");
    System.out.println("Rank: " + f + "\tTitle: " + t+ "\tYear: " + y + "\tSub: " + s);
    }

Similar Threads

  1. Replies: 0
    Last Post: 02-12-2010, 03:33 PM
  2. Print the table in struts...
    By makpandian in forum Web Frameworks
    Replies: 1
    Last Post: 09-22-2009, 06:13 PM
  3. Print table
    By Implode in forum New To Java
    Replies: 7
    Last Post: 09-12-2009, 03:43 PM
  4. how to print out lines in a table- like form?
    By helloworld in forum New To Java
    Replies: 5
    Last Post: 02-10-2009, 09:31 PM
  5. Replies: 0
    Last Post: 07-11-2008, 04:30 PM

Posting Permissions

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