View Full Version : ATTN: Java programmers (need urgent help)
mutant!
06-23-2006, 01:31 PM
UPDATE: I still need help! Details inside...
Sup guys - I'm busy doing a huge Java project for school. I must finish it by Tuesday, but I'm stuck at a very early stage. Anyone who's willing to help me out, get on MSN and add me - henre@danre.co.za.
Please, guys, this is an emergency.
EDIT: My problem relates to ODBC connectivity between a Microsoft Access database table and Java, if that helps.
UPDATE: OK, my latest problem is giving me grief. I've been able to connect my Java program to my Access database with success, and I can read data from the database. However, attempting to create a new record and enter a value into its fields only works once, after which I get a "general error" with absolutely no hints as to how to correct it.
For example, the line set.executeUpdate("INSERT INTO wine([Vintage]) VALUES ('2005')"); works perfectly the first time, creating a new record with no values other than "2005" in the "Vintage" field. After that, attempting to create any new records fails. Help?! The error refers to that specific line of code.
The Haitian
06-23-2006, 01:33 PM
Can you give a description of the project?
mutant!
06-23-2006, 01:35 PM
The specific part I'm struggling with is trying to extract data from a MS Access database table through Java, via ODBC. I'm getting the connection, but my program just doesn't extract the data. :confused:
mutant!
06-23-2006, 01:37 PM
never... focking... mind.
Stupid mistake. I figured it out now. After like half an hour. :angry:
mutant!
06-23-2006, 01:41 PM
JESUS H CHRIST
I thought I had it figured out but apparently I didn't.
The Haitian
06-23-2006, 01:43 PM
Ahh I've been there :p
I don't have MSN but if you have any other problems, shoot a message here. I've been programming for about 6 years in a wide variety of languages so I should be able to help you out.
edit:
JESUS H CHRIST
I thought I had it figured out but apparently I didn't.
What exactly does it do? Give you some sort of error message or does it just not retrieve anything?
mutant!
06-23-2006, 02:18 PM
The program connects to the database successfully, but either it's not retrieving the contents of the table or it's just not printing it to screen.
Do you have Java programming tools? I could YSI the files to you, see if you can spot something I can't.
mutant!
06-25-2006, 12:06 PM
bump for first post edit.
Cybergasm
06-25-2006, 01:09 PM
Are you closing the primary stream? After you finish writing to one, you would have to close the stream before writing to another file. Unless you are using seperate streams.
mutant!
06-25-2006, 01:53 PM
Are you closing the primary stream? After you finish writing to one, you would have to close the stream before writing to another file. Unless you are using seperate streams.
I do close the primary stream directly after. However, I'm trying to write to the same file multiple times. And regardless, that's not the problem. The problem is I can write to the file when it doesn't contain any records, but if there's even one record present, I get an error message instead.
Cybergasm
06-25-2006, 03:37 PM
You are sure you are not trying to write to the same line the second time around? That could be causing it.
I know how java IO works and file/input but I've never worked with manipulating Acess sheets before; just .dat, .ini and things of that nature.
It is still hard, however, to help you when we do not know what you are doing. I can think of many different scenarios where that would be a problem;
Scenario1;
your program takes input and writes it to the sheet; the problem could be that you start the stream at the beginning, close it once you write, then call the write method again without ever re-opening the stream.
Scenario2;
Your program is a one time execute that closes once done, and you try opening up another instance to write to the file, but the file is now Read-only, or, you actually create the file with your program and on the second time running it you try to create two instances of the same file.
If you could post code snippets or a description of the program it would help.
mutant!
06-25-2006, 03:58 PM
public static void main (String[] args)
{
Connection conn;
Statement set;
String sql;
if(!loadODBC())
return;
try
{
conn = DriverManager.getConnection ("jdbc:odbc:wineData","","");
set = conn.createStatement();
System.out.println("New wine added...");
set.executeUpdate("INSERT INTO wine([Vintage]) VALUES ('2005')");
conn.close();
}
catch (Exception e)
{
e.printStackTrace(); //used for troubleshooting
}
System.out.println("");
}
The Haitian
06-26-2006, 06:42 PM
How is the database set up? Do you have an autonumber generating the primary key? I haven't used Java with ODBC but I have used vb.net and vba with it. The only thing catching my eye is executeUpdate. Is that the correct function to use?
Here is a snippet of working code from a complete database utility in vb.net that I had to make for a class that allows me to add over and over. I also keep the connection open until the program is completely finished.
frmMain.ClaimDataAdapter.InsertCommand.CommandText = "INSERT INTO Claims VALUES('" & intClaimNumber & "', '" & dtFileDate & "', '" & strUPCNumber & "', '" & strReasonForReturn & "')"
If frmMain.ClaimDataAdapter.InsertCommand.ExecuteNonQ uery() > 0 Then
' Notify user that record was successfully added
I find that most of my database problems stem from improper SQL language. If you have fields in your database that are not required, you still need to pass NULL. Autonumber is the exception to this.
Your code looks pretty good. I would just check to make sure executeUpdate is the right function. I haven't used Java with ODBC though so it might be correct. Other than that, make sure your SQL syntax is correct and try passing NULL to nonrequired fields.
Sorry I didn't reply earlier. Some crap came up and I haven't had any time to get on the computer.
vBulletin® v3.8.1, Copyright ©2000-2009, Jelsoft Enterprises Ltd.