Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Error in Java when attempting to insert data in database

Posted on 2011-04-18
56
Medium Priority
?
405 Views
Last Modified: 2012-05-11
The attached code throws the errors below when I run it in Eclipse, I want to add a line to a single table database, any suggestions please?

errors -

Exception in thread "main" java.lang.NullPointerException
      at database.Insert.insertRow(Insert.java:60)
      at database.Insert.<init>(Insert.java:36)
      at database.Insert.main(Insert.java:28)
package database;

import java.sql.*;

public class Insert {
	
	static Connection link;
	static Statement statement;
	static ResultSet results;
	
	public static void main(String[] args){
		
		try{
			//Step 1
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
			//Step 2
			link = DriverManager.getConnection("jdbc:odbc:BookShelf","","");
		}
		catch(ClassNotFoundException e){
			System.out.println("Unable to load driver");
			System.exit(1);
		}
		catch(SQLException e){
			System.out.println("Cannot connect to the database");
			System.exit(1);
		}
	
		new Insert();
		
	} //End main
	
	public Insert()
	{
		try
		{
			insertRow(11, "BookTest", "Peter Andre", "Publish", 2000, 20.0);
		}
		catch (SQLException e)
		{
			System.out.println("Message: " + e.getMessage());
			System.out.println("SQL State: " + e.getSQLState());
			System.out.println("SQL Error Code: " + e.getErrorCode());
			e.printStackTrace();
			System.exit(1);
		}
	
	}
	
	public void insertRow(int ID, String Title, String Author, String Publisher, int Year, double Cost) throws SQLException
	  {
		  String insert = "INSERT INTO BookShelf"
              + " VALUES (" + ID   + ", " 
                            + "'" + Title + "', "
                            + "'" + Author   + "','"
                            + Publisher + "', " + Year +"," +  Cost   + ")";


   
	       
	       int result = statement.executeUpdate(insert);
	       if (result == 0)
	       { 
	    	  System.out.println("Failed to insert the following data: " 
	    			         + ID   + "," 
                             + Title + ","
                             + Author   + ","
                             + Publisher   + ","
                             + Year   + ","
                             + Cost);
	  }
	
	
	
	
	

	  }
}

Open in new window

0
Comment
Question by:t38
  • 31
  • 18
  • 6
  • +1
56 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 35418970
>>int result = statement.executeUpdate(insert);

The Statement is not initialized:

statement = link.createStatement();

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
ID: 35418978
I suggest that uyou print exactly the same string as you insert
before update operation and then try to do insert of the same for the SQL command prompt
Do you have aacses to SQL command prompt?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35418993
Yes, in this case CEHJ is right this issue is becuase uou need to create statement,
nevertheless what I wrote above is a very good pracitice
when debugging DB activity
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 86

Expert Comment

by:CEHJ
ID: 35419034
You should probably use a PreparedStatement - it will save you all the bother of quoting and will be less error-prone
0
 

Author Comment

by:t38
ID: 35419116
thanks for the info, where exactly should i insert statement = link.createStatement(); ?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35419134


Just after you create connection:

link = DriverManager.getConnection("jdbc:odbc:BookShelf","","");

 statement = link.createStatement();

0
 
LVL 47

Expert Comment

by:for_yan
ID: 35419215
Everyone is recommending PreparedStatement, and I do use them especially when
a lot of similar updates need to be done, and PreparedStatemnet should be faster in this case.

However, in my personal observations I very rarely noticed increase of efficiency
comparing PreparedStatement with regular updates. My guess is that I'm working exclusively with Oracle
and Oracle does this caching for similar SQL by itself anyway, that's why I don't see
time efficiency of PreparedStatement

