Database not being populated....

Reply

Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Database not being populated....

 
0
  #1
Feb 6th, 2005
I have the datasoure and drivers set up, but for some reason this code is not updating the database.
import java.sql.*;

public class TestCreateCoffeeTable
{
	public TestCreateCoffeeTable()
	{
	}
	public static void main(String[] args)
	{
	   try
	   {
		String createStatement;
		
		Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
		
		String dataSource = "jdbc:odbc:Coffeebreak";
		
		Connection con = DriverManager.getConnection(dataSource);
		
		Statement statement1 = con.createStatement();
		
		String insert;
		
		//add the info
		insert = "INSERT INTO Coffeebreak VALUES( 'Bob', 24, 23)";
		
		statement1.executeUpdate(insert);
		
	   }
	   catch(ClassNotFoundException cnfe)
	   {
		   System.err.println(cnfe);
	   }
	   catch(SQLException sqle)
	   {
		   System.err.println(sqle);
	   }
	   catch(Exception e)
	   {
		   System.err.println(e);
	   }
	}
}

I'm sure that the problem has to be with what's in bold, but it looks right...
I do have the code that created the table if you need that. It created three columns, Name, ID_NUM, and Pay...But none of those are being populated.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 213
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Database not being populated....

 
0
  #2
Feb 6th, 2005
Are you getting any errors?
Unless you have a DSN named exactly the same as the table you're trying to update that may be your problem.

Also remember that (unlike SQL standard...) MSAccess I think uses case sensitive table and field names.
The most correct form of the insert statement would also include the field names in the statement to ensure that they're in the order you want.
Furthermore you may or may not need to add a semicolon to the SQL statement. Officially it shouldn't be needed but some drivers and databases aren't as nice as others

So your SQL should be INSERT INTO Coffeebreak (Name, ID_NUM, Pay) VALUES ('Bob', 24, 23)
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: Database not being populated....

 
0
  #3
Feb 6th, 2005
I tried that, but it's still not working. There's not exceptions thrown, and I made sure no DNS has the same name...I checked case sensitivity, and everything was correct..Did I enter everything correctly according to the data types?

Name is a String
ID_NUM is an int
Pay is an int
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 35
Reputation: tigerxx is an unknown quantity at this point 
Solved Threads: 0
tigerxx tigerxx is offline Offline
Light Poster

Re: Database not being populated....

 
0
  #4
Feb 7th, 2005
**************************************
Statement statement1 = con.createStatement();
****************************************

here is the problem u hav to give the query inside createStatement()

like

Statement statement1 = con.createStatement(string);

then it'll work
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 213
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Database not being populated....

 
0
  #5
Feb 7th, 2005
there is no createStatement() method taking a String...

What's the returnvalue you get from executeUpdate()? That's the number of records the database reports inserted.

If it's 1 and afterwards you don't see them appearing, autocommit may be off on the database.
Either do an explicit commit or set autocommit to on.
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: Database not being populated....

 
0
  #6
Feb 7th, 2005
I tried something like this:

System.out.println(statement1.executeUpdate(insert));

but it throws an sql exception saying simply "general error"

If I shouldn't use System.out for checking the return val, please let me know how I should be checking it.. I do have the code that created the table, so I could post that if you think it's something in effect to it..
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 213
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Database not being populated....

 
0
  #7
Feb 7th, 2005
no, the executeUpdate threw an SQLException.
If there's no more detailed information given, you got to hope your database server has a log somewhere.
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: Database not being populated....

 
0
  #8
Feb 7th, 2005
I finally figured it out.. When I created the table, I specified the data type after the name of the field...I don't know how I could be that stupid(which is why I'm blaming my computer this time)... It did update the single test field like I wanted, but for some reason the same sql exception is being thrown. I guess it doesn't matter much now, but do you have any idea why it's still being thrown.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC