Thread problem...i think


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Thread problem...i think

  1. #1
    Join Date
    Apr 2005
    Posts
    7

    Thread problem...i think

    Code:
    import java.util.*;
    import java.io.*;
    import java.sql.*;
    
    public class Seznam implements Serializable{
    
    private TreeMap tr = new TreeMap();
    private BancniRacun trenutni;
    private Collection vrednosti;
    private Iterator i;
    
    public void dodaj(BancniRacun b) {
    	
    tr.put( String.valueOf( b.getId() ), b );
    }
    
    public void odvzemi(int id){
    	
    	tr.remove( String.valueOf( id ) );
    	
    	}
    	
    public void popravi( int id, BancniRacun b ){
    	
    	tr.put( String.valueOf( id ), b );
    	
    	}
    	
    public BancniRacun najdiBancniRacun(String pin){
    	
    	vrednosti = tr.values();
    	
    	i = vrednosti.iterator();
    	
    		while ( i.hasNext() ){
    			
    			BancniRacun temp = (BancniRacun)i.next();
    			
    			if( temp.getPin().equals(pin) ){
    				setBancniRacun( temp );
    				return temp;
    				
    				}
    			
    			}
    			
    		return null;
    
    	
    }
    
    public TreeMap vrniSeznam(){
    	
    	return tr;
    	
    	}
    	
    public void setBancniRacun(BancniRacun b){
    	trenutni = b;
    }
    
    public void shrani() throws Exception{
    	
    	
    	Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    	Connection con = DriverManager.getConnection("jdbc:odbc:VAJA9");
    	Statement stmt = con.createStatement();
    	stmt.executeUpdate("DELETE * FROM BANCNI_RACUN");
    	while( i.hasNext() ){
    		
    				BancniRacun t = (BancniRacun)i.next();
    				
    				int id = t.getId();
    				String pin = t.getPin();
    				int stanje = t.getStanje();
    				String ime = "bla";
    
    				
    				stmt.executeUpdate("INSERT INTO BANCNI_RACUN VALUES (id, ime, pin, stanje);");
    				con.commit();
    	}
    	
    	con.close();
    
    	
    }
    	
    public void nalozi() throws Exception{
    	
    	Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    	Connection con = DriverManager.getConnection("jdbc:odbc:VAJA9");
    	Statement stmt = con.createStatement();
    	ResultSet rs = stmt.executeQuery("SELECT * FROM BANCNI_RACUN");
    	while (rs.next()){
    		int id = rs.getInt("ID");
    		String ime = rs.getString("IME");
    		String pin = rs.getString("PIN");
    		int stanje = rs.getInt("STANJE");
    		
    		BancniRacun temp = new BancniRacun( id, ime, pin, stanje );
    		dodaj( temp );
    		
    		}
    	con.close();
    }
    	
    
    
    public BancniRacun getBancniRacun(){ return trenutni;}
    
    }
    i have a problem with writting into database...i think, the problem is, because the program, "can't" read the variable and send its value to the database at the same time...when using strings in quotes like, there are no problems...

    here is the printstack error, when i call the critical function...

    java.lang.NullPointerException
    at Seznam.shrani(Seznam.java:69)
    at Razporejevalnik$1.windowClosing(Razporejevalnik.java:72)
    at java.awt.Window.processWindowEvent(Unknown Source)
    at java.awt.Window.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)

    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)

    how could i solve the problem?

    thanks in advance...

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    Well, with all respect for your language, I dont understand a squak of your named
    elements and methods but I see some strange stuff;
    1: you do a delete and then you seemingly want to iterate through a java.sql.Resultset.
    There is none when you do a delete (at least I've never detected it). All you get is
    an int saying how many rows that was affected.
    2: the iterator is not assigned a value in the shrani -method, -> nullpointer

    And, for the case of select you use the resultset like:

    ResultSet rs=stmt.executeQuery("SELECT * FROM BANCNI_RACUN");
    while (rs.next()) { // note ! initially you are positioned at row -1 (e.g. nowhere)
    // process rows
    }
    eschew obfuscation

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center