Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

JDBC MSAccess code works but not really!! Need Help

Status
Not open for further replies.

geekett

Technical User
Jun 18, 2001
4
US
I'm executing the following code and all of the System.out lines are printing fine, not getting any errors. However, when I open the access database, the record has not been inserted. What am I missing? Thanks.

public class JokesToDB
{
public static void main(String args[])
{
String strContrib;
String strJoke;
String strCat;

try {

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = dbf.newDocumentBuilder();
Document doc = builder.parse(new File(args[0]));
//Get NodeList of Joke elements
NodeList nlJoke = doc.getElementsByTagName("joke");
System.out.println("nlJoke length: " + nlJoke.getLength());

//Capture first Joke element
Element eltJoke = (Element) nlJoke.item(0);

//Capture Joketext element
Element eltJoketext = (Element) XPathAPI.selectSingleNode(eltJoke, "joketext");

//Retrieve text node value of joketext
strJoke=eltJoketext.getFirstChild().getNodeValue();
System.out.println("strJoke: " + strJoke);

//Retrieve attribute values from Joke element
strContrib = eltJoke.getAttribute("contributor");
System.out.println("strContrib: " + strContrib);
strCat = eltJoke.getAttribute("category");
System.out.println("strCat: " + strCat);

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("No problem with Class.forName");
String url = "jdbc:eek:dbc:AppDev";
Connection con = DriverManager.getConnection(url, "admin", "");
System.out.println("No problem with DriverManager.getConnection");
Statement stmt = con.createStatement();
System.out.println("No problem with con.createStatement");
String strSQL = "INSERT INTO Jokes (Contributor, Joketext, Category) VALUES ('" + strContrib + "', '" + strJoke + "', '" + strCat + "')";
System.out.println("strSQL: " + strSQL);
stmt.execute(strSQL);
System.out.println("No problem with executeUpdate");
}
catch (Throwable t){
System.out.println("Error: " + t.getMessage());
}

}
}
 
>>stmt.execute(strSQL);

should be
Code:
stmt.executeUpdate(strSQL)

hope this helps.
 
Sorry, I should have mentioned that I've tried both execute and executeUpdate. Same results.
 
Okay, I figured it out. After I closed the stmt and con objects, the insert was written.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top