One reason I don't use PreparedStatement all the time is exactly what I wrote above - as far as I know,
PreapredStatemnt does not have a simple method to print prepared SQL string
before you execute it. And this is a very important thing, especially if you work
without setparate testing environemnt and need to do your
debugging on real or almost real database.
In my opinion, the best thing is just make up this insert string
like you do it, and then cut and paste contents of the parenthseies exactly the same
string to System.out.println() - and pruint it out.
System.out.println(..) should of course  go before the actual update line
(otherwise you'll not see it when there is an error).
Actually when I first write the program in the first run I only  leave System.out...
and comment out actual update line - that will save you
possibility to do siomething weird with your database while debugging the initial
version of your code (quite reall possibility in many cases - sya, if you forget
the clause in the update statement)
 


0
 

Author Comment

by:t38
ID: 35419218
Thanks, it's still not populating the database though.  I'm not getting an error, when I press Run in Eclipse it compiles but does nothing?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35419235

I sggest that you follow what I wrote above - use the same insertString to print right before
the update operation - you'll see if you get to that point and how the insert line looks
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35419247

Does nothing - ?
Does it print what it should print when it failed to update?
and no exception?
0
 

Author Comment

by:t38
ID: 35419258
sorry for_yan i don't follow what you mean, i'm relatively new to eclipse, could you get me some more detail please?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35419267

You say that db is not updated fter the run.
But when you run you should see someconsole output or error message - do you see it?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35419280

This is with samll mod that I gsuggested - run this one and lets see if we get printout

package database;

import java.sql.*;

public class Insert {
	
	static Connection link;
	static Statement statement;
	static ResultSet results;
	
	public static void main(String[] args){
		
		try{
			//Step 1
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
			//Step 2
			link = DriverManager.getConnection("jdbc:odbc:BookShelf","","");
		}
		catch(ClassNotFoundException e){
			System.out.println("Unable to load driver");
			System.exit(1);
		}
		catch(SQLException e){
			System.out.println("Cannot connect to the database");
			System.exit(1);
		}
	
		new Insert();
		
	} //End main
	
	public Insert()
	{
		try
		{
			insertRow(11, "BookTest", "Peter Andre", "Publish", 2000, 20.0);
		}
		catch (SQLException e)
		{
			System.out.println("Message: " + e.getMessage());
			System.out.println("SQL State: " + e.getSQLState());
			System.out.println("SQL Error Code: " + e.getErrorCode());
			e.printStackTrace();
			System.exit(1);
		}
	
	}
	
	public void insertRow(int ID, String Title, String Author, String Publisher, int Year, double Cost) throws SQLException
	  {
		  String insert = "INSERT INTO BookShelf"
              + " VALUES (" + ID   + ", " 
                            + "'" + Title + "', "
                            + "'" + Author   + "','"
                            + Publisher + "', " + Year +"," +  Cost   + ")";

  System.out.println(insert);
   
	       
	       int result = statement.executeUpdate(insert);
	       if (result == 0)
	       { 
	    	  System.out.println("Failed to insert the following data: " 
	    			         + ID   + "," 
                             + Title + ","
                             + Author   + ","
                             + Publisher   + ","
                             + Year   + ","
                             + Cost);
	  }
	
	
	
	
	

	  }
}

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
ID: 35419291


In the above code I just added
System.out.println(insert);
   
             befiore this:

             int result = statement.executeUpdate(insert);

So we should see, if we reach that place , and if we do, what
kind of insert tsring we hacve
0
 

Author Comment

by:t38
ID: 35419293
no, that's the problem it literally does nothing, it seems to compile properly i.e. no errors but when i check the database nothing has been added!
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35419299
Sorry , i copied your orioginal, so you need this creatConnection to add again
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35419305
OK, so add

System.out.println(insert);

as I suggested above and run again and let us know what you see
0
 

Author Comment

by:t38
ID: 35419315
here's the error with the 'new' code -

INSERT INTO BookShelf VALUES (11, 'BookTest', 'Peter Andre','Publish', 2000,20.0)
Exception in thread "main" java.lang.NullPointerException
      at database.Insert.insertRow(Insert.java:60)
      at database.Insert.<init>(Insert.java:36)
      at database.Insert.main(Insert.java:28)
0
 

Author Comment

by:t38
ID: 35419319
sorry, didn't see your entry, hold on a sec and i'll run it again..
0
 

Author Comment

by:t38
ID: 35419338
this is the output to the console - -

INSERT INTO BookShelf VALUES (11, 'BookTest', 'Peter Andre','Publish', 2000,20.0)
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35419352
Even after you added the

System.out.println(insert) ;

line, as I wrote above - ?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35419359
Please, post the whole code again.
Maybe yyiou still don't have createStatement() line?
0
 

Author Comment

by:t38
ID: 35419370
yes, here's the code copied from eclipse
package database;

import java.sql.*;

public class Insert {
	
	static Connection link;
	static Statement statement;
	static ResultSet results;
	
	public static void main(String[] args){
		
		try{
			//Step 1
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
			//Step 2
			link = DriverManager.getConnection("jdbc:odbc:BookShelf","","");
			statement = link.createStatement();
		}
		catch(ClassNotFoundException e){
			System.out.println("Unable to load driver");
			System.exit(1);
		}
		catch(SQLException e){
			System.out.println("Cannot connect to the database");
			System.exit(1);
		}
	
		new Insert();
		
	} //End main
	
	public Insert()
	{
		try
		{
			insertRow(11, "BookTest", "Peter Andre", "Publish", 2000, 20.0);
		}
		catch (SQLException e)
		{
			System.out.println("Message: " + e.getMessage());
			System.out.println("SQL State: " + e.getSQLState());
			System.out.println("SQL Error Code: " + e.getErrorCode());
			e.printStackTrace();
			System.exit(1);
		}
	
	}
	
	public void insertRow(int ID, String Title, String Author, String Publisher, int Year, double Cost) throws SQLException
	  {
		  String insert = "INSERT INTO BookShelf"
              + " VALUES (" + ID   + ", " 
                            + "'" + Title + "', "
                            + "'" + Author   + "','"
                            + Publisher + "', " + Year +"," +  Cost   + ")";

  System.out.println(insert);
   
	       
	       int result = statement.executeUpdate(insert);
	       if (result == 0)
	       { 
	    	  System.out.println("Failed to insert the following data: " 
	    			         + ID   + "," 
                             + Title + ","
                             + Author   + ","
                             + Publisher   + ","
                             + Year   + ","
                             + Cost);
	  }
	
	
	
	
	

	  }
}

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
ID: 35419372
Actually printout is OK, but it looks like ststemnent is still not created
0
 

Author Comment

by:t38
ID: 35419405
so what should i do?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35419412
Remove static from the beginning
(it probably would not help, but its kind of more normal)

      Connection link;
       Statement statement;
       ResultSet results;


and then
add
  System.out.println("statn: " + statement);
after this:

link = DriverManager.getConnection("jdbc:odbc:BookShelf","","");
                  statement = link.createStatement();


and also after
Syatem.out.println(insert);

so we'll check if ataement is not null in either case








0
 
LVL 47

Expert Comment

by:for_yan
ID: 35419433
maybe it cannot connect to the databse?

You may also print
System.out.pruintln("linl: " + link);

in the first part after you create connection (agfter ..getConnection)
0
 

Author Comment

by:t38
ID: 35419450
ok thanks again, i won't be able to try this for about 15 minutes but will report back shortly..
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35419568

I suggest that you try first this much less convoluted code whicvh should do the same:
(save your variant somewhere and  then run this one)


package database;

import java.sql.*;

public class Insert {

	static Connection link;
	static Statement statement;
	static ResultSet results;

	public static void main(String[] args){

         new Insert();
    }

      public Insert() {



		try{
			//Step 1
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
			//Step 2
			link = DriverManager.getConnection("jdbc:odbc:BookShelf","","");
			statement = link.createStatement();
int ID=11;

String Title =  "BookTest";

String Author =  "Peter Andre";
String Publisher =  "Publish";
double Year = 2000;
double Cost = 20.0;

		  String insert = "INSERT INTO BookShelf"
              + " VALUES (" + ID   + ", "
                            + "'" + Title + "', "
                            + "'" + Author   + "','"
                            + Publisher + "', " + Year +"," +  Cost   + ")";

  System.out.println(insert);


	       int result = statement.executeUpdate(insert);

  System.out.println("iserted " + result + " records");

    link.close();

		}catch(Exception ex){
     System.out.println("Error " + ex.toString();
     ex.printStackTrace();

    }
System.exit(0);
}

}

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
ID: 35419610
Sorry, this is better:

package database;

import java.sql.*;

public class Insert {

	 Connection link;
	 Statement statement;
	 ResultSet results;

	public static void main(String[] args){

         new Insert();
    }

      public Insert() {



		try{
			//Step 1
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
			//Step 2
			link = DriverManager.getConnection("jdbc:odbc:BookShelf","","");
			statement = link.createStatement();
int ID=11;

String Title =  "BookTest";

String Author =  "Peter Andre";
String Publisher =  "Publish";
double Year = 2000;
double Cost = 20.0;

		  String insert = "INSERT INTO BookShelf"
              + " VALUES (" + ID   + ", "
                            + "'" + Title + "', "
                            + "'" + Author   + "','"
                            + Publisher + "', " + Year +"," +  Cost   + ")";

  System.out.println(insert);


	       int result = statement.executeUpdate(insert);

  System.out.println("iserted " + result + " records");

    link.close();

		}catch(Exception ex){
     System.out.println("Error " + ex.toString());
     ex.printStackTrace();

    }
System.exit(0);
}

}

Open in new window

0
 

Author Comment

by:t38
ID: 35419737
ok here's what prints to the console -

linl: sun.jdbc.odbc.JdbcOdbcConnection@de6f34
statn: sun.jdbc.odbc.JdbcOdbcStatement@e09713
INSERT INTO BookShelf VALUES (11, 'BookTest', 'Peter Andre','Publish', 2000,20.0)
statn: sun.jdbc.odbc.JdbcOdbcStatement@e09713

i've also attached the revised code to ensure i've put it in properly
linl: sun.jdbc.odbc.JdbcOdbcConnection@de6f34
statn: sun.jdbc.odbc.JdbcOdbcStatement@e09713
INSERT INTO BookShelf VALUES (11, 'BookTest', 'Peter Andre','Publish', 2000,20.0)
statn: sun.jdbc.odbc.JdbcOdbcStatement@e09713

Open in new window

0
 

Author Comment

by:t38
ID: 35419742
sorry here;s the code
package database;

import java.sql.*;

public class Insert {
	
	static Connection link;
	static Statement statement;
	static ResultSet results;
	
	public static void main(String[] args){
		
		try{
			//Step 1
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
			//Step 2
			link = DriverManager.getConnection("jdbc:odbc:BookShelf","","");
			statement = link.createStatement();
			
			System.out.println("linl: " + link);


			
			 System.out.println("statn: " + statement);

		}
		catch(ClassNotFoundException e){
			System.out.println("Unable to load driver");
			System.exit(1);
		}
		catch(SQLException e){
			System.out.println("Cannot connect to the database");
			System.exit(1);
		}
	
		new Insert();
		
	} //End main
	
	public Insert()
	{
		try
		{
			
			insertRow(11, "BookTest", "Peter Andre", "Publish", 2000, 20.0);
			
		}
		catch (SQLException e)
		{
			System.out.println("Message: " + e.getMessage());
			System.out.println("SQL State: " + e.getSQLState());
			System.out.println("SQL Error Code: " + e.getErrorCode());
			e.printStackTrace();
			System.exit(1);
		}
	
	}
	
	
	
	
	
	
	public void insertRow(int ID, String Title, String Author, String Publisher, int Year, double Cost) throws SQLException
	  {
		  String insert = "INSERT INTO BookShelf"
              + " VALUES (" + ID   + ", " 
                            + "'" + Title + "', "
                            + "'" + Author   + "','"
                            + Publisher + "', " + Year +"," +  Cost   + ")";

  System.out.println(insert);
  
  System.out.println("statn: " + statement);

   
	       
	       int result = statement.executeUpdate(insert);
	       if (result == 0)
	       { 
	    	  System.out.println("Failed to insert the following data: " 
	    			         + ID   + "," 
                             + Title + ","
                             + Author   + ","
                             + Publisher   + ","
                             + Year   + ","
                             + Cost);
	  }
	
	
	
	
	

	  }
}

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
ID: 35419757
All this output looks ok, and there is no error - are you sure it does not insert in database?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35419768
Id it still did not insert, it is strange.
If indeed it still didn't insert, then
I'd still suggest that you try the code which
I sent you while you were in the break - the second variant
0
 

Author Comment

by:t38
ID: 35419779
ok so your suggested code worked, what's the difference that's making it function??
0
 

Author Comment

by:t38
ID: 35419792
apart from the obvious answer that you know what you're doing of course..!!  i'd really like to know why my version won't work though.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35419798
Are you sure that your code which made that good output wihout any errors ddid not insert into databse?

it didn't peint any exceptio, it didn't print your outpout if(ressult == 0) how could it not work?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35419800
You need to close the Statement and Connection
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35419805
It is suffiecient to close connection, but it is nit somethinfg which would explain any issue
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35419813
Please, check - when you executed last time your variant it should have been worked

Change the book name and execvute it again - thesre was no error - it should have worked
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35419826
>>it didn't peint any exceptio, it didn't print your outpout if(ressult == 0) how could it not work?

by not closing the Connection
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35419840

Yest it is cgood practce to do it, but normally it works for me even I forget to close connection
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35419848
Well, myabe Orcale is different - yes it is yuseful to close connections
0
 

Author Comment

by:t38
ID: 35419872
my code is definitely updating the database...
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35419880
Great!
0
 

Author Comment

by:t38
ID: 35419888
no sorry, that should have had a NOT in it, it has been a long day, it's not updating using my code..  this is very frustrating
0
 
LVL 47

Accepted Solution

by:
for_yan earned 2000 total points
ID: 35419903
Did you add link.close(); after result = ...  ?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35419909
Add the close, and change Book title, so that it would be easy to check.
It should update
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35419950
This should be the general pattern (and using a PreparedStatement so that an author name like O'Leary won't make the app fall over)
import java.sql.*;


public class Insert {
    Connection link;
    PreparedStatement ps;
    ResultSet results;

    public void insert(int ID, String Title, String Author, String Publisher, int Year, double Cost) {
	try {
	    //Step 1
	    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
	    //Step 2
	    link = DriverManager.getConnection("jdbc:odbc:BookShelf", "", "");
	    doRow(ID, Title, Author, Publisher, Year, Cost);
	} catch (ClassNotFoundException e) {
	    e.printStackTrace();
	} catch (SQLException e) {
	    e.printStackTrace();
	}
	finally {
	    try {
		ps.close();
	    }
	    catch(SQLException e) {
		/* ignore */
	    }
	    try {
		link.close();
	    }
	    catch(SQLException e) {
		/* ignore */
	    }
	}
    }

    public void doRow(int ID, String Title, String Author,
	    String Publisher, int Year, double Cost) throws SQLException {
	String insert = "INSERT INTO Bookshelf VALUES(?, ?, ?, ?, ?, ?)";
	ps = link.prepareStatement(insert);
	ps.setInt(1, ID);
	ps.setString(2, Title);
	ps.setString(3, Author);
	ps.setString(4, Publisher);
	ps.setInt(5, Year);
	ps.setDouble(6, Cost);

	int result = ps.executeUpdate(insert);

	if (result == 0) {
	    System.out.println("Failed to insert the following data: " + ID +
		    "," + Title + "," + Author + "," + Publisher + "," + Year +
		    "," + Cost);
	}
    }

    public static void main(String[] args) {	
	Insert i = new Insert();
	i.insert(11, "BookTest", "Peter Andre", "Publish", 2000, 20.0);
    }
}

Open in new window

0
 

Author Comment

by:t38
ID: 35419951
ok, i've added link.close(); but even before i did so i re-ran the code and it produced an error - if copied and pasted it some many times i don't know if i've made a mistake or not but it looks ok, here's the error -

linl: sun.jdbc.odbc.JdbcOdbcConnection@de6f34
statn: sun.jdbc.odbc.JdbcOdbcStatement@e09713
INSERT INTO BookShelf VALUES (11, 'BookTest', 'Peter Andre','Publish', 2000,20.0)
statn: sun.jdbc.odbc.JdbcOdbcStatement@e09713
Message: General error
SQL State: S1000
SQL Error Code: 0
java.sql.SQLException: General error
      at sun.jdbc.odbc.JdbcOdbc.createSQLException(Unknown Source)
      at sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source)
      at sun.jdbc.odbc.JdbcOdbc.SQLExecDirect(Unknown Source)
      at sun.jdbc.odbc.JdbcOdbcStatement.execute(Unknown Source)
      at sun.jdbc.odbc.JdbcOdbcStatement.executeUpdate(Unknown Source)
      at database.Insert.insertRow(Insert.java:74)
      at database.Insert.<init>(Insert.java:45)
      at database.Insert.main(Insert.java:36)
package database;

import java.sql.*;

public class Insert {
	
	static Connection link;
	static Statement statement;
	static ResultSet results;
	
	public static void main(String[] args){
		
		try{
			//Step 1
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
			//Step 2
			link = DriverManager.getConnection("jdbc:odbc:BookShelf","","");
			statement = link.createStatement();
			
			System.out.println("linl: " + link);


			
			 System.out.println("statn: " + statement);

		}
		catch(ClassNotFoundException e){
			System.out.println("Unable to load driver");
			System.exit(1);
		}
		catch(SQLException e){
			System.out.println("Cannot connect to the database");
			System.exit(1);
		}
	
		new Insert();
		
	} //End main
	
	public Insert()
	{
		try
		{
			
			insertRow(11, "BookTest", "Peter Andre", "Publish", 2000, 20.0);
			
		}
		catch (SQLException e)
		{
			System.out.println("Message: " + e.getMessage());
			System.out.println("SQL State: " + e.getSQLState());
			System.out.println("SQL Error Code: " + e.getErrorCode());
			e.printStackTrace();
			System.exit(1);
		}
	
	}
		
	
	public void insertRow(int ID, String Title, String Author, String Publisher, int Year, double Cost) throws SQLException
	  {
		  String insert = "INSERT INTO BookShelf"
              + " VALUES (" + ID   + ", " 
                            + "'" + Title + "', "
                            + "'" + Author   + "','"
                            + Publisher + "', " + Year +"," +  Cost   + ")";

  System.out.println(insert);
  
  System.out.println("statn: " + statement);

   
	       
	       int result = statement.executeUpdate(insert);
	       if (result == 0)
	       { 
	    	  System.out.println("Failed to insert the following data: " 
	    			         + ID   + "," 
                             + Title + ","
                             + Author   + ","
                             + Publisher   + ","
                             + Year   + ","
                             + Cost);
	  }
	
	link.close();
	
	
	

	  }
}

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
ID: 35419974

Well, I suggest that you run several times,
till there is some consistent result.

If you don't chaneg anyhting it should run the same.
If you risrt time ran - there was not error, and second time there was error -
it is something wrong with the hardawre perhaps.

Try to run several times as it is - and if it behacves differently
then you have some hardware  issue or something
0
 

Author Comment

by:t38
ID: 35419981
actually i think that error was because i was trying to repopulate a row already in use -

here#s what i'm entering now -

insertRow(15, "TestOne", "Peter Andre", "Publish", 2000, 20.0);

OK, it's working now!  i think it was the link.close that did it.

thank you so much for all your help and patience....
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35419988
Great!

Perhpas there is some uniquer constraint somehwre in the table
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35420010
Well, CEHJ deserves credit in this case.
0
 
LVL 92

Expert Comment

by:objects
ID: 35420595
you should be using a PreparedStatement

let me know if you need help doing that
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35422317
>>Well, CEHJ deserves credit in this case.

Thanks for that for yan - next time maybe
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
The viewer will learn how to implement Singleton Design Pattern in Java.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month12 days, 9 hours left to enroll

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question